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, through the SymbolMethods module.
Defined Under Namespace
Modules: SymbolMethods
Constant Summary collapse
- BLOCKED_FIELD_OPERATORS =
MongoDB operators that are blocked in field names to prevent injection.
%w[$where $function $accumulator $expr].freeze
- SYMBOL_METHOD_RENAMES =
Operator names that are never installed as
Symbolmethods because they would replace or shadow behavior other code relies on::sizeis Ruby's ownSymbol#size; replacing it brokesort_by(&:size)and any code that measures a symbol.:idmakes every symbolrespond_to?(:id), which ActiveRecord treats as a record.where(status: :draft)then raisedTypeError: can't cast Parse::Operation(issue #83).
Each one is installed under the alternate name given here instead. The original name stays registered in operators, so an explicit
Parse::Operation.new(:tags, :size)still resolves. { id: :pointer_id, size: :array_size }.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.
-
.symbol_conflicts ⇒ Array<Symbol>
Operator names that were not installed on
Symbolbecause another library already defines a method of that name.
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.
55 56 57 58 59 60 |
# File 'lib/parse/query/operation.rb', line 55 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
32 33 34 |
# File 'lib/parse/query/operation.rb', line 32 def operators @operators ||= {} end |
Instance Attribute Details
#operand ⇒ Symbol
The field in Parse for this operation.
20 21 22 |
# File 'lib/parse/query/operation.rb', line 20 def operand @operand end |
#operator ⇒ Symbol
The type of Parse operation.
25 26 27 |
# File 'lib/parse/query/operation.rb', line 25 def operator @operator end |
Class Method Details
.register(op, klass) ⇒ Object
Register a new symbol operator method mapped to a specific Constraint.
125 126 127 128 129 130 131 |
# File 'lib/parse/query/operation.rb', line 125 def self.register(op, klass) op = op.to_sym Operation.operators[op] = klass method_name = SYMBOL_METHOD_RENAMES.fetch(op, op) Operation.operators[method_name] = klass install_symbol_method(method_name) end |
.symbol_conflicts ⇒ Array<Symbol>
Operator names that were not installed on Symbol because another
library already defines a method of that name. Use the explicit
form Parse::Operation.new(:field, :op) => value for these.
117 118 119 |
# File 'lib/parse/query/operation.rb', line 117 def symbol_conflicts @symbol_conflicts ||= [] end |
Instance Method Details
#constraint(value = nil) ⇒ Parse::Constraint
Create a new constraint based on the handler that had been registered with this operation.
87 88 89 |
# File 'lib/parse/query/operation.rb', line 87 def constraint(value = nil) handler.new(self, value) end |
#handler ⇒ Parse::Constraint
Returns the constraint class designed to handle this operator.
44 45 46 |
# File 'lib/parse/query/operation.rb', line 44 def handler Operation.operators[@operator] unless @operator.nil? end |
#valid? ⇒ Boolean
Whether this operation is defined properly.
38 39 40 |
# File 'lib/parse/query/operation.rb', line 38 def valid? !(@operand.nil? || @operator.nil? || handler.nil?) end |