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

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_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) ⇒ NumericNode

Returns a new instance of NumericNode.



46
47
48
49
50
# File 'lib/xml/mapping/standard_nodes.rb', line 46

def initialize(*args)
  path,*args = super(*args)
  @path = XML::XXPath.new(path)
  args
end

Instance Method Details

#extract_attr_value(xml) ⇒ Object

:nodoc:



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xml/mapping/standard_nodes.rb', line 52

def extract_attr_value(xml) # :nodoc:
  txt = default_when_xpath_err{ @path.first(xml).text }

  if txt.nil?
    raise NoAttrValueSet, "Attribute #{@attrname} not set (text missing)"
  else
    begin
      Integer(txt)
    rescue ArgumentError
      Float(txt)
    end
  end
end

#set_attr_value(xml, value) ⇒ Object

:nodoc:

Raises:

  • (RuntimeError)


66
67
68
69
# File 'lib/xml/mapping/standard_nodes.rb', line 66

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