Class: Parse::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/query/constraint.rb,
lib/parse/query/constraints.rb

Overview

Constraints are the heart of the Parse::Query system. Each constraint is made up of an Operation and a value (the right side of an operator). Constraints are responsible for making their specific Parse hash format required when sending Queries to Parse. All constraints can be combined by merging different constraints (since they are multiple hashes) and some constraints may have higher precedence than others (ex. equality is higher precedence than an “in” query).

All constraints should inherit from Parse::Constraint and should register their specific Operation method (ex. :eq or :lte) For more information about the query design pattern from DataMapper that inspired this, see datamapper.org/docs/find.html

Defined Under Namespace

Classes: CompoundQueryConstraint, ContainedInConstraint, ContainsAllConstraint, ExistsConstraint, GreaterThanConstraint, GreaterThanOrEqualConstraint, InQueryConstraint, LessThanConstraint, LessThanOrEqualConstraint, NearSphereQueryConstraint, NotContainedInConstraint, NotEqualConstraint, NotInQueryConstraint, NullabilityConstraint, ObjectIdConstraint, RegularExpressionConstraint, RejectionConstraint, RelationQueryConstraint, SelectionConstraint, WithinGeoBoxQueryConstraint, WithinPolygonQueryConstraint

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operation, value) { ... } ⇒ Constraint

Create a new constraint.

Parameters:

  • operation (Parse::Operation)

    the operation for this constraint.

  • value (Object)

    the value to attach to this constraint.

Yields:

  • You may also pass a block to modify the operation or value.


37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/parse/query/constraint.rb', line 37

def initialize(operation, value)
  # if the first parameter is not an Operation, but it is a symbol
  # it most likely is just the field name, so let's assume they want
  # the default equality operation.
  if operation.is_a?(Operation) == false && operation.respond_to?(:to_sym)
      operation = Operation.new(operation.to_sym, self.class.operand)
  end
  @operation = operation
  @value = value
  yield(self) if block_given?

end

Class Attribute Details

.keySymbol

The class attributes keep track of the Parse key (special Parse text symbol representing this operation. Ex. local method could be called .ex, where the Parse Query operation that should be sent out is “$exists”) in this case, key should be set to “$exists”

Returns:


57
58
59
# File 'lib/parse/query/constraint.rb', line 57

def key
  @key
end

.operandSymbol

Returns the operand for this constraint.

Returns:

  • (Symbol)

    the operand for this constraint.


67
68
69
# File 'lib/parse/query/constraint.rb', line 67

def operand
  @operand
end

.precedence(priority = nil) ⇒ Integer

Set the default precedence for this constraint.

Parameters:

  • priority (Integer) (defaults to: nil)

    a higher priority has higher precedence

Returns:

  • (Integer)

63
64
65
# File 'lib/parse/query/constraint.rb', line 63

def precedence
  @precedence
end

Instance Attribute Details

#operandSymbol

Returns the operand for the operation.

Returns:

  • (Symbol)

    the operand for the operation.


140
141
142
# File 'lib/parse/query/constraint.rb', line 140

def operand
  @operation.operand unless @operation.nil?
end

#operationParse::Operation

The value to be applied to this constraint.

Returns:

# File 'lib/parse/query/constraint.rb', line 23

#operatorSymbol

Returns the operator for the operation.

Returns:

  • (Symbol)

    the operator for the operation.


150
151
152
# File 'lib/parse/query/constraint.rb', line 150

def operator
  @operation.operator unless @operation.nil?
end

#valueParse::Operation

The value to be applied to this constraint.

Returns:


31
# File 'lib/parse/query/constraint.rb', line 31

attr_accessor :operation, :value

Class Method Details

.contraint_keyword(keyword) ⇒ Symbol

Set the keyword for this Constaint. Subclasses should use this method.

Parameters:

Returns:


81
82
83
# File 'lib/parse/query/constraint.rb', line 81

def contraint_keyword(keyword)
  @key = keyword
end

.create(operation, value) ⇒ Object

Creates a new constraint given an operation and value.


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

def create(operation, value)
  #default to a generic equality constraint if not passed an operation
  unless operation.is_a?(Parse::Operation) && operation.valid?
    return self.new(operation, value)
  end
  operation.constraint(value)
end

.formatted_value(value) ⇒ Object

Returns a formatted value based on the data type.

Returns:

  • (Object)

    a formatted value based on the data type.


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/parse/query/constraint.rb', line 105

def formatted_value(value)
  d = value
  d = { __type: Parse::Model::TYPE_DATE, iso: d.utc.iso8601(3) } if d.respond_to?(:utc)
  # if it responds to parse_date (most likely a time/date object), then call the conversion
  d = d.parse_date if d.respond_to?(:parse_date)
  # if it's not a Parse::Date, but still responds to iso8601, then do it manually
  if d.is_a?(Parse::Date) == false && d.respond_to?(:iso8601)
    d = { __type: Parse::Model::TYPE_DATE, iso: d.iso8601(3) }
  end
  d = d.pointer if d.respond_to?(:pointer) #simplified query object
  d = d.to_s if d.is_a?(Regexp)
  # d = d.pointer if d.is_a?(Parse::Object) #simplified query object
  #  d = d.compile
  if d.is_a?(Parse::Query)
    compiled = d.compile(encode: false, includeClassName: true)
    # compiled["className"] = d.table
    d = compiled
  end
  d
end

.register(op, klass = self) ⇒ Object

Note:

All subclasses should register their operation and themselves.

Register the given operand for this Parse::Constraint subclass.

Parameters:


99
100
101
102
# File 'lib/parse/query/constraint.rb', line 99

def register(op, klass = self)
  self.operand ||= op
  Operation.register op, klass
end

Instance Method Details

#as_json(*args) ⇒ Hash

Calls build internally

Returns:


165
166
167
# File 'lib/parse/query/constraint.rb', line 165

def as_json(*args)
  build
end

#buildHash

Builds the JSON hash representation of this constraint for a Parse query. This method should be overriden by subclasses. The default implementation implements buildling the equality constraint.

Returns:

Raises:

  • ArgumentError if the constraint could be be build due to a bad parameter. This will be different depending on the constraint subclass.


175
176
177
178
# File 'lib/parse/query/constraint.rb', line 175

def build
  return { @operation.operand => formatted_value } if @operation.operator == :eq || key.nil?
  { @operation.operand => { key => formatted_value } }
end

#formatted_valueObject

Returns formatted value based on the specific data type.

Returns:

  • (Object)

    formatted value based on the specific data type.


186
187
188
# File 'lib/parse/query/constraint.rb', line 186

def formatted_value
  self.class.formatted_value(@value)
end

#keySymbol

Returns the Parse keyword for this constraint.

Returns:

  • (Symbol)

    the Parse keyword for this constraint.


134
135
136
# File 'lib/parse/query/constraint.rb', line 134

def key
  self.class.key
end

#precedenceInteger

Returns the precedence of this constraint

Returns:

  • (Integer)

    the precedence of this constraint


129
130
131
# File 'lib/parse/query/constraint.rb', line 129

def precedence
  self.class.precedence
end

#to_sString

Returns string representation of this constraint.

Returns:

  • (String)

    string representation of this constraint.


181
182
183
# File 'lib/parse/query/constraint.rb', line 181

def to_s
  inspect
end