module AwesomeXML
This module should be included by every class wishing to pose as an ‘AwesomeXML` node. It gives access to the methods described in this module and also to class methods in the `AwesomeXML::ClassMethods` module.
A collection of class methods that will be available on classes that include ‘AwesomeXML`.
A class that lets you parse a string according to the rules of a specified duration format chunk.
A representation of a user defined duration format.
A class that holds an array of characters and represents a dynamic component of a format string. Has a ‘unit`, a `parse_length` or a `delimiter`. See `AwesomeXML::Duration::Format` for more info.
A class that holds an array of characters and represents a static component of a format string. See ‘AwesomeXML::Duration::Format` for more info.
This module is shared by all native type classes of ‘AwesomeXML`. It defines the interface for types that the `AwesomeXML::NodeEvaluator` expects.
This class is responsible for parsing a specific value from an XML document given all the necessary information.
This class’s responsibility is to build an XPath from specified options.
This class converts types passed in as arguments to ‘.node` method calls to a type class. Either native or user-defined.
A class that knows how to parse a point in time given a timestamp and a format string.
A class that knows how to parse a duration given a duration string and a format string.
A class that knows how to parse a Float
from a String.
A class that knows how to parse an Integer
from a String.
A class that knows how to parse a String from a String. Shockingly, it doesn’t do much.
A class that knows how to not parse but simply pass on a node.
Attributes
Public Class Methods
# File lib/awesome_xml.rb, line 32 def self.included(base) base.class_eval do base.extend(AwesomeXML::ClassMethods) end end
Pass in a string representing valid XML and an options hash to initialize a class that includes ‘AwesomeXML`.
# File lib/awesome_xml.rb, line 43 def initialize(xml = nil, options = {}) @xml = xml @parent_node = options[:parent_node] end
Public Instance Methods
Call this method to the names and parsed values of the non-private nodes of your class in a hash, structured as they were defined. Goes down the rabbit hole and calls ‘evaluate` on child nodes, too.
# File lib/awesome_xml.rb, line 60 def evaluate parse_values Hash[self.class.public_nodes.map { |node| [node, public_send(node)] }] end
This methods runs the parsing operations and assigns the parsed values to the corresponding attribute of each node defined in the class. Returns the class instance.
# File lib/awesome_xml.rb, line 50 def parse @xml = Nokogiri::XML(xml).remove_namespaces! @xml = xml&.xpath(self.class.context) if self.class.context.present? parse_values self end
Private Instance Methods
# File lib/awesome_xml.rb, line 72 def evaluate_nodes(xpath, type_class, options = {}, &block) evaluated_node = AwesomeXML::NodeEvaluator.new(xml, xpath, type_class, options.merge(parent_node: self)).call if block_given? yield(evaluated_node, self) else evaluated_node end end
# File lib/awesome_xml.rb, line 68 def parse_values self.class.nodes.each { |node| public_send("parse_#{node}") } end