Class: Parse::Object

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

Overview

Represents an individual Parse API object.

Direct Known Subclasses

User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, data = nil) ⇒ Object

Returns a new instance of Object.



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

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) Also known as: id

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



138
139
140
141
142
143
144
# File 'lib/parse/object.rb', line 138

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.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/parse/object.rb', line 115

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

#new?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/parse/object.rb', line 61

def new?
  self["objectId"].nil?
end

#parse_deleteObject

Delete the remote Parse API object.



105
106
107
108
109
110
111
112
# File 'lib/parse/object.rb', line 105

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

#pointerObject



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

def pointer
  Parse::Pointer.new self
end

#refreshObject

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



93
94
95
96
97
98
99
100
101
102
# File 'lib/parse/object.rb', line 93

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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/parse/object.rb', line 70

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
  
  if @class_name == Parse::Protocol::CLASS_USER
    self.delete("password")
    self.delete(:username)
    self.delete(:password)
  end
  
  self
end

#uriObject



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

def uri
  Protocol.class_uri @class_name, @parse_object_id
end