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, #reverse_each, #slice_before, #sort, #sort_by, #take, #take_while, #zip

Constructor Details

#initialize(*args) ⇒ Struct

Returns a new instance of Struct



54
55
56
57
58
# File 'opal/opal/corelib/struct.rb', line 54

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

Class Method Details

.define_struct_attribute(name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'opal/opal/corelib/struct.rb', line 18

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
    instance_variable_get "@#{name}"
  end

  define_method "#{name}=" do |value|
    instance_variable_set "@#{name}", value
  end
end

.inherited(klass) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'opal/opal/corelib/struct.rb', line 42

def self.inherited(klass)
  return if self == Struct

  members = @members

  klass.instance_eval {
    @members = members
  }
end

.membersObject



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

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

  @members ||= []
end

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



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'opal/opal/corelib/struct.rb', line 2

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



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

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

    name = members[name]
  else
    raise NameError, "no member '#{name}' in struct" unless members.include?(name.to_sym)
  end

  instance_variable_get "@#{name}"
end

#[]=(name, value) ⇒ Object



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

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

    name = members[name]
  else
    raise NameError, "no member '#{name}' in struct" unless members.include?(name.to_sym)
  end

  instance_variable_set "@#{name}", value
end

#eachObject



88
89
90
91
92
# File 'opal/opal/corelib/struct.rb', line 88

def each
  return enum_for :each unless block_given?

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

#each_pairObject



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

def each_pair
  return enum_for :each_pair unless block_given?

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

#eql?(other) ⇒ Boolean

Returns:



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

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

#inspectObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'opal/opal/corelib/struct.rb', line 118

def inspect
  result = "#<struct "

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

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

  result += ">"

  result
end

#lengthObject Also known as: size



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

def length
  members.length
end

#membersObject



60
61
62
# File 'opal/opal/corelib/struct.rb', line 60

def members
  self.class.members
end

#to_aObject Also known as: values



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

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