Class: XML::Mapping::SubObjectBaseNode
- Inherits:
-
SingleAttributeNode
- Object
- Node
- SingleAttributeNode
- XML::Mapping::SubObjectBaseNode
- 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
Instance Method Summary collapse
-
#initialize(*args) ⇒ SubObjectBaseNode
constructor
processes the keyword arguments :class, :marshaller, and :unmarshaller (args is ignored).
Methods inherited from SingleAttributeNode
#default_when_xpath_err, #extract_attr_value, #initialize_impl, #is_present_in?, #obj_initializing, #obj_to_xml, #set_attr_value, #xml_to_obj
Methods inherited from Node
#is_present_in?, #obj_initializing, #obj_to_xml, #xml_to_obj
Constructor Details
#initialize(*args) ⇒ SubObjectBaseNode
processes the keyword arguments :class, :marshaller, and :unmarshaller (args is ignored). When this initiaizer returns, @marshaller and @unmarshaller 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.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/xml/mapping/standard_nodes.rb', line 101 def initialize(*args) args = super(*args) @sub_mapping = [:sub_mapping] || @mapping @marshaller, @unmarshaller = [:marshaller], [:unmarshaller] if [:class] unless @marshaller @marshaller = proc {|xml,value| value.fill_into_xml xml, :mapping=>@sub_mapping if xml.unspecified? xml.name = value.class.root_element_name :mapping=>@sub_mapping xml.unspecified = false end } end unless @unmarshaller @unmarshaller = proc {|xml| [:class].load_from_xml xml, :mapping=>@sub_mapping } end end unless @marshaller @marshaller = proc {|xml,value| value.fill_into_xml xml, :mapping=>@sub_mapping if xml.unspecified? xml.name = value.class.root_element_name :mapping=>@sub_mapping xml.unspecified = false end } end unless @unmarshaller @unmarshaller = proc {|xml| XML::Mapping.load_object_from_xml xml, :mapping=>@sub_mapping } end args end |