lib/happymapper/item.rb in happymapper-0.2.5 vs lib/happymapper/item.rb in happymapper-0.3.0
- old
+ new
@@ -57,11 +57,11 @@
end
def xpath(namespace = self.namespace)
xpath = ''
xpath += './/' if options[:deep]
- xpath += "#{namespace}:" if namespace
+ xpath += "#{DEFAULT_NS}:" if namespace
xpath += tag
# puts "xpath: #{xpath}"
xpath
end
@@ -126,23 +126,36 @@
type
end
end
def find(node, namespace, &block)
- # this node has a custom namespace (that is present in the doc)
- if self.namespace && node.namespaces.find_by_prefix(self.namespace)
- # from the class definition
- namespace = self.namespace
- elsif options[:namespace] && node.namespaces.find_by_prefix(options[:namespace])
+ if options[:namespace] == false
+ namespace = nil
+ elsif options[:namespace]
# from an element definition
- namespace = options[:namespace]
+ namespace = "#{DEFAULT_NS}:#{options[:namespace]}"
+ elsif self.namespace
+ # this node has a custom namespace (that is present in the doc)
+ namespace = "#{DEFAULT_NS}:#{self.namespace}"
end
-
+
if element?
- result = node.find_first(xpath(namespace))
+ if(options[:single].nil? || options[:single])
+ result = node.find_first(xpath(namespace), namespace)
+ else
+ result = node.find(xpath(namespace))
+ end
# puts "vfxn: #{xpath} #{result.inspect}"
if result
- value = yield(result)
+ if(options[:single].nil? || options[:single])
+ value = yield(result)
+ else
+ value = []
+
+ result.each do |res|
+ value << yield(res)
+ end
+ end
if options[:attributes].is_a?(Hash)
result.attributes.each do |xml_attribute|
if attribute_options = options[:attributes][xml_attribute.name.to_sym]
attribute_value = Attribute.new(xml_attribute.name.to_sym, *attribute_options).from_xml_node(result, namespace)
result.instance_eval <<-EOV
\ No newline at end of file