Class: HappyMapper::Item
- Inherits:
-
Object
- Object
- HappyMapper::Item
- Defined in:
- lib/happymapper/item.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#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
- #constant ⇒ Object
- #from_xml_node(node, namespace, xpath_options) ⇒ Object
-
#initialize(name, type, o = {}) ⇒ Item
constructor
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.
- #method_name ⇒ Object
-
#typecast(value) ⇒ String, ...
Convert the value into the correct type.
- #xpath(namespace = self.namespace) ⇒ Object
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. = { single: true }.merge(o.merge(name: self.name)) @xml_type = self.class.to_s.split('::').last.downcase end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/happymapper/item.rb', line 3 def name @name end |
#namespace ⇒ Object
Returns the value of attribute namespace.
3 4 5 |
# File 'lib/happymapper/item.rb', line 3 def namespace @namespace end |
#options ⇒ Object
Returns the value of attribute options.
3 4 5 |
# File 'lib/happymapper/item.rb', line 3 def @options end |
#tag ⇒ Object
Returns the value of attribute tag.
3 4 5 |
# File 'lib/happymapper/item.rb', line 3 def tag @tag end |
#type ⇒ Object
Returns the value of attribute type.
3 4 5 |
# File 'lib/happymapper/item.rb', line 3 def type @type end |
Instance Method Details
#constant ⇒ Object
24 25 26 |
# File 'lib/happymapper/item.rb', line 24 def constant @constant ||= constantize(type) end |
#from_xml_node(node, namespace, xpath_options) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/happymapper/item.rb', line 33 def from_xml_node(node, namespace, ) namespace = [:namespace] if .key?(:namespace) if suported_type_registered? find(node, namespace, ) { |n| process_node_as_supported_type(n) } elsif constant == XmlContent find(node, namespace, ) { |n| process_node_as_xml_content(n) } elsif custom_parser_defined? find(node, namespace, ) { |n| process_node_with_custom_parser(n) } else process_node_with_default_parser(node, namespaces: ) end end |
#method_name ⇒ Object
56 57 58 |
# File 'lib/happymapper/item.rb', line 56 def method_name @method_name ||= name.tr('-', '_') end |
#typecast(value) ⇒ String, ...
Convert the value into the correct type.
69 70 71 |
# File 'lib/happymapper/item.rb', line 69 def typecast(value) typecaster(value).apply(value) end |
#xpath(namespace = self.namespace) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/happymapper/item.rb', line 47 def xpath(namespace = self.namespace) xpath = '' xpath += './/' if [:deep] xpath += "#{namespace}:" if namespace xpath += tag # puts "xpath: #{xpath}" xpath end |