Class: Query::Node::Query
- Inherits:
-
Query::Node
- Object
- Query::Node
- Query::Node::Query
- Defined in:
- lib/query/node/query.rb
Constant Summary collapse
- LOGIC_OPERATORS =
{ "or" => "or", "Or" => "or", "OR" => "or", "||" => "or", "|" => "or", "and" => "and", "And" => "and", "AND" => "and", "&&" => "and", "&" => "and", "" => "and", nil => "and" }.freeze
- NEGATIVE_OPERATORS =
{ "not" => "not", "Not" => "not", "NOT" => "not", "!" => "not" }.freeze
Instance Attribute Summary collapse
-
#left ⇒ Object
Returns the value of attribute left.
-
#negative ⇒ Object
Returns the value of attribute negative.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#query ⇒ Object
Returns the value of attribute query.
-
#right ⇒ Object
Returns the value of attribute right.
Instance Method Summary collapse
- #evaluate(**args) ⇒ Object
-
#initialize(parsed) ⇒ Query
constructor
A new instance of Query.
Constructor Details
#initialize(parsed) ⇒ Query
Returns a new instance of Query.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/query/node/query.rb', line 30 def initialize(parsed) self.negative = (parsed.presence || {}).delete(:not) if parsed.is_a?(Hash) if parsed.key?(:key_value) self.query = KeyValue.new(parsed.delete(:key_value)) elsif parsed.key?(:string) self.query = String.new(parsed.delete(:string)) else self.left = Query.new(parsed.delete(:left)) self.operator = parsed.delete(:operator) self.right = Query.new(parsed.delete(:right)) end else self.query = String.new(parsed) end end |
Instance Attribute Details
#left ⇒ Object
Returns the value of attribute left.
6 7 8 |
# File 'lib/query/node/query.rb', line 6 def left @left end |
#negative ⇒ Object
Returns the value of attribute negative.
6 7 8 |
# File 'lib/query/node/query.rb', line 6 def negative @negative end |
#operator ⇒ Object
Returns the value of attribute operator.
6 7 8 |
# File 'lib/query/node/query.rb', line 6 def operator @operator end |
#query ⇒ Object
Returns the value of attribute query.
6 7 8 |
# File 'lib/query/node/query.rb', line 6 def query @query end |
#right ⇒ Object
Returns the value of attribute right.
6 7 8 |
# File 'lib/query/node/query.rb', line 6 def right @right end |
Instance Method Details
#evaluate(**args) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/query/node/query.rb', line 48 def evaluate(**args) if negative && query { operator: NEGATIVE_OPERATORS.fetch(negative), right: query.evaluate(**args) } elsif negative { operator: NEGATIVE_OPERATORS.fetch(negative), right: { left: left.evaluate(**args), operator: LOGIC_OPERATORS.fetch(operator), right: right.evaluate(**args) } } elsif query query.evaluate(**args) else { left: left.evaluate(**args), operator: LOGIC_OPERATORS.fetch(operator), right: right.evaluate(**args) } end end |