Class: XML::Mapping::HashNode
- Inherits:
-
SubObjectBaseNode
- Object
- Node
- SingleAttributeNode
- SubObjectBaseNode
- XML::Mapping::HashNode
- Defined in:
- lib/xml/mapping/standard_nodes.rb
Overview
Node factory function synopsis:
hash_node :_attrname_, _per_hashelement_path_, _key_path_
[, :default_value=>_obj_]
[, :optional=>true]
[, :class=>_c_]
[, :marshaller=>_proc_]
[, :unmarshaller=>_proc_]
[, :mapping=>_m_]
[, :sub_mapping=>_sm_]
-
or -
hash_node :attrname, base_path, per_hashelement_path, key_path
[keyword args the same]
Node that maps a sequence of sub-nodes of the XML tree to an attribute containing a hash of Ruby objects, with each hash value mapping to a corresponding member of the sequence of sub-nodes. The (string-valued) hash key associated with a hash value v is mapped to the text of a specific sub-node of v’s sub-node.
Analogously to ArrayNode, base_path and per_arrelement_path define the XPath expression that “yields” the sequence of XML nodes, each of which maps to a value in the hash table. Relative to such a node, key_path_ names the node whose text becomes the associated hash key.
Instance Method Summary collapse
-
#extract_attr_value(xml) ⇒ Object
:nodoc:.
-
#initialize(*args) ⇒ HashNode
constructor
Initializer.
-
#set_attr_value(xml, value) ⇒ Object
:nodoc:.
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) ⇒ HashNode
Initializer. Called with keyword arguments and either 2 or 3 paths; the hindmost path argument passed is delegated to key_path, the preceding path argument is delegated to per_arrelement_path, the path preceding that argument (if present, “” by default) is delegated to base_path. The meaning of the keyword arguments is the same as for ObjectNode.
335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/xml/mapping/standard_nodes.rb', line 335 def initialize(*args) path1,path2,path3,*args = super(*args) base_path,per_hashelement_path,key_path = if path3 [path1,path2,path3] else ["",path1,path2] end per_hashelement_path=per_hashelement_path[1..-1] if per_hashelement_path[0]==?/ @base_path = XML::XXPath.new(base_path) @per_hashelement_path = XML::XXPath.new(per_hashelement_path) @key_path = XML::XXPath.new(key_path) @reader_path = XML::XXPath.new(base_path+"/"+per_hashelement_path) args end |
Instance Method Details
#extract_attr_value(xml) ⇒ Object
:nodoc:
349 350 351 352 353 354 355 356 357 |
# File 'lib/xml/mapping/standard_nodes.rb', line 349 def extract_attr_value(xml) # :nodoc: result = {} default_when_xpath_err{@reader_path.all(xml)}.each do |elt| key = @key_path.first(elt).text value = @unmarshaller.call(elt) result[key] = value end result end |
#set_attr_value(xml, value) ⇒ Object
:nodoc:
358 359 360 361 362 363 364 365 |
# File 'lib/xml/mapping/standard_nodes.rb', line 358 def set_attr_value(xml, value) # :nodoc: base_elt = @base_path.first(xml,:ensure_created=>true) value.each_pair do |k,v| elt = @per_hashelement_path.create_new(base_elt) @marshaller.call(elt,v) @key_path.first(elt,:ensure_created=>true).text = k end end |