class XML::Mapping::SubObjectBaseNode

(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.

Public Class Methods

new(*args) click to toggle source

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.

Calls superclass method XML::Mapping::SingleAttributeNode.new
# File lib/xml/mapping/standard_nodes.rb, line 101
def initialize(*args)
  args = super(*args)

  @sub_mapping = @options[:sub_mapping] || @mapping
  @marshaller, @unmarshaller = @options[:marshaller], @options[:unmarshaller]

  if @options[: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|
        @options[: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