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).



120
121
122
123
124
125
126
# File 'lib/parse/object.rb', line 120

def decrement(field, amount = 1)
  #increment field, -amount
  body = {field => Parse::Decrement.new(amount)}.to_json
  data = Parse.client.request(self.uri, :put, body)
  parse data
  self
end

#increment(field, amount = 1) ⇒ Object

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/parse/object.rb', line 97

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
  body = {field => Parse::Increment.new(amount)}.to_json
  data = Parse.client.request(self.uri, :put, body)
  parse data
  self
end

#parse_deleteObject

Delete the remote Parse API object.



89
90
91
92
93
94
# File 'lib/parse/object.rb', line 89

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.



78
79
80
81
82
83
84
85
86
# File 'lib/parse/object.rb', line 78

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.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/parse/object.rb', line 64

def save
  method   = @parse_object_id ? :put : :post
  Protocol::RESERVED_KEYS.each { |k| self.delete(k) }
  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