Skip to content

Commit

Permalink
Merge pull request #242 from mvz/clean-up-find-nodes-to-parse
Browse files Browse the repository at this point in the history
Clean up HappyMapper::ClassMethods#find_nodes_to_parse
  • Loading branch information
mvz authored Nov 10, 2024
2 parents a23596a + c1a0428 commit 37b2f6b
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions lib/happymapper/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,6 @@ def find_nodes_to_parse(options, namespace, tag_name, namespaces, node, root)
# when at the root use the xpath '/' otherwise use a more gready './/'
# unless an xpath has been specified, which should overwrite default
# and finally attach the current namespace if one has been defined
#

xpath = if options[:xpath]
options[:xpath].to_s.sub(%r{([^/])$}, '\1/')
elsif root
Expand All @@ -389,31 +387,14 @@ def find_nodes_to_parse(options, namespace, tag_name, namespaces, node, root)
xpath += "#{namespace}:"
end

nodes = []

# when finding nodes, do it in this order:
# 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.

if options.key?(:tag)
nodes = node.xpath(xpath + options[:tag].to_s, namespaces)
# Find the nodes by the :tag option if given, or by tag_name (derived
# from the class name by default). If the :tag option is given, the
# tag_name will not be used.
if (xpath_ext = options[:tag] || tag_name)
node.xpath(xpath + xpath_ext.to_s, namespaces)
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|
nodes = node.xpath(xpath + xpath_ext.to_s, namespaces)
break if nodes && !nodes.empty?
end

[]
end

nodes
end

def parse_node(node, options, namespace, namespaces)
Expand Down

0 comments on commit 37b2f6b

Please sign in to comment.