Module: SuperStruct::InstanceMethods

Defined in:
lib/super_struct.rb

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



34
35
36
# File 'lib/super_struct.rb', line 34

def ==(other)
  other.respond_to?(:attributes) && attributes == other.attributes
end

#attributesObject



17
18
19
20
21
22
# File 'lib/super_struct.rb', line 17

def attributes
  members.inject({}) do |attributes, member|
    attributes[member] = send(member)
    attributes
  end
end

#deep_convert!Object



24
25
26
27
28
29
30
31
32
# File 'lib/super_struct.rb', line 24

def deep_convert!
  members.each do |member|
    if self[member].respond_to?(:has_key?) && self[member].size > 0
      self[member] = ::SuperStruct.from_hash(self[member])
      self[member].deep_convert!
    end
  end
  self
end

#initialize(*input) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/super_struct.rb', line 5

def initialize(*input)
  if input.first.respond_to?(:has_key?)
    keys, values = input.first.sort.transpose
    unless keys.map(&:to_sym) == members
      raise ArgumentError.new("#{self.class.name} expects a hash with keys #{members.join(', ')}")
    end
    super(*values)
  else
    super(*input)
  end
end