class ScopedSearch::QueryLanguage::AST::LogicalOperatorNode
AST class for representing AND or OR constructs. Logical constructs can be simplified resulting in a less complex AST.
Public Instance Methods
compatible_with(node)
click to toggle source
Checks whether another node is comparable so that it can be used for tree simplification. A node can only be simplified if the logical operator is equal.
# File lib/scoped_search/query_language/ast.rb, line 133 def compatible_with(node) node.kind_of?(LogicalOperatorNode) && node.operator == self.operator end
simplify()
click to toggle source
Simplifies nested AND and OR constructs to single constructs with multiple arguments: e.g. (a AND (b AND c)) -> (a AND b AND c)
# File lib/scoped_search/query_language/ast.rb, line 139 def simplify if children.length == 1 # AND or OR constructs do nothing if they only have one operand # So remove the logal operator from the AST by simply using the opeand return children.first.simplify else # nested AND or OR constructs can be combined into one construct @children = children.map { |c| c.simplify }.map { |c| self.compatible_with(c) ? c.children : c }.flatten return self end end