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



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

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 new(*initialization_args) do |inst|
      roxml_attrs.each do |attr|
        value = attr.to_ref(inst).value_in(xml)
        setter = :"#{attr.variable_name}="
        inst.respond_to?(setter) \
          ? inst.send(setter, value) \
          : inst.instance_variable_set("@#{attr.variable_name}", value)
      end
      inst.try(:after_parse)
    end
  end
rescue ArgumentError => e
  raise e, e.message + " for class #{self}"
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