Class: RubyXmlMapper::Hash
- Inherits:
-
Hash
- Object
- Hash
- RubyXmlMapper::Hash
- Defined in:
- lib/ruby-xml-mapper/basic_containers.rb
Instance Method Summary collapse
-
#autocast_value(value) ⇒ Object
we’re eating copypasta!.
- #cast_to_boolean(value) ⇒ Object
- #cast_to_float(value) ⇒ Object
- #cast_to_integer(value) ⇒ Object
- #cast_to_range(value) ⇒ Object
- #cast_to_string(value) ⇒ Object
- #initialize_from_xml_node(node) ⇒ Object
Instance Method Details
#autocast_value(value) ⇒ Object
we’re eating copypasta!
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ruby-xml-mapper/basic_containers.rb', line 88 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 |
#cast_to_boolean(value) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/ruby-xml-mapper/basic_containers.rb', line 123 def cast_to_boolean value if value =~ /\A\s*true\s*\z/i true elsif value =~ /\A\s*false\s*\z/i false else nil end end |
#cast_to_float(value) ⇒ Object
110 111 112 |
# File 'lib/ruby-xml-mapper/basic_containers.rb', line 110 def cast_to_float value value.to_f end |
#cast_to_integer(value) ⇒ Object
114 115 116 |
# File 'lib/ruby-xml-mapper/basic_containers.rb', line 114 def cast_to_integer value value.to_i end |
#cast_to_range(value) ⇒ Object
118 119 120 121 |
# File 'lib/ruby-xml-mapper/basic_containers.rb', line 118 def cast_to_range value matchdata = value.match(/\A\s*(-*\d+)\s*\.\.\s*(-*\d+)\s*\z/) Range.new(matchdata[1].to_i, matchdata[2].to_i) end |
#cast_to_string(value) ⇒ Object
133 134 135 |
# File 'lib/ruby-xml-mapper/basic_containers.rb', line 133 def cast_to_string value value.strip.gsub(/\s*[\r\n]\s*/, "\n").gsub(/\n\s*\n/, "\n") end |
#initialize_from_xml_node(node) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ruby-xml-mapper/basic_containers.rb', line 69 def initialize_from_xml_node node node.each_element do |child| next if xml_child_mappings.has_key?(child.name) self[child.name.to_sym] = if child.attributes["type"] && respond_to?(cast_method = "cast_to_#{child.attributes["type"]}") __send__(cast_method, child.content) else autocast_value(child.content) end end node.each_attr do |attr| next if xml_attr_mappings.has_key?(attr.name) self[attr.name.to_sym] = autocast_value(attr.value) end end |