Class: Struct

Inherits:
Object
  • 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, #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



52
53
54
55
56
# File 'opal/opal/corelib/struct.rb', line 52

def initialize(*args)
  members.each_with_index {|name, index|
    instance_variable_set "@#{name}", args[index]
  }
end

Class Method Details

.define_struct_attribute(name) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'opal/opal/corelib/struct.rb', line 22

def self.define_struct_attribute(name)
  if self == Struct
    raise ArgumentError, 'you cannot define attributes to the Struct class'
  end

  members << name

  attr_accessor name
end

.inherited(klass) ⇒ Object



40
41
42
43
44
45
46
# File 'opal/opal/corelib/struct.rb', line 40

def self.inherited(klass)
  members = @members

  klass.instance_eval {
    @members = members
  }
end

.membersObject



32
33
34
35
36
37
38
# File 'opal/opal/corelib/struct.rb', line 32

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

  @members ||= []
end

.new(name = undefined, *args, &block) ⇒ Object Also known as: []



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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 }

      instance_eval(&block) if block
    }
  end
end

Instance Method Details

#[](name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'opal/opal/corelib/struct.rb', line 62

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
    raise NameError, "no member '#{name}' in struct" unless members.include?(name.to_sym)
  else
    raise TypeError, "no implicit conversion of #{name.class} into Integer"
  end

  instance_variable_get "@#{name}"
end

#[]=(name, value) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'opal/opal/corelib/struct.rb', line 77

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, "no member '#{name}' in struct" unless members.include?(name.to_sym)
  else
    raise TypeError, "no implicit conversion of #{name.class} into Integer"
  end

  instance_variable_set "@#{name}", value
end

#eachObject



92
93
94
95
96
97
# File 'opal/opal/corelib/struct.rb', line 92

def each
  return enum_for :each unless block_given?

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

#each_pairObject



99
100
101
102
103
104
# File 'opal/opal/corelib/struct.rb', line 99

def each_pair
  return enum_for :each_pair unless block_given?

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

#eql?(other) ⇒ Boolean

Returns:



106
107
108
109
110
# File 'opal/opal/corelib/struct.rb', line 106

def eql?(other)
  hash == other.hash || other.each_with_index.all? {|object, index|
    self[members[index]] == object
  }
end

#inspectObject Also known as: to_s



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'opal/opal/corelib/struct.rb', line 124

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



112
113
114
# File 'opal/opal/corelib/struct.rb', line 112

def length
  members.length
end

#membersObject



58
59
60
# File 'opal/opal/corelib/struct.rb', line 58

def members
  self.class.members
end

#to_aObject Also known as: values



118
119
120
# File 'opal/opal/corelib/struct.rb', line 118

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

#to_hObject



142
143
144
# File 'opal/opal/corelib/struct.rb', line 142

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

#values_at(*args) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'opal/opal/corelib/struct.rb', line 146

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