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



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/roxml.rb', line 529

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

  new(*initialization_args).tap 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