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, options = {}) ⇒ 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.


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

def initialize(name, type, options = {})
  self.name = name.to_s
  self.type = type
  # self.tag = options.delete(:tag) || name.to_s
  self.tag = options[:tag] || name.to_s
  self.options = { single: true }.merge(options.merge(name: self.name))

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

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#tagObject

Returns the value of attribute tag.



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

def tag
  @tag
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#constantObject



26
27
28
# File 'lib/happymapper/item.rb', line 26

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

#from_xml_node(node, namespace, xpath_options) ⇒ Object

Parameters:

  • node (Nokogiri::XML::Element)

    the xml node that is being parsed

  • namespace (String)

    the name of the namespace

  • xpath_options (Hash)

    additional xpath options



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

def from_xml_node(node, namespace, xpath_options)
  namespace = options[:namespace] if options.key?(:namespace)

  if custom_parser_defined?
    find(node, namespace, xpath_options) { |n| process_node_with_custom_parser(n) }
  elsif 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) }
  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