Class: Struct
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- opal/opal/corelib/struct.rb,
opal/opal/corelib/marshal/write_buffer.rb,
opal/opal/corelib/pattern_matching/base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Enumerable
#all?, #any?, #chunk, #chunk_while, #collect, #collect_concat, #compact, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #enumerator_size, #filter_map, #find_all, #find_index, #first, #grep, #grep_v, #group_by, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #slice_after, #slice_before, #slice_when, #sort, #sort_by, #sum, #take, #take_while, #tally, #to_set, #uniq, #zip
Constructor Details
#initialize(*args) ⇒ Struct
Returns a new instance of Struct.
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'opal/opal/corelib/struct.rb', line 86
def initialize(*args)
if `#{self.class}.$$keyword_init`
kwargs = args.last || {}
if args.length > 1 || `(args.length === 1 && !kwargs.$$is_hash)`
::Kernel.raise ::ArgumentError, "wrong number of arguments (given #{args.length}, expected 0)"
end
= kwargs.keys - self.class.members
if .any?
::Kernel.raise ::ArgumentError, "unknown keywords: #{.join(', ')}"
end
self.class.members.each do |name|
self[name] = kwargs[name]
end
else
if args.length > self.class.members.length
::Kernel.raise ::ArgumentError, 'struct size differs'
end
self.class.members.each_with_index do |name, index|
self[name] = args[index]
end
end
end
|
Class Method Details
.define_struct_attribute(name) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'opal/opal/corelib/struct.rb', line 54
def self.define_struct_attribute(name)
if self == ::Struct
::Kernel.raise ::ArgumentError, 'you cannot define attributes to the Struct class'
end
members << name
define_method name do
`self.$$data[name]`
end
define_method "#{name}=" do |value|
`self.$$data[name] = value`
end
end
|
.inherited(klass) ⇒ Object
78
79
80
81
82
83
84
|
# File 'opal/opal/corelib/struct.rb', line 78
def self.inherited(klass)
members = @members
klass.instance_eval do
@members = members
end
end
|
.keyword_init? ⇒ Boolean
124
125
126
|
# File 'opal/opal/corelib/struct.rb', line 124
def self.keyword_init?
`self.$$keyword_init`
end
|
70
71
72
73
74
75
76
|
# File 'opal/opal/corelib/struct.rb', line 70
def self.members
if self == ::Struct
::Kernel.raise ::ArgumentError, 'the Struct class has no members'
end
@members ||= []
end
|
.new(const_name, *args, keyword_init: false, &block) ⇒ Object
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'opal/opal/corelib/struct.rb', line 8
def self.new(const_name, *args, keyword_init: false, &block)
if const_name
if const_name.class == ::String && const_name[0].upcase != const_name[0]
args.unshift(const_name)
const_name = nil
else
begin
const_name = ::Opal.const_name!(const_name)
rescue ::TypeError, ::NameError
args.unshift(const_name)
const_name = nil
end
end
end
args.map do |arg|
::Opal.coerce_to!(arg, ::String, :to_str)
end
klass = ::Class.new(self) do
args.each { |arg| define_struct_attribute(arg) }
class << self
def new(*args)
instance = allocate
`#{instance}.$$data = {}`
instance.initialize(*args)
instance
end
alias_method :[], :new
end
end
klass.module_eval(&block) if block
`klass.$$keyword_init = keyword_init`
if const_name
::Struct.const_set(const_name, klass)
end
klass
end
|
Instance Method Details
#==(other) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'opal/opal/corelib/struct.rb', line 172
def ==(other)
return false unless other.instance_of?(self.class)
%x{
var recursed1 = {}, recursed2 = {};
function _eqeq(struct, other) {
var key, a, b;
recursed1[#{`struct`.__id__}] = true;
recursed2[#{`other`.__id__}] = true;
for (key in struct.$$data) {
a = struct.$$data[key];
b = other.$$data[key];
if (#{::Struct === `a`}) {
if (!recursed1.hasOwnProperty(#{`a`.__id__}) || !recursed2.hasOwnProperty(#{`b`.__id__})) {
if (!_eqeq(a, b)) {
return false;
}
}
} else {
if (!#{`a` == `b`}) {
return false;
}
}
}
return true;
}
return _eqeq(self, other);
}
end
|
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'opal/opal/corelib/struct.rb', line 136
def [](name)
if ::Integer === name
::Kernel.raise ::IndexError, "offset #{name} too small for struct(size:#{self.class.members.size})" if name < -self.class.members.size
::Kernel.raise ::IndexError, "offset #{name} too large for struct(size:#{self.class.members.size})" if name >= self.class.members.size
name = self.class.members[name]
elsif ::String === name
%x{
if(!self.$$data.hasOwnProperty(name)) {
#{::Kernel.raise ::NameError.new("no member '#{name}' in struct", name)}
}
}
else
::Kernel.raise ::TypeError, "no implicit conversion of #{name.class} into Integer"
end
name = ::Opal.coerce_to!(name, ::String, :to_str)
`self.$$data[name]`
end
|
#[]=(name, value) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
# File 'opal/opal/corelib/struct.rb', line 156
def []=(name, value)
if ::Integer === name
::Kernel.raise ::IndexError, "offset #{name} too small for struct(size:#{self.class.members.size})" if name < -self.class.members.size
::Kernel.raise ::IndexError, "offset #{name} too large for struct(size:#{self.class.members.size})" if name >= self.class.members.size
name = self.class.members[name]
elsif ::String === name
::Kernel.raise ::NameError.new("no member '#{name}' in struct", name) unless self.class.members.include?(name.to_sym)
else
::Kernel.raise ::TypeError, "no implicit conversion of #{name.class} into Integer"
end
name = ::Opal.coerce_to!(name, ::String, :to_str)
`self.$$data[name] = value`
end
|
#__marshal__(buffer) ⇒ Object
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 168
def __marshal__(buffer)
buffer.save_link(self)
buffer.write_ivars_prefix(self)
buffer.write_extends(self)
buffer.append('S')
buffer.append_symbol(self.class.name)
buffer.write_fixnum(length)
each_pair do |attr_name, value|
buffer.append_symbol(attr_name)
buffer.write(value)
end
buffer.write_ivars_suffix(self)
end
|
#deconstruct_keys(keys) ⇒ Object
This function is specified in a very weird way...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'opal/opal/corelib/pattern_matching/base.rb', line 16
def deconstruct_keys(keys)
return to_h if keys.nil?
::Kernel.raise ::TypeError, 'expected Array or nil' unless ::Array === keys
return {} if keys.length > values.length
out = {}
keys.each do |key|
should_break = case key
when ::Integer
values.length < key
when ::Symbol !members.include?(key)
end
break if should_break
out[key] = self[key]
end
out
end
|
#dig(key, *keys) ⇒ Object
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# File 'opal/opal/corelib/struct.rb', line 313
def dig(key, *keys)
item = if `key.$$is_string && self.$$data.hasOwnProperty(key)`
`self.$$data[key] || nil`
end
%x{
if (item === nil || keys.length === 0) {
return item;
}
}
unless item.respond_to?(:dig)
::Kernel.raise ::TypeError, "#{item.class} does not have #dig method"
end
item.dig(*keys)
end
|
244
245
246
247
248
249
|
# File 'opal/opal/corelib/struct.rb', line 244
def each
return enum_for(:each) { size } unless block_given?
self.class.members.each { |name| yield self[name] }
self
end
|
#each_pair ⇒ Object
251
252
253
254
255
256
|
# File 'opal/opal/corelib/struct.rb', line 251
def each_pair
return enum_for(:each_pair) { size } unless block_given?
self.class.members.each { |name| yield [name, self[name]] }
self
end
|
#eql?(other) ⇒ Boolean
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
# File 'opal/opal/corelib/struct.rb', line 208
def eql?(other)
return false unless other.instance_of?(self.class)
%x{
var recursed1 = {}, recursed2 = {};
function _eqeq(struct, other) {
var key, a, b;
recursed1[#{`struct`.__id__}] = true;
recursed2[#{`other`.__id__}] = true;
for (key in struct.$$data) {
a = struct.$$data[key];
b = other.$$data[key];
if (#{::Struct === `a`}) {
if (!recursed1.hasOwnProperty(#{`a`.__id__}) || !recursed2.hasOwnProperty(#{`b`.__id__})) {
if (!_eqeq(a, b)) {
return false;
}
}
} else {
if (!#{`a`.eql?(`b`)}) {
return false;
}
}
}
return true;
}
return _eqeq(self, other);
}
end
|
132
133
134
|
# File 'opal/opal/corelib/struct.rb', line 132
def hash
[self.class, to_a].hash
end
|
#initialize_copy(from) ⇒ Object
113
114
115
116
117
118
119
120
121
122
|
# File 'opal/opal/corelib/struct.rb', line 113
def initialize_copy(from)
%x{
self.$$data = {}
var keys = Object.keys(from.$$data), i, max, name;
for (i = 0, max = keys.length; i < max; i++) {
name = keys[i];
self.$$data[name] = from.$$data[name];
}
}
end
|
#inspect ⇒ Object
Also known as:
to_s
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
# File 'opal/opal/corelib/struct.rb', line 268
def inspect
result = '#<struct '
if `inspect_stack`.include? __id__
result + ':...>'
else
`inspect_stack` << __id__
pushed = true
if ::Struct === self && self.class.name
result += "#{self.class} "
end
result += each_pair.map do |name, value|
"#{name}=#{Opal.inspect(value)}"
end.join ', '
result += '>'
result
end
ensure
`inspect_stack.pop()` if pushed
end
|
#length ⇒ Object
Also known as:
size
258
259
260
|
# File 'opal/opal/corelib/struct.rb', line 258
def length
self.class.members.length
end
|
128
129
130
|
# File 'opal/opal/corelib/struct.rb', line 128
def members
self.class.members
end
|
#to_a ⇒ Object
Also known as:
values, deconstruct
262
263
264
|
# File 'opal/opal/corelib/struct.rb', line 262
def to_a
self.class.members.map { |name| self[name] }
end
|
#to_h(*args, &block) ⇒ Object
293
294
295
296
297
|
# File 'opal/opal/corelib/struct.rb', line 293
def to_h(*args, &block)
return map(&block).to_h(*args) if block_given?
self.class.members.each_with_object({}) { |name, h| h[name] = self[name] }
end
|
#values_at(*args) ⇒ Object
299
300
301
302
303
304
305
306
307
308
309
310
311
|
# File 'opal/opal/corelib/struct.rb', line 299
def values_at(*args)
args = args.map { |arg| `arg.$$is_range ? #{arg.to_a} : arg` }.flatten
%x{
var result = [];
for (var i = 0, len = args.length; i < len; i++) {
if (!args[i].$$is_number) {
#{::Kernel.raise ::TypeError, "no implicit conversion of #{`args[i]`.class} into Integer"}
}
result.push(#{self[`args[i]`]});
}
return result;
}
end
|