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 or a String 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 #xml_initialize method.

See also: xml_initialize



575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/roxml.rb', line 575

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

  unless xml_construction_args_without_deprecation.empty?
    args = xml_construction_args_without_deprecation.map do |arg|
       roxml_attrs.find {|attr| attr.accessor == arg }
    end.map {|attr| attr.to_ref(self).value_in(xml) }
    new(*args)
  else
    returning allocate do |inst|
      roxml_attrs.each do |attr|
        inst.instance_variable_set("@#{attr.variable_name}", attr.to_ref(inst).value_in(xml))
      end
      inst.send(:xml_initialize, *initialization_args)
    end
  end
end

#parse(data) ⇒ Object

Deprecated in favor of #from_xml



594
595
596
# File 'lib/roxml.rb', line 594

def parse(data) # :nodoc:
  from_xml(data)
end