Module: RGData::Model::ClassMethods

Defined in:
lib/rgdata/model.rb

Instance Method Summary collapse

Instance Method Details

#field(name, &parse_block) ⇒ Object

Declare a field. It will be given attr_accessor. parse_block will be instructions on how to drill down into the xml_hash and get the value.



18
19
20
21
# File 'lib/rgdata/model.rb', line 18

def field(name, &parse_block)
  parse_blocks[name] = parse_block
  attr_accessor name
end

#new_from_xml_hash(xml_hash) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rgdata/model.rb', line 23

def new_from_xml_hash(xml_hash)
  attributes = parse_blocks.inject({}) do |atts, (field, parse_block)|
    begin
      atts[field] = parse_block.call(xml_hash)
      atts
    rescue Exception => ex
      raise FieldError.new(field, ex.message)
    end
  end
  new(attributes)
end