Class: Parse::Constraint::ObjectIdConstraint

Inherits:
Parse::Constraint show all
Defined in:
lib/parse/query/constraints.rb

Overview

A constraint for matching by a specific objectId value.

# where this Parse object equals the object in the column `field`.
q.where :field => Parse::Pointer("Field", "someObjectId")
# alias, shorthand when we infer `:field` maps to `Field` parse class.
q.where :field.id => "someObjectId"
# "field":{"__type":"Pointer","className":"Field","objectId":"someObjectId"}}

class Artist < Parse::Object
end

class Song < Parse::Object
  belongs_to :artist
end

artist = Artist.first # get any artist
artist_id = artist.id # ex. artist.id

# find all songs for this artist object
Song.all :artist => artist

In some cases, you do not have the Parse object, but you have its ‘objectId`. You can use the objectId in the query as follows:

# shorthand if you are using convention. Will infer class `Artist`
Song.all :artist.id => artist_id

# other approaches, same result
Song.all :artist.id => artist # safely supported Parse::Pointer
Song.all :artist => Artist.pointer(artist_id)
Song.all :artist => Parse::Pointer.new("Artist", artist_id)

Instance Attribute Summary

Attributes inherited from Parse::Constraint

#operand, #operation, #operator, #value

Instance Method Summary collapse

Methods inherited from Parse::Constraint

#as_json, constraint_keyword, create, formatted_value, #formatted_value, #initialize, #key, #precedence, register, #to_s

Constructor Details

This class inherits a constructor from Parse::Constraint

Instance Method Details

#buildHash

Returns the compiled constraint.

Returns:

  • (Hash)

    the compiled constraint.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/parse/query/constraints.rb', line 109

def build
  className = operand.to_parse_class
  value = formatted_value
  # if it is already a pointer value, just return the constraint. Allows for
  # supporting strings, symbols and pointers.
  return { @operation.operand => value } if value.is_a?(Parse::Pointer)

  begin
    klass = className.constantize
  rescue NameError
    klass = Parse::Model.find_class className
  end

  unless klass.present? && klass.is_a?(Parse::Object) == false
    raise ArgumentError, "#{self.class}: No Parse class defined for #{operand} as '#{className}'"
  end

  # allow symbols
  value = value.to_s if value.is_a?(Symbol)

  unless value.is_a?(String) && value.strip.present?
    raise ArgumentError, "#{self.class}: value must be of string type representing a Parse object id."
  end
  value.strip!
  return { @operation.operand => klass.pointer(value) }
end

#idObjectIdConstraint

A registered method on a symbol to create the constraint.

Examples:

q.where :field.id => "someObjectId"
q.where :field.id => pointer # safely supported

Returns:



106
# File 'lib/parse/query/constraints.rb', line 106

register :id