Class: XmlHate::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/xml_hate/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Node



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/xml_hate/node.rb', line 7

def initialize(hash)
  @_keys = []
  all_items_in_the_hash_that_are_not_nil(hash).each do |k, v|
    @_keys << get_a_valid_property_name(k)
    form = convert_the_value_to_the_appropriate_form(v)
    if form.is_a?(Node)
      keys = form._keys.map { |x| x.to_s.singularize }.uniq
      if keys.count == 1 && form.send(keys.first.to_sym).is_a?(Array)
        form = form.send(keys.first.to_sym)
      end
    end
    create_accessor_for k, form
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



22
23
24
# File 'lib/xml_hate/node.rb', line 22

def method_missing(meth, *args, &blk)
  an_empty_array_for_plurals_or_empty_string_for_singulars meth
end

Instance Attribute Details

#_keysObject (readonly)

Returns the value of attribute _keys.



5
6
7
# File 'lib/xml_hate/node.rb', line 5

def _keys
  @_keys
end

Instance Method Details

#to_hashObject



26
27
28
# File 'lib/xml_hate/node.rb', line 26

def to_hash
  @_keys.reduce({}) { |t, i| t.merge(i => self.send(i) ) }
end