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, FullTextSearchQueryConstraint, 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
# 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:


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

def key
  @key
end

.operandSymbol

Returns the operand for this constraint.

Returns:

  • (Symbol)

    the operand for this constraint.


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

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)

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

def precedence
  @precedence
end

Instance Attribute Details

#operandSymbol

Returns the operand for the operation.

Returns:

  • (Symbol)

    the operand for the operation.


138
139
140
# File 'lib/parse/query/constraint.rb', line 138

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.


148
149
150
# File 'lib/parse/query/constraint.rb', line 148

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:


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

def contraint_keyword(keyword)
  @key = keyword
end

.create(operation, value) ⇒ Object

Creates a new constraint given an operation and value.


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

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.


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

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:


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

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

Instance Method Details

#as_json(*args) ⇒ Hash

Calls build internally

Returns:


163
164
165
# File 'lib/parse/query/constraint.rb', line 163

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.


173
174
175
176
# File 'lib/parse/query/constraint.rb', line 173

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.


184
185
186
# File 'lib/parse/query/constraint.rb', line 184

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.


132
133
134
# File 'lib/parse/query/constraint.rb', line 132

def key
  self.class.key
end

#precedenceInteger

Returns the precedence of this constraint.

Returns:

  • (Integer)

    the precedence of this constraint


127
128
129
# File 'lib/parse/query/constraint.rb', line 127

def precedence
  self.class.precedence
end

#to_sString

Returns string representation of this constraint.

Returns:

  • (String)

    string representation of this constraint.


179
180
181
# File 'lib/parse/query/constraint.rb', line 179

def to_s
  inspect
end