Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up HappyMapper::ClassMethods#find_nodes_to_parse #242

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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