Class: RexleXPath

Inherits:
Object
  • Object
show all
Defined in:
lib/rexle-xpath.rb

Instance Method Summary collapse

Constructor Details

#initialize(node = nil) ⇒ RexleXPath

Returns a new instance of RexleXPath.



10
11
12
13
14
# File 'lib/rexle-xpath.rb', line 10

def initialize(node=nil)

  @node = node

end

Instance Method Details

#parse(s) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rexle-xpath.rb', line 16

def parse(s)

  case s
    
  # it's an xpath function
  when /^(\w+)\(\)$/
    @node.method(($1).to_sym).call
  else
    query @node, RexleXPathParser.new(s).to_a
  end
end

#query(node = @node, xpath_instructions) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rexle-xpath.rb', line 28

def query(node=@node, xpath_instructions)

  r = []

  row = xpath_instructions.shift    
  
  method_name, *args = row

  return query node, row if row.first.is_a? Array
  
  result = if method_name.to_sym == :select then

    method(:select).call node, args, xpath_instructions
    
  elsif method_name.to_sym == :text or method_name.to_sym == :value then

    method(:value).call node, args, xpath_instructions
    
  elsif method_name.to_sym == :predicate then

    method(:predicate).call node, args, xpath_instructions      
    
  elsif row.is_a? Array then
    query node, row
  else
    []
  end

  result.is_a?(Array) ? result.flatten : result

end