Class: HappyMapper::Item

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

Direct Known Subclasses

Attribute, Element, TextNode

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 xpath)
:namespace => String Element's namespace if it's not the global or inherited
               default
:parser =>  Symbol Class method to use for type coercion.
:raw    =>  Boolean Use raw node value (inc. tags) when parsing.
:single =>  Boolean False if object should be collection, True for single object
:tag    =>  String Element name if it doesn't match the specified name.


14
15
16
17
18
19
20
21
22
# File 'lib/happymapper/item.rb', line 14

def initialize(name, type, o={})
  self.name = name.to_s
  self.type = type
  #self.tag = o.delete(:tag) || name.to_s
  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

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



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

def namespace
  @namespace
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

#constantObject



24
25
26
# File 'lib/happymapper/item.rb', line 24

def constant
  @constant ||= constantize(type)
end

#from_xml_node(node, namespace, xpath_options) ⇒ Object

Parameters:

  • node (XMLNode)

    the xml node that is being parsed

  • namespace (String)

    the name of the namespace

  • xpath_options (Hash)

    additional xpath options



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/happymapper/item.rb', line 33

def from_xml_node(node, namespace, xpath_options)

  namespace = options[:namespace] if options.key?(:namespace)

  if suported_type_registered?
    find(node, namespace, xpath_options) { |n| process_node_as_supported_type(n) }
  elsif constant == XmlContent
    find(node, namespace, xpath_options) { |n| process_node_as_xml_content(n) }
  elsif custom_parser_defined?
    find(node, namespace, xpath_options) { |n| process_node_with_custom_parser(n) }
  else
    process_node_with_default_parser(node,:namespaces => xpath_options)
  end

end

#method_nameObject



58
59
60
# File 'lib/happymapper/item.rb', line 58

def method_name
  @method_name ||= name.tr('-', '_')
end

#typecast(value) ⇒ String, ...

Convert the value into the correct type.

Parameters:

  • value (String)

    the string value parsed from the XML value that will be converted to the particular primitive type.

Returns:

  • (String, Float, Time, Date, DateTime, Boolean, Integer)

    the converted value to the new type.



71
72
73
# File 'lib/happymapper/item.rb', line 71

def typecast(value)
  typecaster(value).apply(value)
end

#xpath(namespace = self.namespace) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/happymapper/item.rb', line 49

def xpath(namespace = self.namespace)
  xpath  = ''
  xpath += './/' if options[:deep]
  xpath += "#{namespace}:" if namespace
  xpath += tag
  #puts "xpath: #{xpath}"
  xpath
end