class Flexparser::Fragment

Wraps Nokogiri::Xml::Document to automatically hand down namespaces to fragments generated by xpath

Attributes

doc[R]
namespaces[W]

Public Class Methods

new(str, namespaces: {}) click to toggle source
# File lib/flexparser/fragment.rb, line 15
def initialize(str, namespaces: {})
  @doc = str.is_a?(String) ? Nokogiri::XML(str) : str
  @propagated_namespaces = namespaces
end

Public Instance Methods

pns()
propagating_namespaces() click to toggle source

The namespaces that are propagating to the child-fragment of this.

# File lib/flexparser/fragment.rb, line 37
def propagating_namespaces
  return @propagated_namespaces unless doc.respond_to?(:namespaces)
  if doc.respond_to?(:collect_namespaces)
    return @propagated_namespaces.merge doc.collect_namespaces
  end
  @propagated_namespaces.merge doc.namespaces
end
Also aliased as: pns
to_s() click to toggle source
# File lib/flexparser/fragment.rb, line 20
def to_s
  "#{self.class}: \n\tNamespaces: #{_namespaces}
  \tRaw:\n\t\t#{@raw_doc}\tNokogiri:\n#{doc}"
end
xpath(path) click to toggle source

The same as Nokogiri::Xml::Document#xpath but namespaces are passed down to resulting fragments.

# File lib/flexparser/fragment.rb, line 29
def xpath(path)
  FragmentBuilder
    .build(doc.xpath(path, propagating_namespaces))
end