Class: XML::Mapping::NumericNode

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

Overview

Node factory function synopsis:

numeric_node :_attrname_, _path_ [, :default_value=>_obj_]
                               [, :optional=>true]

Like TextNode, but interprets the XML node's text as a number (Integer or Float, depending on the nodes's text) and maps it to an Integer or Float attribute.

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

#extract_attr_value(xml) ⇒ Object

:nodoc:



48
49
50
51
52
53
54
55
# File 'lib/xml/mapping/standard_nodes.rb', line 48

def extract_attr_value(xml) # :nodoc:
  txt = default_when_xpath_err{ @path.first(xml).text }
  begin
    Integer(txt)
  rescue ArgumentError
    Float(txt)
  end
end

#initialize_impl(path) ⇒ Object



45
46
47
# File 'lib/xml/mapping/standard_nodes.rb', line 45

def initialize_impl(path)
  @path = XML::XXPath.new(path)
end

#set_attr_value(xml, value) ⇒ Object

:nodoc:

Raises:

  • (RuntimeError)


56
57
58
59
# File 'lib/xml/mapping/standard_nodes.rb', line 56

def set_attr_value(xml, value) # :nodoc:
  raise RuntimeError, "Not an integer: #{value}" unless Numeric===value
  @path.first(xml,:ensure_created=>true).text = value.to_s
end