Class: XPath::Renderer
- Inherits:
-
Object
- Object
- XPath::Renderer
- Defined in:
- lib/xpath/renderer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #anywhere(element_names) ⇒ Object
- #attribute(current, name) ⇒ Object
- #axis(current, name, element_names) ⇒ Object
- #binary_operator(name, left, right) ⇒ Object
- #child(current, element_names) ⇒ Object
- #convert_argument(argument) ⇒ Object
- #css(current, selector) ⇒ Object
- #descendant(current, element_names) ⇒ Object
- #function(name, *arguments) ⇒ Object
-
#initialize(type) ⇒ Renderer
constructor
A new instance of Renderer.
- #is(one, two) ⇒ Object
- #literal(node) ⇒ Object
- #render(node) ⇒ Object
- #string_literal(string) ⇒ Object
- #text(current) ⇒ Object
- #this_node ⇒ Object
- #union(*expressions) ⇒ Object
- #variable(name) ⇒ Object
- #where(on, condition) ⇒ Object
Constructor Details
#initialize(type) ⇒ Renderer
Returns a new instance of Renderer.
9 10 11 |
# File 'lib/xpath/renderer.rb', line 9 def initialize(type) @type = type end |
Class Method Details
.render(node, type) ⇒ Object
5 6 7 |
# File 'lib/xpath/renderer.rb', line 5 def self.render(node, type) new(type).render(node) end |
Instance Method Details
#anywhere(element_names) ⇒ Object
55 56 57 |
# File 'lib/xpath/renderer.rb', line 55 def anywhere(element_names) with_element_conditions('//', element_names) end |
#attribute(current, name) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/xpath/renderer.rb', line 63 def attribute(current, name) if valid_xml_name?(name) "#{current}/@#{name}" else "#{current}/attribute::*[local-name(.) = #{string_literal(name)}]" end end |
#axis(current, name, element_names) ⇒ Object
51 52 53 |
# File 'lib/xpath/renderer.rb', line 51 def axis(current, name, element_names) with_element_conditions("#{current}/#{name}::", element_names) end |
#binary_operator(name, left, right) ⇒ Object
71 72 73 |
# File 'lib/xpath/renderer.rb', line 71 def binary_operator(name, left, right) "(#{left} #{name} #{right})" end |
#child(current, element_names) ⇒ Object
47 48 49 |
# File 'lib/xpath/renderer.rb', line 47 def child(current, element_names) with_element_conditions("#{current}/", element_names) end |
#convert_argument(argument) ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/xpath/renderer.rb', line 18 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
95 96 97 98 99 100 |
# File 'lib/xpath/renderer.rb', line 95 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
43 44 45 |
# File 'lib/xpath/renderer.rb', line 43 def descendant(current, element_names) with_element_conditions("#{current}//", element_names) end |
#function(name, *arguments) ⇒ Object
106 107 108 |
# File 'lib/xpath/renderer.rb', line 106 def function(name, *arguments) "#{name}(#{arguments.join(', ')})" end |
#is(one, two) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/xpath/renderer.rb', line 75 def is(one, two) if @type == :exact binary_operator('=', one, two) else function(:contains, one, two) end end |
#literal(node) ⇒ Object
91 92 93 |
# File 'lib/xpath/renderer.rb', line 91 def literal(node) node end |
#render(node) ⇒ Object
13 14 15 16 |
# File 'lib/xpath/renderer.rb', line 13 def render(node) arguments = node.arguments.map { |argument| convert_argument(argument) } send(node.expression, *arguments) end |
#string_literal(string) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/xpath/renderer.rb', line 28 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
87 88 89 |
# File 'lib/xpath/renderer.rb', line 87 def text(current) "#{current}/text()" end |
#this_node ⇒ Object
39 40 41 |
# File 'lib/xpath/renderer.rb', line 39 def this_node '.' end |
#union(*expressions) ⇒ Object
102 103 104 |
# File 'lib/xpath/renderer.rb', line 102 def union(*expressions) expressions.join(' | ') end |
#variable(name) ⇒ Object
83 84 85 |
# File 'lib/xpath/renderer.rb', line 83 def variable(name) "%{#{name}}" end |
#where(on, condition) ⇒ Object
59 60 61 |
# File 'lib/xpath/renderer.rb', line 59 def where(on, condition) "#{on}[#{condition}]" end |