lib/happymapper.rb in nokogiri-happymapper-0.5.7 vs lib/happymapper.rb in nokogiri-happymapper-0.5.8
- old
+ new
@@ -12,19 +12,21 @@
DEFAULT_NS = "happymapper"
def self.included(base)
if !(base.superclass <= HappyMapper)
base.instance_eval do
- @attributes = []
- @elements = []
+ @attributes = {}
+ @elements = {}
@registered_namespaces = {}
@wrapper_anonymous_classes = {}
end
else
base.instance_eval do
- @attributes = superclass.attributes.dup
- @elements = superclass.elements.dup
+ @attributes =
+ superclass.instance_variable_get(:@attributes).dup
+ @elements =
+ superclass.instance_variable_get(:@elements).dup
@registered_namespaces =
superclass.instance_variable_get(:@registered_namespaces).dup
@wrapper_anonymous_classes =
superclass.instance_variable_get(:@wrapper_anonymous_classes).dup
end
@@ -50,22 +52,22 @@
# the object will be converted upon parsing
# @param [Hash] options additional parameters to send to the relationship
#
def attribute(name, type, options={})
attribute = Attribute.new(name, type, options)
- @attributes << attribute
+ @attributes[name] = attribute
attr_accessor attribute.method_name.intern
end
#
# The elements defined through {#attribute}.
#
# @return [Array<Attribute>] a list of the attributes defined for this class;
# an empty array is returned when there have been no attributes defined.
#
def attributes
- @attributes
+ @attributes.values
end
#
# Register a namespace that is used to persist the object namespace back to
# XML.
@@ -105,11 +107,11 @@
# the object will be converted upon parsing
# @param [Hash] options additional parameters to send to the relationship
#
def element(name, type, options={})
element = Element.new(name, type, options)
- @elements << element
+ @elements[name] = element
attr_accessor element.method_name.intern
end
#
# The elements defined through {#element}, {#has_one}, and {#has_many}.
@@ -117,11 +119,11 @@
# @return [Array<Element>] a list of the elements contained defined for this
# class; an empty array is returned when there have been no elements
# defined.
#
def elements
- @elements
+ @elements.values
end
#
# The value stored in the text node of the current element.
#
@@ -336,21 +338,39 @@
xpath += "#{namespace}:" if namespace
nodes = []
# when finding nodes, do it in this order:
- # 1. specified tag
+ # 1. specified tag if one has been provided
# 2. name of element
# 3. tag_name (derived from class name by default)
+ # If a tag has been provided we need to search for it.
- [options[:tag], options[:name], tag_name].compact.each do |xpath_ext|
+ if options.key?(:tag)
begin
- nodes = node.xpath(xpath + xpath_ext.to_s, namespaces)
+ nodes = node.xpath(xpath + options[:tag].to_s, namespaces)
rescue
- break
+ # This exception takes place when the namespace is often not found
+ # and we should continue on with the empty array of nodes.
end
- break if nodes && !nodes.empty?
+ else
+
+ # This is the default case when no tag value is provided.
+ # First we use the name of the element `items` in `has_many items`
+ # Second we use the tag name which is the name of the class cleaned up
+
+ [options[:name], tag_name].compact.each do |xpath_ext|
+ begin
+ nodes = node.xpath(xpath + xpath_ext.to_s, namespaces)
+ rescue
+ break
+ # This exception takes place when the namespace is often not found
+ # and we should continue with the empty array of nodes or keep looking
+ end
+ break if nodes && !nodes.empty?
+ end
+
end
nodes
end