Module: XML::Mapping::ClassMethods

Defined in:
lib/support/xml_mapping.rb

Instance Method Summary collapse

Instance Method Details

#load_from_xml(xml, options = {:mapping=>:_default}) ⇒ Object

Base on original method

Raises:

  • (MappingError)


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

def load_from_xml(xml, options={:mapping=>:_default})
  raise(MappingError, "undefined mapping: #{options[:mapping].inspect}") \
    unless xml_mapping_nodes_hash.has_key?(options[:mapping]) || 
      (superclass && superclass.xml_mapping_nodes_hash.has_key?(options[:mapping]))
  # create the new object. It is recommended that the class
  # have a no-argument initializer, so try new first. If that
  # doesn't work, try allocate, which bypasses the initializer.
  begin
    obj = self.new
  rescue ArgumentError # TODO: this may hide real errors.
                       #   how to statically check whether
                       #   self self.new accepts an empty
                       #   argument list?
    obj = self.allocate
  end
  obj.initialize_xml_mapping :mapping=>options[:mapping]
  obj.fill_from_xml xml, :mapping=>options[:mapping]
  obj
end