Class: Parse::Operation
- Inherits:
-
Object
- Object
- Parse::Operation
- Defined in:
- lib/parse/query/operation.rb
Overview
An operation is the core part of Constraint when performing queries. It contains an operand (the Parse field) and an operator (the Parse operation). These combined with a value, provide you with a constraint.
All operation registrations add methods to the Symbol class.
Constant Summary collapse
- BLOCKED_FIELD_OPERATORS =
MongoDB operators that are blocked in field names to prevent injection.
%w[$where $function $accumulator $expr].freeze
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#operand ⇒ Symbol
The field in Parse for this operation.
-
#operator ⇒ Symbol
The type of Parse operation.
Class Method Summary collapse
-
.register(op, klass) ⇒ Object
Register a new symbol operator method mapped to a specific Constraint.
Instance Method Summary collapse
-
#constraint(value = nil) ⇒ Parse::Constraint
Create a new constraint based on the handler that had been registered with this operation.
-
#handler ⇒ Parse::Constraint
The constraint class designed to handle this operator.
-
#initialize(field, op) ⇒ Operation
constructor
Create a new operation.
-
#valid? ⇒ Boolean
Whether this operation is defined properly.
Constructor Details
#initialize(field, op) ⇒ Operation
Create a new operation.
54 55 56 57 58 59 |
# File 'lib/parse/query/operation.rb', line 54 def initialize(field, op) self.operand = field.to_sym self.operand = :objectId if operand == :id validate_field_name!(operand) self.operator = op.to_sym end |
Class Attribute Details
.operators ⇒ Object
31 32 33 |
# File 'lib/parse/query/operation.rb', line 31 def operators @operators ||= {} end |
Instance Attribute Details
#operand ⇒ Symbol
The field in Parse for this operation.
19 20 21 |
# File 'lib/parse/query/operation.rb', line 19 def operand @operand end |
#operator ⇒ Symbol
The type of Parse operation.
24 25 26 |
# File 'lib/parse/query/operation.rb', line 24 def operator @operator end |
Class Method Details
.register(op, klass) ⇒ Object
Register a new symbol operator method mapped to a specific Constraint.
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/parse/query/operation.rb', line 91 def self.register(op, klass) Operation.operators[op.to_sym] = klass # Some operator names (e.g. :size) collide with existing Symbol methods. # The override is intentional - the query DSL repurposes these for # constraint building. Remove the prior definition so define_method # does not emit "method redefined" under ruby -W. Symbol.send(:remove_method, op) if Symbol.method_defined?(op, false) Symbol.send :define_method, op do |value = nil| operation = Operation.new self, op value.nil? ? operation : operation.constraint(value) end end |
Instance Method Details
#constraint(value = nil) ⇒ Parse::Constraint
Create a new constraint based on the handler that had been registered with this operation.
86 87 88 |
# File 'lib/parse/query/operation.rb', line 86 def constraint(value = nil) handler.new(self, value) end |
#handler ⇒ Parse::Constraint
Returns the constraint class designed to handle this operator.
43 44 45 |
# File 'lib/parse/query/operation.rb', line 43 def handler Operation.operators[@operator] unless @operator.nil? end |
#valid? ⇒ Boolean
Whether this operation is defined properly.
37 38 39 |
# File 'lib/parse/query/operation.rb', line 37 def valid? !(@operand.nil? || @operator.nil? || handler.nil?) end |