Class: ExistDB::Dom::Mapper::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/existdb/dom/mapper.rb

Direct Known Subclasses

Attribute, Element

Instance Attribute Summary collapse

Instance Method Summary collapse

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.options = { :single => true }.merge(o.merge(:name => self.name))

    @xml_type = self.class.to_s.split('::').last.downcase
end

Instance Attribute Details

#method_nameObject

Returns the value of attribute method_name.



234
235
236
# File 'lib/existdb/dom/mapper.rb', line 234

def method_name
  @method_name
end

#nameObject

Returns the value of attribute name.



234
235
236
# File 'lib/existdb/dom/mapper.rb', line 234

def name
  @name
end

#optionsObject

Returns the value of attribute options.



234
235
236
# File 'lib/existdb/dom/mapper.rb', line 234

def options
  @options
end

#tagObject

Returns the value of attribute tag.



234
235
236
# File 'lib/existdb/dom/mapper.rb', line 234

def tag
  @tag
end

#typeObject

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