Class: Parse::RelationAction

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/model/core/actions.rb

Overview

A Parse::RelationAction is special operation that adds one object to a relational table as to another. Depending on the polarity of the action, the objects are either added or removed from the relation. This class is used to generate the proper hash request formats Parse needs in order to modify relational information for classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, polarity: true, objects: []) ⇒ RelationAction

Returns a new instance of RelationAction.

Parameters:

  • field (String)

    the name of the Parse field tied to this relation.

  • polarity (Boolean) (defaults to: true)

    whether this is an addition (true) or removal (false) action.

  • objects (Array<Parse::Object>) (defaults to: [])

    the set of objects tied to this relation action.



71
72
73
74
75
# File 'lib/parse/model/core/actions.rb', line 71

def initialize(field, polarity: true, objects: [])
  @key = field.to_s
  self.polarity = polarity
  @objects = Array.wrap(objects).compact
end

Instance Attribute Details

#keyBoolean, ...

Returns:

  • (Boolean)

    whether it is an addition (true) or removal (false) action.

  • (String)

    the name of the Parse field (column).

  • (Array<Parse::Object>)

    the set of objects in this relation action.



66
# File 'lib/parse/model/core/actions.rb', line 66

attr_accessor :polarity, :key, :objects

#objectsBoolean, ...

Returns:

  • (Boolean)

    whether it is an addition (true) or removal (false) action.

  • (String)

    the name of the Parse field (column).

  • (Array<Parse::Object>)

    the set of objects in this relation action.



66
# File 'lib/parse/model/core/actions.rb', line 66

attr_accessor :polarity, :key, :objects

#polarityBoolean, ...

Returns:

  • (Boolean)

    whether it is an addition (true) or removal (false) action.

  • (String)

    the name of the Parse field (column).

  • (Array<Parse::Object>)

    the set of objects in this relation action.



66
67
68
# File 'lib/parse/model/core/actions.rb', line 66

def polarity
  @polarity
end

Instance Method Details

#as_json(*args) ⇒ Hash

Returns a hash representing a relation operation.

Returns:

  • (Hash)

    a hash representing a relation operation.



78
79
80
81
82
83
# File 'lib/parse/model/core/actions.rb', line 78

def as_json(*args)
  { @key => {
    "__op" => (@polarity == true ? ADD : REMOVE),
    "objects" => objects.parse_pointers,
  } }.as_json
end