Class: Parse::Constraint::SelectionConstraint

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

Overview

Equivalent to the `$select` Parse query operation. This matches a value for a key in the result of a different query.

q.where :field.select => { key: "field", query: query }

# example
value = { key: 'city', query: Artist.where(:fan_count.gt => 50) }
q.where :hometown.select => value

# if the local field is the same name as the foreign table field, you can omit hash
# assumes key: 'city'
q.where :city.select => Artist.where(:fan_count.gt => 50)

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.



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/parse/query/constraints.rb', line 410

def build

  # if it's a hash, then it should be {:key=>"objectId", :query=>[]}
  remote_field_name = @operation.operand
  query = nil
  if @value.is_a?(Hash)
    res = @value.symbolize_keys
    remote_field_name = res[:key] || remote_field_name
    query = res[:query]
    unless query.is_a?(Parse::Query)
      raise ArgumentError, "Invalid Parse::Query object provided in :query field of value: #{@operation.operand}.#{$dontSelect} => #{@value}"
    end
    query = query.compile(encode: false, includeClassName: true)
  elsif @value.is_a?(Parse::Query)
    # if its a query, then assume dontSelect key is the same name as operand.
    query = @value.compile(encode: false, includeClassName: true)
  else
    raise ArgumentError, "Invalid `:select` query constraint. It should follow the format: :field.select => { key: 'key', query: '<Parse::Query>' }"
  end
  { @operation.operand => { :$select => { key: remote_field_name, query: query } } }
end

#selectSelectionConstraint

A registered method on a symbol to create the constraint. Maps to Parse operator “$select”.

Returns:



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

contraint_keyword :$select