Module: Nokogiri::Decorators::Slop

Defined in:
lib/nokogiri/decorators/slop.rb

Overview

The Slop decorator implements method missing such that a methods may be used instead of XPath or CSS. See Nokogiri.Slop

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

look for node with name. See Nokogiri.Slop



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nokogiri/decorators/slop.rb', line 9

def method_missing name, *args, &block
  prefix = implied_xpath_context

  if args.empty?
    list = xpath("#{prefix}#{name.to_s.sub(/^_/, '')}")
  elsif args.first.is_a? Hash
    hash = args.first
    if hash[:css]
      list = css("#{name}#{hash[:css]}")
    elsif hash[:xpath]
      conds = Array(hash[:xpath]).join(' and ')
      list = xpath("#{prefix}#{name}[#{conds}]")
    end
  else
    CSS::Parser.without_cache do
      list = xpath(
        *CSS.xpath_for("#{name}#{args.first}", :prefix => prefix)
      )
    end
  end

  super if list.empty?
  list.length == 1 ? list.first : list
end