Class: Parse::Object

Inherits:
Hash
  • Object
show all
Defined in:
lib/parse/object.rb

Overview

Represents an individual Parse API object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, data = nil) ⇒ Object

Returns a new instance of Object.



14
15
16
17
18
19
# File 'lib/parse/object.rb', line 14

def initialize(class_name, data = nil)
  @class_name = class_name
  if data
    parse data
  end
end

Instance Attribute Details

#class_nameObject (readonly)

Returns the value of attribute class_name.



10
11
12
# File 'lib/parse/object.rb', line 10

def class_name
  @class_name
end

#created_atObject (readonly)

Returns the value of attribute created_at.



11
12
13
# File 'lib/parse/object.rb', line 11

def created_at
  @created_at
end

#parse_object_idObject (readonly)

Returns the value of attribute parse_object_id.



9
10
11
# File 'lib/parse/object.rb', line 9

def parse_object_id
  @parse_object_id
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



12
13
14
# File 'lib/parse/object.rb', line 12

def updated_at
  @updated_at
end

Instance Method Details

#decrement(field, amount = 1) ⇒ Object

Decrement the given field by an amount, which defaults to 1. A synonym for increment(field, -amount).



113
114
115
# File 'lib/parse/object.rb', line 113

def decrement(field, amount = 1)
  increment field, -amount
end

#increment(field, amount = 1) ⇒ Object

Increment the given field by an amount, which defaults to 1.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/parse/object.rb', line 94

def increment(field, amount = 1)
  value = (self[field] || 0) + amount
  self[field] = value
  if !@parse_object_id
    # TODO - warn that the object must be stored first
    return nil
  end

  if amount != 0
    op = amount > 0 ? Protocol::OP_INCREMENT : Protocol::OP_DECREMENT
    body = "{\"#{field}\": {\"#{Protocol::KEY_OP}\": \"#{op}\", \"#{Protocol::KEY_AMOUNT}\" : #{amount.abs}}}"
    data = Parse.client.request( self.uri, :put, body)
    parse data
  end
  self
end

#parse_deleteObject

Delete the remote Parse API object.



86
87
88
89
90
91
# File 'lib/parse/object.rb', line 86

def parse_delete
  if @parse_object_id
    response = Parse.client.delete self.uri
  end
  nil
end

#pointerObject



25
26
27
# File 'lib/parse/object.rb', line 25

def pointer
  Parse::Pointer.new self
end

#refreshObject

Update the fields of the local Parse object with the current values from the API.



75
76
77
78
79
80
81
82
83
# File 'lib/parse/object.rb', line 75

def refresh
  if @parse_object_id
    data = Parse.client.get self.uri
    if data
      parse data
    end
  end
  self
end

#saveObject

Write the current state of the local object to the API. If the object has never been saved before, this will create a new object, otherwise it will update the existing stored object.



62
63
64
65
66
67
68
69
70
71
# File 'lib/parse/object.rb', line 62

def save
  method   = @parse_object_id ? :put : :post
  body     = self.to_json

  data = Parse.client.request(self.uri, method, body)
  if data
    parse data
  end
  self
end

#uriObject



21
22
23
# File 'lib/parse/object.rb', line 21

def uri
  Protocol.class_uri @class_name, @parse_object_id
end