Class: Parse::Constraint
- Inherits:
-
Object
- Object
- Parse::Constraint
- 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
Direct Known Subclasses
CompoundQueryConstraint, ContainedInConstraint, ContainsAllConstraint, ExistsConstraint, FullTextSearchQueryConstraint, GreaterThanConstraint, GreaterThanOrEqualConstraint, InQueryConstraint, LessThanConstraint, LessThanOrEqualConstraint, NearSphereQueryConstraint, NotContainedInConstraint, NotEqualConstraint, NotInQueryConstraint, NullabilityConstraint, ObjectIdConstraint, RegularExpressionConstraint, RejectionConstraint, RelationQueryConstraint, SelectionConstraint, WithinGeoBoxQueryConstraint, WithinPolygonQueryConstraint
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
-
.key ⇒ Symbol
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”.
-
.operand ⇒ Symbol
The operand for this constraint.
-
.precedence(priority = nil) ⇒ Integer
Set the default precedence for this constraint.
Instance Attribute Summary collapse
-
#operand ⇒ Symbol
The operand for the operation.
-
#operation ⇒ Parse::Operation
The value to be applied to this constraint.
-
#operator ⇒ Symbol
The operator for the operation.
-
#value ⇒ Parse::Operation
The value to be applied to this constraint.
Class Method Summary collapse
-
.contraint_keyword(keyword) ⇒ Symbol
Set the keyword for this Constaint.
-
.create(operation, value) ⇒ Object
Creates a new constraint given an operation and value.
-
.formatted_value(value) ⇒ Object
A formatted value based on the data type.
-
.register(op, klass = self) ⇒ Object
Register the given operand for this Parse::Constraint subclass.
Instance Method Summary collapse
-
#as_json(*args) ⇒ Hash
Calls build internally.
-
#build ⇒ Hash
Builds the JSON hash representation of this constraint for a Parse query.
-
#formatted_value ⇒ Object
Formatted value based on the specific data type.
-
#initialize(operation, value) { ... } ⇒ Constraint
constructor
Create a new constraint.
-
#key ⇒ Symbol
The Parse keyword for this constraint.
-
#precedence ⇒ Integer
The precedence of this constraint.
-
#to_s ⇒ String
String representation of this constraint.
Constructor Details
#initialize(operation, value) { ... } ⇒ Constraint
Create a new constraint.
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
.key ⇒ Symbol
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”
57 58 59 |
# File 'lib/parse/query/constraint.rb', line 57 def key @key end |
.operand ⇒ Symbol
Returns 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.
63 64 65 |
# File 'lib/parse/query/constraint.rb', line 63 def precedence @precedence end |
Instance Attribute Details
#operand ⇒ Symbol
Returns the operand for the operation.
140 141 142 |
# File 'lib/parse/query/constraint.rb', line 140 def operand @operation.operand unless @operation.nil? end |
#operation ⇒ Parse::Operation
The value to be applied to this constraint.
|
|
# File 'lib/parse/query/constraint.rb', line 23
|
#operator ⇒ Symbol
Returns the operator for the operation.
150 151 152 |
# File 'lib/parse/query/constraint.rb', line 150 def operator @operation.operator unless @operation.nil? end |
#value ⇒ Parse::Operation
The value to be applied to this constraint.
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.
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.
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
All subclasses should register their operation and themselves.
Register the given operand for this Parse::Constraint subclass.
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
165 166 167 |
# File 'lib/parse/query/constraint.rb', line 165 def as_json(*args) build end |
#build ⇒ Hash
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.
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_value ⇒ Object
Returns 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 |
#key ⇒ Symbol
Returns the Parse keyword for this constraint.
134 135 136 |
# File 'lib/parse/query/constraint.rb', line 134 def key self.class.key end |
#precedence ⇒ Integer
Returns the precedence of this constraint.
129 130 131 |
# File 'lib/parse/query/constraint.rb', line 129 def precedence self.class.precedence end |
#to_s ⇒ String
Returns string representation of this constraint.
181 182 183 |
# File 'lib/parse/query/constraint.rb', line 181 def to_s inspect end |