Class: RubyXmlMapper::HashOfStringAndNumeric

Inherits:
Hash
  • Object
show all
Defined in:
lib/ruby-xml-mapper/basic_containers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_from_xml_node(node) ⇒ Object



70
71
72
73
74
# File 'lib/ruby-xml-mapper/basic_containers.rb', line 70

def self.new_from_xml_node node
  created = new
  created.initialize_from_xml_node node
  created
end

Instance Method Details

#autocast_value(value) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby-xml-mapper/basic_containers.rb', line 85

def autocast_value value
    if value =~ /\A\s*-*\d+\.\d+\s*\z/
      value.to_f

    elsif value =~ /\A\s*-*\d+\s*\z/
      value.to_i

    elsif matchdata = value.match(/\A\s*(-*\d+)\s*\.\.\s*(-*\d+)\s*\z/)
      Range.new(matchdata[1].to_i, matchdata[2].to_i)

    elsif value =~ /\A\s*true\s*\z/i
      true

    elsif value =~ /\A\s*false\s*\z/i
      false

    else
      value.strip.gsub(/\s*[\r\n]\s*/, "\n").gsub(/\n\s*\n/, "\n")
      
    end
end

#initialize_from_xml_node(node) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/ruby-xml-mapper/basic_containers.rb', line 76

def initialize_from_xml_node node
  node.each_element do |child|
    self[child.name.to_sym] = autocast_value(child.content)
  end
  node.each_attr do |attr|
    self[attr.name.to_sym] = autocast_value(attr.value)
  end
end