Class: Parse::Operation

Inherits:
Object
  • Object
show all
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 Symbol methods because they would replace or shadow behavior other code relies on:

  • :size is Ruby's own Symbol#size; replacing it broke sort_by(&:size) and any code that measures a symbol.
  • :id makes every symbol respond_to?(:id), which ActiveRecord treats as a record. where(status: :draft) then raised TypeError: 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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, op) ⇒ Operation

Create a new operation.

Parameters:

  • field (Symbol) —

    the name of the Parse field

  • op (Symbol) —

    the operator name (ex. :eq, :lt)

Raises:

  • (ArgumentError) —

    if the field name contains a blocked MongoDB operator.



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.

Returns:



20
21
22
# File 'lib/parse/query/operation.rb', line 20

def operand
  @operand
end

#operator ⇒ Symbol

The type of Parse operation.

Returns:



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.

Parameters:

  • op (Symbol) —

    the operator name.

  • klass (Class) —

    the Constraint subclass that handles it.



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.

Returns:



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.

Parameters:

  • value (Object) (defaults to: nil) —

    a value to pass to the constraint subclass.

Returns:



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.

Returns:



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.

Returns:

  • (Boolean)


38
39
40
# File 'lib/parse/query/operation.rb', line 38

def valid?
  !(@operand.nil? || @operator.nil? || handler.nil?)
end