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 | # 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”
56 57 58 | # File 'lib/parse/query/constraint.rb', line 56 def key @key end |
.operand ⇒ Symbol
Returns 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.
62 63 64 | # File 'lib/parse/query/constraint.rb', line 62 def precedence @precedence end |
Instance Attribute Details
#operand ⇒ Symbol
Returns the operand for the operation.
138 139 140 | # File 'lib/parse/query/constraint.rb', line 138 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.
148 149 150 | # File 'lib/parse/query/constraint.rb', line 148 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.
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.
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
All subclasses should register their operation and themselves.
Register the given operand for this Parse::Constraint subclass.
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
163 164 165 | # File 'lib/parse/query/constraint.rb', line 163 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.
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_value ⇒ Object
Returns 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 |
#key ⇒ Symbol
Returns the Parse keyword for this constraint.
132 133 134 | # File 'lib/parse/query/constraint.rb', line 132 def key self.class.key end |
#precedence ⇒ Integer
Returns the precedence of this constraint.
127 128 129 | # File 'lib/parse/query/constraint.rb', line 127 def precedence self.class.precedence end |
#to_s ⇒ String
Returns string representation of this constraint.
179 180 181 | # File 'lib/parse/query/constraint.rb', line 179 def to_s inspect end |