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)

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

  • objects (Array<Parse::Object>)

    the set of objects tied to this relation action.


75
76
77
78
79
# File 'lib/parse/model/core/actions.rb', line 75

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.


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

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.


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

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.


70
71
72
# File 'lib/parse/model/core/actions.rb', line 70

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.


82
83
84
85
86
87
88
89
# File 'lib/parse/model/core/actions.rb', line 82

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