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_]
                  [, :mapping=>_m_]
                  [, :sub_mapping=>_sm_]

-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_impl, #is_present_in?, #obj_initializing, #obj_to_xml, #xml_to_obj

Methods inherited from Node

#is_present_in?, #obj_initializing, #obj_to_xml, #xml_to_obj

Constructor Details

#initialize(*args) ⇒ ArrayNode

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



270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/xml/mapping/standard_nodes.rb', line 270

def initialize(*args)
  path,path2,*args = super(*args)
  base_path,per_arrelement_path = if path2
                                    [path,path2]
                                  else
                                    [".",path]
                                  end
  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)
  args
end

Instance Method Details

#extract_attr_value(xml) ⇒ Object

:nodoc:



283
284
285
286
287
288
289
# File 'lib/xml/mapping/standard_nodes.rb', line 283

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

#set_attr_value(xml, value) ⇒ Object

:nodoc:



290
291
292
293
294
295
# File 'lib/xml/mapping/standard_nodes.rb', line 290

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