class AwesomeXML::NodeEvaluator

Attributes

options[R]
type_class[R]
xml[R]
xpath[R]

Public Class Methods

new(xml, xpath, type_class, options) click to toggle source

Initialize an instance of this class with a Nokogiri::XML object, a string representing an XPath to the value(s) you want to parse, a type class (see ‘AwesomeXML::Type` for more info), and an options hash.

# File lib/awesome_xml/node_evaluator.rb, line 13
def initialize(xml, xpath, type_class, options)
  @xml = xml
  @xpath = xpath
  @type_class = type_class
  @options = options
end

Public Instance Methods

call() click to toggle source

Parses one or several nodes, depending on the ‘options` setting, according to the type passed in in the form of a class that handles the conversion.

# File lib/awesome_xml/node_evaluator.rb, line 22
def call
  if options[:array]
    all_nodes.map { |node| type_class.new(content(node), options).evaluate }
  else
    type_class.new(content(first_node), options).evaluate
  end
end

Private Instance Methods

all_nodes() click to toggle source
# File lib/awesome_xml/node_evaluator.rb, line 37
def all_nodes
  xml_in_context&.xpath(xpath)
end
content(node) click to toggle source
# File lib/awesome_xml/node_evaluator.rb, line 32
def content(node)
  return node unless type_class.parsing_type?
  (options[:element_name] || options[:attribute_name] || options[:self_name]) ? node&.name : node&.text
end
first_node() click to toggle source
# File lib/awesome_xml/node_evaluator.rb, line 41
def first_node
  xml_in_context&.at_xpath(xpath)
end
xml_in_context() click to toggle source
# File lib/awesome_xml/node_evaluator.rb, line 45
def xml_in_context
  options[:local_context] ? xml&.xpath(options[:local_context]) : xml
end