Class: PaloAlto::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/palo_alto/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Renderer

Returns a new instance of Renderer.



191
192
193
# File 'lib/palo_alto/config.rb', line 191

def initialize(type)
@type = type
end

Class Method Details

.render(node, type) ⇒ Object



188
189
190
# File 'lib/palo_alto/config.rb', line 188

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

Instance Method Details

#anywhere(element_names) ⇒ Object



241
242
243
# File 'lib/palo_alto/config.rb', line 241

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

#attribute(current, name) ⇒ Object



247
248
249
250
251
252
253
# File 'lib/palo_alto/config.rb', line 247

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



238
239
240
# File 'lib/palo_alto/config.rb', line 238

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

#binary_operator(name, left, right) ⇒ Object



220
221
222
# File 'lib/palo_alto/config.rb', line 220

def binary_operator(name, left, right)
"#{left}#{name}#{right}".gsub('./@', '@')
end

#child(current, element_names) ⇒ Object



235
236
237
# File 'lib/palo_alto/config.rb', line 235

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

#convert_argument(argument) ⇒ Object



198
199
200
201
202
203
204
205
206
# File 'lib/palo_alto/config.rb', line 198

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



270
271
272
273
274
275
# File 'lib/palo_alto/config.rb', line 270

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



232
233
234
# File 'lib/palo_alto/config.rb', line 232

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

#function(name, *arguments) ⇒ Object



279
280
281
# File 'lib/palo_alto/config.rb', line 279

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

#is(one, two) ⇒ Object



254
255
256
257
258
259
260
# File 'lib/palo_alto/config.rb', line 254

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

#literal(node) ⇒ Object



267
268
269
# File 'lib/palo_alto/config.rb', line 267

def literal(node)
node
end

#parenthesis(arg) ⇒ Object



223
224
225
# File 'lib/palo_alto/config.rb', line 223

def parenthesis(arg)
"(#{arg})"
end

#relative(_current, _element_names) ⇒ Object



229
230
231
# File 'lib/palo_alto/config.rb', line 229

def relative(_current, _element_names)
'.'
end

#render(node) ⇒ Object



194
195
196
197
# File 'lib/palo_alto/config.rb', line 194

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

#root(_current, element_names) ⇒ Object



226
227
228
# File 'lib/palo_alto/config.rb', line 226

def root(_current, element_names)
element_names.any? ? "/#{element_names.join('/')}" : ''
end

#string_literal(string) ⇒ Object



207
208
209
210
211
212
213
214
215
216
# File 'lib/palo_alto/config.rb', line 207

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



264
265
266
# File 'lib/palo_alto/config.rb', line 264

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

#this_nodeObject



217
218
219
# File 'lib/palo_alto/config.rb', line 217

def this_node
'.'
end

#union(*expressions) ⇒ Object



276
277
278
# File 'lib/palo_alto/config.rb', line 276

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

#variable(name) ⇒ Object



261
262
263
# File 'lib/palo_alto/config.rb', line 261

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

#where(on, condition) ⇒ Object



244
245
246
# File 'lib/palo_alto/config.rb', line 244

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