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.

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)



50
51
52
53
54
# File 'lib/parse/query/operation.rb', line 50

def initialize(field, op)
  self.operand = field.to_sym
  self.operand = :objectId if operand == :id
  self.operator = op.to_sym
end

Class Attribute Details

.operatorsHash

to their Constraint subclass.

Returns:

  • (Hash)

    a hash containing all supported Parse operations mapped



29
30
31
# File 'lib/parse/query/operation.rb', line 29

def operators
  @operators
end

Instance Attribute Details

#operandSymbol

The field in Parse for this operation.

Returns:



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

def operand
  @operand
end

#operatorSymbol

The type of Parse operation.

Returns:



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.



70
71
72
73
74
75
76
# File 'lib/parse/query/operation.rb', line 70

def self.register(op, klass)
  Operation.operators[op.to_sym] = klass
  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.

Parameters:

  • value (Object) (defaults to: nil)

    a value to pass to the constraint subclass.

Returns:



65
66
67
# File 'lib/parse/query/operation.rb', line 65

def constraint(value = nil)
  handler.new(self, value)
end

#handlerParse::Constraint

Returns the constraint class designed to handle this operator.

Returns:



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.

Returns:

  • (Boolean)


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

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