class Gammo::XPath::AST::BoolExpr

Class for representing a binary expression that returns a boolean. @!visibility private

Public Instance Methods

evaluate(context) click to toggle source

@!visibility private

# File lib/gammo/xpath/ast/expression.rb, line 36
def evaluate(context)
  compare(context, *evaluate_values(context))
end

Private Instance Methods

compare(context, left, right) click to toggle source

Compares both values and returns a boolean. @param [Context] context @param [Value] left @param [Value] right @return [TrueClass, FalseClass] @!visibility private

# File lib/gammo/xpath/ast/expression.rb, line 48
def compare(context, left, right)
  return compare_with_node_set(
    context, left.to_node_set(context), right) if left.node_set?
  return compare_with_node_set(
    context, right.to_node_set(context), left, reverse: true) if right.node_set?
  do_compare(left, right)
end
compare_with_node_set(context, node_set, value, reverse: false) click to toggle source

@!visibility private

# File lib/gammo/xpath/ast/expression.rb, line 57
def compare_with_node_set(context, node_set, value, reverse: false)
  if value.node_set?
    node_set.each do |lnode|
      ls = string_from_node(lnode)
      value.to_node_set(context).each do |rnode|
        return true if compare(context, ls, string_from_node(rnode))
      end
    end
  end
  if value.number?
    node_set.each do |node|
      n = number_from_node(node)
      return true if compare(context, *(reverse ? [value, n] : [n, value]))
    end
    return false
  end
  if value.string?
    node_set.each do |node|
      s = string_from_node(node)
      return true if compare(context, *(reverse ? [value, s] : [s, value]))
    end
    return false
  end
  if value.bool?
    b = node_set.to_bool
    return compare(context, *(reverse ? [value, b] : [b, value]))
  end
  fail UnreachableError, 'unreachable pattern happens; please file an issue on github.'
end
equal?(left, right) click to toggle source

@!visibility private

# File lib/gammo/xpath/ast/expression.rb, line 111
def equal?(left, right)
  return left.to_bool == right.to_bool if left.bool? || right.bool?
  return left.to_number == right.to_number if left.number? || right.number?
  left.to_s == right.to_s
end
number_from_node(node) click to toggle source

@!visibility private

# File lib/gammo/xpath/ast/expression.rb, line 100
def number_from_node(node)
  case node
  when Gammo::Attribute
    # TODO: Consider float case.
    AST::Value::Number.new(node.value.to_i)
  when Gammo::Node::Comment, Gammo::Node::Text
    AST::Value::Number.new(node.data)
  end
end
string_from_node(node) click to toggle source

@!visibility private

# File lib/gammo/xpath/ast/expression.rb, line 88
def string_from_node(node)
  case node
  when Gammo::Node::Element, Gammo::Node::Document
    AST::Value::String.new(node.inner_text)
  when Gammo::Attribute
    AST::Value::String.new(node.value)
  when Gammo::Node::Comment, Gammo::Node::Text
    AST::Value::String.new(node.data)
  end
end