Class: ExistDB::Dom::Mapper::Item
- Inherits:
-
Object
- Object
- ExistDB::Dom::Mapper::Item
- Defined in:
- lib/existdb/dom/mapper.rb
Instance Attribute Summary collapse
-
#method_name ⇒ Object
Returns the value of attribute method_name.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#tag ⇒ Object
Returns the value of attribute tag.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(name, type, o = {}) ⇒ Item
constructor
A new instance of Item.
- #type_cast(value, type = self.type) ⇒ Object
Constructor Details
#initialize(name, type, o = {}) ⇒ Item
Returns a new instance of Item.
235 236 237 238 239 240 241 242 243 |
# File 'lib/existdb/dom/mapper.rb', line 235 def initialize(name, type, o={}) self.name = name.to_s self.method_name = self.name.tr('-','_') self.type = type self.tag = o[:tag] || name.to_s self. = { :single => true }.merge(o.merge(:name => self.name)) @xml_type = self.class.to_s.split('::').last.downcase end |
Instance Attribute Details
#method_name ⇒ Object
Returns the value of attribute method_name.
234 235 236 |
# File 'lib/existdb/dom/mapper.rb', line 234 def method_name @method_name end |
#name ⇒ Object
Returns the value of attribute name.
234 235 236 |
# File 'lib/existdb/dom/mapper.rb', line 234 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
234 235 236 |
# File 'lib/existdb/dom/mapper.rb', line 234 def @options end |
#tag ⇒ Object
Returns the value of attribute tag.
234 235 236 |
# File 'lib/existdb/dom/mapper.rb', line 234 def tag @tag end |
#type ⇒ Object
Returns the value of attribute type.
234 235 236 |
# File 'lib/existdb/dom/mapper.rb', line 234 def type @type end |
Instance Method Details
#type_cast(value, type = self.type) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/existdb/dom/mapper.rb', line 245 def type_cast(value, type = self.type) begin if type == String then value.to_s elsif type == Float then value.to_f elsif type == Time then Time.parse(value.to_s) rescue Time.at(value.to_i) elsif type == Date then Date.parse(value.to_s) elsif type == DateTime then DateTime.parse(value.to_s) elsif type == Boolean then ['true','t','1','y','yes'].include?(value.to_s.downcase) elsif type == Integer then # ganked from datamapper, and happymapper value_to_i = value.to_i if value_to_i == 0 && value != '0' value_to_s = value.to_s begin Integer(value_to_s =~ /^(\d+)/ ? $1 : value_to_s) rescue ArgumentError nil end else value_to_i end else value end rescue value end end |