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, contraint_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.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/parse/query/constraints.rb', line 58

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 => e
    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:


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

register :id