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 133 def compatible_with(node) 134 node.kind_of?(LogicalOperatorNode) && node.operator == self.operator 135 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 139 def simplify 140 if children.length == 1 141 # AND or OR constructs do nothing if they only have one operand 142 # So remove the logal operator from the AST by simply using the opeand 143 return children.first.simplify 144 else 145 # nested AND or OR constructs can be combined into one construct 146 @children = children.map { |c| c.simplify }.map { |c| self.compatible_with(c) ? c.children : c }.flatten 147 return self 148 end 149 end