Class: XPath::Renderer

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Renderer

Returns a new instance of Renderer.



7
8
9
# File 'lib/xpath/renderer.rb', line 7

def initialize(type)
  @type = type
end

Class Method Details

.render(node, type) ⇒ Object



3
4
5
# File 'lib/xpath/renderer.rb', line 3

def self.render(node, type)
  new(type).render(node)
end

Instance Method Details

#anywhere(element_names) ⇒ Object



53
54
55
# File 'lib/xpath/renderer.rb', line 53

def anywhere(element_names)
  with_element_conditions("//", element_names)
end

#attribute(current, name) ⇒ Object



61
62
63
# File 'lib/xpath/renderer.rb', line 61

def attribute(current, name)
  "#{current}/@#{name}"
end

#axis(current, name, element_names) ⇒ Object



49
50
51
# File 'lib/xpath/renderer.rb', line 49

def axis(current, name, element_names)
  with_element_conditions("#{current}/#{name}::", element_names)
end

#binary_operator(name, left, right) ⇒ Object



65
66
67
# File 'lib/xpath/renderer.rb', line 65

def binary_operator(name, left, right)
  "(#{left} #{name} #{right})"
end

#child(current, element_names) ⇒ Object



45
46
47
# File 'lib/xpath/renderer.rb', line 45

def child(current, element_names)
  with_element_conditions("#{current}/", element_names)
end

#convert_argument(argument) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/xpath/renderer.rb', line 16

def convert_argument(argument)
  case argument
    when Expression, Union then render(argument)
    when Array then argument.map { |element| convert_argument(element) }
    when String then string_literal(argument)
    when Literal then argument.value
    else argument.to_s
  end
end

#css(current, selector) ⇒ Object



89
90
91
92
93
94
# File 'lib/xpath/renderer.rb', line 89

def css(current, selector)
  paths = Nokogiri::CSS.xpath_for(selector).map do |xpath_selector|
    "#{current}#{xpath_selector}"
  end
  union(paths)
end

#descendant(current, element_names) ⇒ Object



41
42
43
# File 'lib/xpath/renderer.rb', line 41

def descendant(current, element_names)
  with_element_conditions("#{current}//", element_names)
end

#function(name, *arguments) ⇒ Object



100
101
102
# File 'lib/xpath/renderer.rb', line 100

def function(name, *arguments)
  "#{name}(#{arguments.join(", ")})"
end

#is(one, two) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/xpath/renderer.rb', line 69

def is(one, two)
  if @type == :exact
    binary_operator("=", one, two)
  else
    function(:contains, one, two)
  end
end

#literal(node) ⇒ Object



85
86
87
# File 'lib/xpath/renderer.rb', line 85

def literal(node)
  node
end

#render(node) ⇒ Object



11
12
13
14
# File 'lib/xpath/renderer.rb', line 11

def render(node)
  arguments = node.arguments.map { |argument| convert_argument(argument) }
  send(node.expression, *arguments)
end

#string_literal(string) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/xpath/renderer.rb', line 26

def string_literal(string)
  if string.include?("'")
    string = string.split("'", -1).map do |substr|
      "'#{substr}'"
    end.join(%q{,"'",})
    "concat(#{string})"
  else
    "'#{string}'"
  end
end

#text(current) ⇒ Object



81
82
83
# File 'lib/xpath/renderer.rb', line 81

def text(current)
  "#{current}/text()"
end

#this_nodeObject



37
38
39
# File 'lib/xpath/renderer.rb', line 37

def this_node
  '.'
end

#union(*expressions) ⇒ Object



96
97
98
# File 'lib/xpath/renderer.rb', line 96

def union(*expressions)
  expressions.join(' | ')
end

#variable(name) ⇒ Object



77
78
79
# File 'lib/xpath/renderer.rb', line 77

def variable(name)
  "%{#{name}}"
end

#where(on, condition) ⇒ Object



57
58
59
# File 'lib/xpath/renderer.rb', line 57

def where(on, condition)
  "#{on}[#{condition}]"
end