Class: XML::Mapping::ArrayNode

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

Overview

Node factory function synopsis:

array_node :_attrname_, _per_arrelement_path_
                  [, :default_value=>_obj_]
                  [, :optional=>true]
                  [, :class=>_c_]
                  [, :marshaller=>_proc_]
                  [, :unmarshaller=>_proc_]

-or-

array_node :_attrname_, _base_path_, _per_arrelement_path_
                  [keyword args the same]

Node that maps a sequence of sub-nodes of the XML tree to an attribute containing an array of Ruby objects, with each array element mapping to a corresponding member of the sequence of sub-nodes.

If base_path is not supplied, it is assumed to be “”. base_path"/"per_arrelement_path is an XPath expression that must “yield” the sequence of XML nodes that is to be mapped to the array. The difference between base_path and per_arrelement_path becomes important when marshalling the array attribute back to XML. When that happens, base_path names the most specific common parent node of all the mapped sub-nodes, and per_arrelement_path names (relative to base_path) the part of the path that is duplicated for each array element. For example, with base_path=="foo/bar" and per_arrelement_path=="hi/ho", an array [x,y,z] will be written to an XML structure that looks like this:

<foo>
 <bar>
  <hi>
   <ho>
    [marshalled object x]
   </ho>
  </hi>
  <hi>
   <ho>
    [marshalled object y]
   </ho>
  </hi>
  <hi>
   <ho>
    [marshalled object z]
   </ho>
  </hi>
 </bar>
</foo>

Instance Method Summary collapse

Methods inherited from SingleAttributeNode

#default_when_xpath_err, #initialize, #obj_initializing, #obj_to_xml, #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

#do_initialize(base_path, per_arrelement_path) ⇒ Object

“Real” initializer.



250
251
252
253
254
255
# File 'lib/xml/mapping/standard_nodes.rb', line 250

def do_initialize(base_path,per_arrelement_path)
	per_arrelement_path=per_arrelement_path[1..-1] if per_arrelement_path[0]==?/
	@base_path = XML::XXPath.new(base_path)
	@per_arrelement_path = XML::XXPath.new(per_arrelement_path)
	@reader_path = XML::XXPath.new(base_path+"/"+per_arrelement_path)
end

#extract_attr_value(xml) ⇒ Object

:nodoc:



256
257
258
259
260
261
262
# File 'lib/xml/mapping/standard_nodes.rb', line 256

def extract_attr_value(xml) # :nodoc:
  result = []
  default_when_xpath_err{@reader_path.all(xml)}.each do |elt|
    result << @options[:unmarshaller].call(elt)
  end
  result
end

#initialize_impl(path, path2 = nil) ⇒ Object

Initializer, delegates to do_initialize. Called with keyword arguments and either 1 or 2 paths; the hindmost path argument passed is delegated to per_arrelement_path; the preceding path argument (if present, “” by default) is delegated to base_path.



241
242
243
244
245
246
247
248
# File 'lib/xml/mapping/standard_nodes.rb', line 241

def initialize_impl(path,path2=nil)
  super
	if path2
	  do_initialize(path,path2)
	else
	  do_initialize("",path)
	end
end

#set_attr_value(xml, value) ⇒ Object

:nodoc:



263
264
265
266
267
268
# File 'lib/xml/mapping/standard_nodes.rb', line 263

def set_attr_value(xml, value) # :nodoc:
	base_elt = @base_path.first(xml,:ensure_created=>true)
	value.each do |arr_elt|
    @options[:marshaller].call(@per_arrelement_path.create_new(base_elt), arr_elt)
	end
end