class Gammo::XPath::Traverser

Class for traversing DOM tree built by Gammo::Parser by a given expression. @!visibility private

Public Class Methods

new(expr:, result_type:) click to toggle source

Constructs an instance of Gammo::XPath::Traverser. @param [String] expr @param [Integer] result_type @!visibility private

# File lib/gammo/xpath.rb, line 27
def initialize(expr:, result_type:)
  @expr = expr
  @result_type = result_type
end

Public Instance Methods

evaluate(context) click to toggle source

Evaluates a given expression and returns value according to the result type. @param [Gammo::XPath::Context] context @return [String, Integer, TrueClass, FalseClass, Gammo::XPath::NodeSet] @!visibility private

# File lib/gammo/xpath.rb, line 37
def evaluate(context)
  convert_value context, Parser.new(@expr).parse.evaluate(context)
end

Private Instance Methods

convert_value(context, value) click to toggle source

@!visibility private

# File lib/gammo/xpath.rb, line 44
def convert_value(context, value)
  case @result_type
  when ANY_TYPE then return value.value
  when NUMBER_TYPE then return value.to_number
  when STRING_TYPE then return value.to_s
  when BOOLEAN_TYPE then return value.to_bool
  when UNORDERED_NODE_ITERATOR_TYPE
    fail TypeError, 'the result is not a node set' unless value.node_set?
    value.to_node_set(context)
  when ANY_UNORDERED_NODE_TYPE, FIRST_UNORDERED_NODE_TYPE
    fail TypeError, 'the result is not a node set' unless value.node_set?
    value.to_node_set(context).first
  end
end