class PaloAlto::Renderer

Public Class Methods

new(type) click to toggle source
# File lib/palo_alto/config.rb, line 236
def initialize(type)
        @type = type
end
render(node, type) click to toggle source
# File lib/palo_alto/config.rb, line 232
def self.render(node, type)
        new(type).render(node)
end

Public Instance Methods

anywhere(element_names) click to toggle source
# File lib/palo_alto/config.rb, line 294
def anywhere(element_names)
        with_element_conditions('//', element_names)
end
attribute(current, name) click to toggle source
# File lib/palo_alto/config.rb, line 302
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) click to toggle source
# File lib/palo_alto/config.rb, line 290
def axis(current, name, element_names)
        with_element_conditions("#{current}/#{name}::", element_names)
end
binary_operator(name, left, right) click to toggle source
# File lib/palo_alto/config.rb, line 270
def binary_operator(name, left, right)
        "#{left}#{name}#{right}".gsub('./@', '@')
end
child(current, element_names) click to toggle source
# File lib/palo_alto/config.rb, line 286
def child(current, element_names)
        with_element_conditions("#{current}/", element_names)
end
convert_argument(argument) click to toggle source
# File lib/palo_alto/config.rb, line 245
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) click to toggle source
# File lib/palo_alto/config.rb, line 330
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) click to toggle source
# File lib/palo_alto/config.rb, line 282
def descendant(current, element_names)
        with_element_conditions("#{current}//", element_names)
end
function(name, *arguments) click to toggle source
# File lib/palo_alto/config.rb, line 341
def function(name, *arguments)
        "#{name}(#{arguments.join(', ')})"
end
is(one, two) click to toggle source
# File lib/palo_alto/config.rb, line 310
def is(one, two)
        if @type == :exact
                binary_operator('=', one, two)
        else
                function(:contains, one, two)
        end
end
literal(node) click to toggle source
# File lib/palo_alto/config.rb, line 326
def literal(node)
        node
end
relative(current, element_names) click to toggle source
# File lib/palo_alto/config.rb, line 278
def relative(current, element_names)
        '.'
end
render(node) click to toggle source
# File lib/palo_alto/config.rb, line 240
def render(node)
        arguments = node.arguments.map { |argument| convert_argument(argument) }
        send(node.expression, *arguments)
end
root(current, element_names) click to toggle source
# File lib/palo_alto/config.rb, line 274
def root(current, element_names)
        element_names.any? ? "/#{element_names.join('/')}" : ''
end
string_literal(string) click to toggle source
# File lib/palo_alto/config.rb, line 255
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) click to toggle source
# File lib/palo_alto/config.rb, line 322
def text(current)
        "#{current}/text()"
end
this_node() click to toggle source
# File lib/palo_alto/config.rb, line 266
def this_node
        '.'
end
union(*expressions) click to toggle source
# File lib/palo_alto/config.rb, line 337
def union(*expressions)
        expressions.join(' | ')
end
variable(name) click to toggle source
# File lib/palo_alto/config.rb, line 318
def variable(name)
        "%{#{name}}"
end
where(on, condition) click to toggle source
# File lib/palo_alto/config.rb, line 298
def where(on, condition)
        "#{on}[#{condition}]"
end

Private Instance Methods

valid_xml_name?(name) click to toggle source
# File lib/palo_alto/config.rb, line 357
def valid_xml_name?(name)
        name =~ /^[a-zA-Z_:][a-zA-Z0-9_:\.\-]*$/
end
with_element_conditions(expression, element_names) click to toggle source
# File lib/palo_alto/config.rb, line 347
def with_element_conditions(expression, element_names)
        if element_names.length == 1
                "#{expression}#{element_names.first}"
        elsif element_names.length > 1
                "#{expression}*[#{element_names.map { |e| "self::#{e}" }.join(' | ')}]"
        else
                "#{expression}*"
        end
end