Module: Nokogiri::XML::Searchable

Defined in:
lib/ryoba/nokogiri/xml/searchable.rb

Instance Method Summary collapse

Instance Method Details

#ancestor(selector = nil) ⇒ Nokogiri::XML::Element?

Like Searchable#ancestors, but returns only the first matching ancestor.

Examples:

xml = Nokogiri::XML(<<-XML)
  <body>
    <div id="a">
      <div id="b">
        <img src="cat.jpg">
      </div>
    </div>
  </body>
XML

xml.at("img").ancestor("div")  # == Node div#b
xml.at("img").ancestor("#a")   # == Node div#a
xml.at("img").ancestor("#z")   # == nil

Parameters:

  • selector (String) (defaults to: nil)

Returns:

  • (Nokogiri::XML::Element, nil)


104
105
106
# File 'lib/ryoba/nokogiri/xml/searchable.rb', line 104

def ancestor(selector = nil)
  self.ancestors(selector).first
end

#ancestor!(selector = nil) ⇒ Nokogiri::XML::Element

Like Searchable#ancestors!, but returns only the first matching ancestor.

Examples:

xml = Nokogiri::XML(<<-XML)
  <body>
    <div id="a">
      <div id="b">
        <img src="cat.jpg">
      </div>
    </div>
  </body>
XML

xml.at("img").ancestor!("div")  # == Node div#b
xml.at("img").ancestor!("#a")   # == Node div#a
xml.at("img").ancestor!("#z")   # raise error

Parameters:

  • selector (String) (defaults to: nil)

Returns:

  • (Nokogiri::XML::Element)

Raises:



130
131
132
# File 'lib/ryoba/nokogiri/xml/searchable.rb', line 130

def ancestor!(selector = nil)
  self.ancestors!(selector).first
end

#ancestors!(selector = nil) ⇒ Nokogiri::XML::NodeSet

Like Searchable#ancestors, but raises an error if there are no matching ancestors.

Examples:

xml = Nokogiri::XML(<<-XML)
  <body>
    <div id="a">
      <div id="b">
        <img src="cat.jpg">
      </div>
    </div>
  </body>
XML

xml.at("img").ancestors!("div")  # == NodeSet [div#b, div#a]
xml.at("img").ancestors!("#a")   # == NodeSet [div#a]
xml.at("img").ancestors!("#z")   # raise error

Parameters:

  • selector (String) (defaults to: nil)

Returns:

  • (Nokogiri::XML::NodeSet)

Raises:



76
77
78
79
80
81
82
# File 'lib/ryoba/nokogiri/xml/searchable.rb', line 76

def ancestors!(selector = nil)
  results = self.ancestors(selector)
  if results.empty?
    raise Ryoba::Error.new("No ancestors matching #{selector.inspect}")
  end
  results
end

#at!(*queries) ⇒ Nokogiri::XML::Element

Like Searchable#at, but raises an error if there are no results.

Examples:

xml = Nokogiri::XML(<<-XML)
  <body>
    <div id="a" />
    <div id="b" />
  </body>
XML

xml.at!("div")  # == Node div#a
xml.at!("img")  # raise error

Parameters:

  • queries (Array<String>)

Returns:

  • (Nokogiri::XML::Element)

Raises:



46
47
48
49
50
51
52
# File 'lib/ryoba/nokogiri/xml/searchable.rb', line 46

def at!(*queries)
  result = self.at(*queries)
  if result.nil?
    raise Ryoba::Error.new("No elements matching #{queries.map(&:inspect).join(" OR ")}")
  end
  result
end

#search!(*queries) ⇒ Nokogiri::XML::NodeSet

Like Searchable#search, but raises an error if there are no results.

Examples:

xml = Nokogiri::XML(<<-XML)
  <body>
    <div id="a" />
    <div id="b" />
  </body>
XML

xml.search!("div")  # == NodeSet [div#a, div#b]
xml.search!("img")  # raise error

Parameters:

  • queries (Array<String>)

Returns:

  • (Nokogiri::XML::NodeSet)

Raises:



21
22
23
24
25
26
27
# File 'lib/ryoba/nokogiri/xml/searchable.rb', line 21

def search!(*queries)
  results = self.search(*queries)
  if results.empty?
    raise Ryoba::Error.new("No elements matching #{queries.map(&:inspect).join(" OR ")}")
  end
  results
end