Class: XML::Mapping::SubObjectBaseNode

Inherits:
SingleAttributeNode show all
Defined in:
lib/xml/mapping/standard_nodes.rb

Overview

(does somebody have a better name for this class?) base node class that provides an initializer which lets the user specify a means to marshal/unmarshal a Ruby object to/from XML. Used as the base class for nodes that map some sub-nodes of their XML tree to (Ruby-)sub-objects of their attribute.

Direct Known Subclasses

ArrayNode, HashNode, ObjectNode

Instance Method Summary collapse

Methods inherited from SingleAttributeNode

#default_when_xpath_err, #extract_attr_value, #initialize, #obj_initializing, #obj_to_xml, #set_attr_value, #xml_to_obj

Methods inherited from Node

#initialize, #obj_initializing, #obj_to_xml, #xml_to_obj

Constructor Details

This class inherits a constructor from XML::Mapping::SingleAttributeNode

Instance Method Details

#initialize_impl(*args) ⇒ Object

processes the keyword arguments :class, :marshaller, and :unmarshaller (args is ignored). When this initiaizer returns, @options and @options are set to procs that marshal/unmarshal a Ruby object to/from an XML tree according to the keyword arguments that were passed to the initializer:

You either supply a :class argument with a class implementing XML::Mapping – in that case, the subtree will be mapped to an instance of that class (using load_from_xml resp. fill_into_xml). Or, you supply :marshaller and :unmarshaller arguments specifying explicit unmarshaller/marshaller procs. The :marshaller proc takes arguments xml,value and must fill value (the object to be marshalled) into xml; the :unmarshaller proc takes xml and must extract and return the object value from it. Or, you specify none of those arguments, in which case the name of the class to create will be automatically deduced from the root element name of the XML node (see XML::Mapping::load_object_from_xml, XML::Mapping::class_for_root_elt_name).

If both :class and :marshaller/:unmarshaller arguments are supplied, the latter take precedence.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/xml/mapping/standard_nodes.rb', line 92

def initialize_impl(*args)
  if @options[:class]
    unless @options[:marshaller]
      @options[:marshaller] = proc {|xml,value|
        value.fill_into_xml(xml)
      }
    end
    unless @options[:unmarshaller]
      @options[:unmarshaller] = proc {|xml|
        @options[:class].load_from_xml(xml)
      }
    end
  end

  unless @options[:marshaller]
    @options[:marshaller] = proc {|xml,value|
      value.fill_into_xml(xml)
      if xml.unspecified?
        xml.name = value.class.root_element_name
        xml.unspecified = false
      end
    }
  end
  unless @options[:unmarshaller]
    @options[:unmarshaller] = proc {|xml|
      XML::Mapping.load_object_from_xml(xml)
    }
  end
end