Class: HappyMapper::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/happymapper/item.rb

Direct Known Subclasses

Attribute, Element

Constant Summary collapse

Types =
[String, Float, Time, Date, DateTime, Integer, Boolean]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, o = {}) ⇒ Item

options:

:deep   =>  Boolean False to only parse element's children, True to include
            grandchildren and all others down the chain (// in expath)
:single =>  Boolean False if object should be collection, True for single object


12
13
14
15
16
# File 'lib/happymapper/item.rb', line 12

def initialize(name, type, o={})
  self.name, self.type, self.tag = name, type, o.delete(:tag) || name.to_s
  self.options = {:single => false, :deep => false}.merge(o)
  @xml_type = self.class.to_s.split('::').last.downcase
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/happymapper/item.rb', line 4

def name
  @name
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/happymapper/item.rb', line 3

def options
  @options
end

#tagObject

Returns the value of attribute tag.



3
4
5
# File 'lib/happymapper/item.rb', line 3

def tag
  @tag
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/happymapper/item.rb', line 3

def type
  @type
end

Instance Method Details

#attribute?Boolean

Returns:



41
42
43
# File 'lib/happymapper/item.rb', line 41

def attribute?
  !element?
end

#element?Boolean

Returns:



37
38
39
# File 'lib/happymapper/item.rb', line 37

def element?
  @xml_type == 'element'
end

#from_xml_node(node, namespace = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/happymapper/item.rb', line 22

def from_xml_node(node, namespace = nil)
  if primitive?
    value_from_xml_node(node, namespace) do |value_before_type_cast|
      typecast(value_before_type_cast)
    end
  else
    use_default_namespace = !namespace.nil?
    type.parse(node, options.merge(:use_default_namespace => use_default_namespace))
  end
end

#primitive?Boolean

Returns:



33
34
35
# File 'lib/happymapper/item.rb', line 33

def primitive?
  Types.include?(type)
end

#typecast(value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/happymapper/item.rb', line 45

def typecast(value)
  return value if value.kind_of?(type) || value.nil?
  begin        
    if    type == String    then value.to_s
    elsif type == Float     then value.to_f
    elsif type == Time      then Time.parse(value.to_s)
    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'].include?(value.to_s.downcase)
    elsif type == Integer
      # ganked from datamapper
      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