Class: Struct

Inherits:
Object show all
Includes:
Enumerable
Defined in:
opal/opal/corelib/struct.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#all?, #any?, #chunk, #collect, #collect_concat, #count, #cycle, #detect, #drop, #drop_while, #each_cons, #each_entry, #each_slice, #each_with_index, #each_with_object, #entries, #enumerator_size, #find_all, #find_index, #first, #grep, #group_by, #include?, #inject, #lazy, #max, #max_by, #min, #min_by, #minmax, #minmax_by, #none?, #one?, #partition, #reject, #reverse_each, #slice_before, #sort, #sort_by, #take, #take_while, #zip

Constructor Details

#initialize(*args) ⇒ Struct

Returns a new instance of Struct



65
66
67
68
69
# File 'opal/opal/corelib/struct.rb', line 65

def initialize(*args)
  members.each_with_index {|name, index|
    self[name] = args[index]
  }
end

Class Method Details

.define_struct_attribute(name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'opal/opal/corelib/struct.rb', line 33

def self.define_struct_attribute(name)
  if self == Struct
    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



57
58
59
60
61
62
63
# File 'opal/opal/corelib/struct.rb', line 57

def self.inherited(klass)
  members = @members

  klass.instance_eval {
    @members = members
  }
end

.membersObject



49
50
51
52
53
54
55
# File 'opal/opal/corelib/struct.rb', line 49

def self.members
  if self == Struct
    raise ArgumentError, 'the Struct class has no members'
  end

  @members ||= []
end

.new(name = undefined, *args, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'opal/opal/corelib/struct.rb', line 6

def self.new(name = undefined, *args, &block)
  return super unless self == Struct

  if name[0] == name[0].upcase
    Struct.const_set(name, new(*args))
  else
    args.unshift name

    Class.new(self) {
      args.each { |arg| define_struct_attribute arg }

      class_eval(&block) if block

      class << self
        def new(*args)
          instance = allocate
          `#{instance}.$$data = {};`
          instance.initialize(*args)
          instance
        end

        alias [] new
      end
    }
  end
end

Instance Method Details

#==(other) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'opal/opal/corelib/struct.rb', line 115

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

#[](name) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'opal/opal/corelib/struct.rb', line 79

def [](name)
  if Integer === name
    raise IndexError, "offset #{name} too small for struct(size:#{members.size})" if name < -members.size
    raise IndexError, "offset #{name} too large for struct(size:#{members.size})" if name >= members.size

    name = members[name]
  elsif String === name
    %x{
      if(!self.$$data.hasOwnProperty(name)) {
        #{raise NameError.new("no member '#{name}' in struct", name)}
      }
    }
  else
    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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'opal/opal/corelib/struct.rb', line 99

def []=(name, value)
  if Integer === name
    raise IndexError, "offset #{name} too small for struct(size:#{members.size})" if name < -members.size
    raise IndexError, "offset #{name} too large for struct(size:#{members.size})" if name >= members.size

    name = members[name]
  elsif String === name
    raise NameError.new("no member '#{name}' in struct", name) unless members.include?(name.to_sym)
  else
    raise TypeError, "no implicit conversion of #{name.class} into Integer"
  end

  name = Opal.coerce_to!(name, String, :to_str)
  `self.$$data[name] = value`
end

#eachObject



187
188
189
190
191
192
# File 'opal/opal/corelib/struct.rb', line 187

def each
  return enum_for(:each){self.size} unless block_given?

  members.each { |name| yield self[name] }
  self
end

#each_pairObject



194
195
196
197
198
199
# File 'opal/opal/corelib/struct.rb', line 194

def each_pair
  return enum_for(:each_pair){self.size} unless block_given?

  members.each { |name| yield [name, self[name]] }
  self
end

#eql?(other) ⇒ Boolean

Returns:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'opal/opal/corelib/struct.rb', line 151

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

#hashObject



75
76
77
# File 'opal/opal/corelib/struct.rb', line 75

def hash
  Hash.new(`self.$$data`).hash
end

#inspectObject Also known as: to_s



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'opal/opal/corelib/struct.rb', line 213

def inspect
  result = "#<struct "

  if self.class == Struct
    result += "#{self.class} "
  end

  result += each_pair.map {|name, value|
    "#{name}=#{value.inspect}"
  }.join ", "

  result += ">"

  result
end

#lengthObject Also known as: size



201
202
203
# File 'opal/opal/corelib/struct.rb', line 201

def length
  members.length
end

#membersObject



71
72
73
# File 'opal/opal/corelib/struct.rb', line 71

def members
  self.class.members
end

#to_aObject Also known as: values



207
208
209
# File 'opal/opal/corelib/struct.rb', line 207

def to_a
  members.map { |name| self[name] }
end

#to_hObject



231
232
233
# File 'opal/opal/corelib/struct.rb', line 231

def to_h
  members.inject({}) {|h, name| h[name] = self[name]; h}
end

#values_at(*args) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'opal/opal/corelib/struct.rb', line 235

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) {
        #{raise TypeError, "no implicit conversion of #{`args[i]`.class} into Integer"}
      }
      result.push(#{self[`args[i]`]});
    }
    return result;
  }
end