class Flexparser::XPaths

A class to represent a collection of paths. This class is supposed to be more convenient for handeling a collection of xpaths.

Attributes

tags[RW]

Public Class Methods

new(tags) click to toggle source

@param tags [Array<String>] a list of names for xpaths

that the xpaths instance will handle.
# File lib/flexparser/xpaths.rb, line 16
def initialize(tags)
  @tags = Array(tags).map(&:to_sym).flatten
  raise ArgumentError, arg_err_msg if @tags.empty?
end

Public Instance Methods

method_name() click to toggle source

The name of an accesor, based on the collection of tags.

# File lib/flexparser/xpaths.rb, line 24
def method_name
  tags.first.to_s.sub(/^@/, '').gsub(/([[:punct:]]|-)+/, '_')
end
valid_paths(doc) click to toggle source

Returns the valid paths from this collection, based on the given doc. @param doc [Flexparser::Fragment] the fragment

that carries the namespaces.

@return [Array<String>] the xpaths that can be applied to this fragment

# File lib/flexparser/xpaths.rb, line 49
def valid_paths(doc)
  xpaths.reject do |path|
    namespaced?(path) && !namespace_available?(path, doc)
  end
end
xpaths() click to toggle source

Builds xpaths from the given tags

# File lib/flexparser/xpaths.rb, line 31
def xpaths
  @xpaths ||= begin
                paths = tags.map do |t|
                  XPath.current.descendant(t)
                end
                if Flexparser.configuration.retry_without_namespaces
                  paths += ns_ignortant_xpaths
                end
                paths
              end
end

Private Instance Methods

arg_err_msg() click to toggle source

no-doc

# File lib/flexparser/xpaths.rb, line 87
def arg_err_msg
  'There needs to be at least one path for a path-collection to be valid'
end
namespace_available?(path, doc) click to toggle source

Checks whether or not a path is can be applied to a given fragment. @param path [String] the path that is to be checked @param doc [Flexparser::Fragment] the fragment that carries the

namespaces.

@return [Boolean] true if the path can be applied, false otherwise.

# File lib/flexparser/xpaths.rb, line 81
def namespace_available?(path, doc)
  nms = doc.propagating_namespaces.keys.map { |ns| ns.gsub('xmlns:', '') }
  path.to_s.match Regexp.union(nms)
end
namespaced?(tag) click to toggle source

Checks whether a tag has a specified namespace. @param tag [String] The tag to be looked at

# File lib/flexparser/xpaths.rb, line 70
def namespaced?(tag)
  !(tag.to_s =~ /\w+:\w+/).nil?
end
ns_ignortant_xpaths() click to toggle source

Returns xpaths that ignore namespaces.

# File lib/flexparser/xpaths.rb, line 60
def ns_ignortant_xpaths
  tags.reject(&method(:namespaced?)).flat_map do |tag|
    XPath.current.descendant[XPath.name.equals(tag.to_s)]
  end
end