Class: DataMapper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ DataMapper

Returns a new instance of DataMapper.



6
7
8
9
# File 'lib/data_mapper.rb', line 6

def initialize(string)
  @doc = Nokogiri.XML(string)
  @mapper = map(@doc.root)
end

Instance Attribute Details

#mapperObject (readonly)

Returns the value of attribute mapper.



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

def mapper
  @mapper
end

Instance Method Details

#adder(el) ⇒ Object



15
16
17
# File 'lib/data_mapper.rb', line 15

def adder(el)
  el.elements.empty? ? el.text : map(el)
end

#map(main_el) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/data_mapper.rb', line 19

def map(main_el)
  obj = OpenStruct.new
  main_el.elements.each do |el|
    plur = plural(el.name)
    if obj.respond_to?(plur)
      obj.send(plur) << adder(el)
    elsif main_el.css(el.name).size > 1
      obj.send("#{plur}=", [])
      obj.send(plur) << adder(el) 
    elsif el.elements.empty?
      obj.send("#{el.name}=", el.text)
    else
      obj.send("#{el.name}=", map(el))
    end
  end 
  obj
end

#plural(string) ⇒ Object



11
12
13
# File 'lib/data_mapper.rb', line 11

def plural(string)
  string + 's'
end