Module: ROXML::ClassMethods::Operations

Defined in:
lib/roxml.rb

Instance Method Summary collapse

Instance Method Details

#from_xml(data, *initialization_args) ⇒ Object

Creates a new Ruby object from XML using mapping information annotated in the class.

The input data is either an XML::Node, String, Pathname, or File representing the XML document.

Example

book = Book.from_xml(File.read("book.xml"))

or

book = Book.from_xml("<book><name>Beyond Java</name></book>")

initialization_args passed into from_xml will be passed into the object’s .new, prior to populating the xml_attrs.

After the instatiation and xml population

See also: xml_initialize



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/roxml.rb', line 493

def from_xml(data, *initialization_args)
  xml = XML::Node.from(data)

  returning new(*initialization_args) do |inst|
    roxml_attrs.each do |attr|
      value = attr.to_ref(inst).value_in(xml)
      inst.respond_to?(attr.setter) \
        ? inst.send(attr.setter, value) \
        : inst.instance_variable_set(attr.instance_variable_name, value)
    end
    inst.send(:after_parse) if inst.respond_to?(:after_parse, true)
  end
rescue ArgumentError => e
  raise e, e.message + " for class #{self}"
end