Module: FacturaePrint::XMLObjectifier

Defined in:
lib/facturae_print/xml_objectifier.rb

Class Method Summary collapse

Class Method Details

.build(node, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/facturae_print/xml_objectifier.rb', line 4

def self.build(node, options={})
  options = {:collection_nodes => []}.merge(options)
  return node.text if node.text?
  
  children = get_node_children(node)
  return nil if children.empty? && node.content.empty?
  
  if children.size == 1 && children.first.text?
    children.first.text
  else
    object = options.delete(:object) || OpenStruct.new
    children.each do |child|
      value = if options[:collection_nodes].include?(child.name)
        get_node_children(child).map{|c| build(c, options)}
      else 
        build(child, options)
      end
      
      object.send("#{child.name.underscore}=", value)
    end
    object
  end
end