Class: Parse::Object

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

Overview

Represents an individual Parse API object.

Direct Known Subclasses

Model, 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
21
# File 'lib/parse/object.rb', line 15

def initialize(class_name, data = nil)
  @class_name = class_name
  @op_fields = {}
  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

#array_add(field, value) ⇒ Object



184
185
186
# File 'lib/parse/object.rb', line 184

def array_add(field, value)
  array_op(field, Protocol::KEY_ADD, value)
end

#array_add_relation(field, value) ⇒ Object



188
189
190
# File 'lib/parse/object.rb', line 188

def array_add_relation(field, value)
  array_op(field, Protocol::KEY_ADD_RELATION, value)
end

#array_add_unique(field, value) ⇒ Object



192
193
194
# File 'lib/parse/object.rb', line 192

def array_add_unique(field, value)
  array_op(field, Protocol::KEY_ADD_UNIQUE, value)
end

#array_remove(field, value) ⇒ Object



196
197
198
# File 'lib/parse/object.rb', line 196

def array_remove(field, value)
  array_op(field, Protocol::KEY_REMOVE, value)
end

#as_json(*a) ⇒ Object



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

def as_json(*a)
  Hash[self.map do |key, value|
    value = if !value.nil?
      value.respond_to?(:as_json) ? value.as_json : value
    else
      Protocol::DELETE_OP
    end

    [key, value]
  end]
end

#decrement(field, amount = 1) ⇒ Object

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



217
218
219
# File 'lib/parse/object.rb', line 217

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

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


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

def eql?(other)
  Parse.object_pointer_equality?(self, other)
end

#getObject

make it easier to deal with the ambiguity of whether you’re passed a pointer or object



42
43
44
# File 'lib/parse/object.rb', line 42

def get
  self
end

#hashObject



29
30
31
# File 'lib/parse/object.rb', line 29

def hash
  Parse.object_pointer_hash(self)
end

#increment(field, amount = 1) ⇒ Object

Increment the given field by an amount, which defaults to 1. Saves immediately to reflect incremented



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/parse/object.rb', line 201

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

  body = {field => Parse::Increment.new(amount)}.to_json
  data = Parse.client.request(self.uri, :put, body)
  parse data
  self
end

#inspectObject



156
157
158
# File 'lib/parse/object.rb', line 156

def inspect
  "#{@class_name}:#{@parse_object_id} #{super}"
end

#new?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/parse/object.rb', line 78

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

#parse_deleteObject

Delete the remote Parse API object.



175
176
177
178
179
180
181
182
# File 'lib/parse/object.rb', line 175

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

  self.clear
  self
end

#pointerObject



37
38
39
# File 'lib/parse/object.rb', line 37

def pointer
  Parse::Pointer.new(self.merge(Parse::Protocol::KEY_CLASS_NAME => class_name)) unless new?
end

#refreshObject

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



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/parse/object.rb', line 162

def refresh
  if @parse_object_id
    data = Parse.get @class_name, @parse_object_id
    clear
    if data
      parse data
    end
  end

  self
end

#safe_hashObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/parse/object.rb', line 82

def safe_hash
  without_reserved = self.dup
  Protocol::RESERVED_KEYS.each { |k| without_reserved.delete(k) }

  without_relations = without_reserved
  without_relations.each do |k,v|
      if v.is_a? Hash
        if v[Protocol::KEY_TYPE] == Protocol::TYPE_RELATION
          without_relations.delete(k)
        end
      end
  end

  without_relations.each do |k, v|
    without_relations[k] = Parse.pointerize_value(v)
  end

  without_relations
end

#safe_jsonObject



102
103
104
# File 'lib/parse/object.rb', line 102

def safe_json
  safe_hash.to_json
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.



111
112
113
114
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 111

def save
  if @parse_object_id
    method = :put
    self.merge!(@op_fields) # use operations instead of our own view of the columns
  else
    method = :post
  end

  body = safe_json
  data = Parse.client.request(self.uri, method, body)

  if data
    # array operations can return mutated view of array which needs to be parsed
    parse Parse.parse_json(class_name, data)
  end

  if @class_name == Parse::Protocol::CLASS_USER
    self.delete("password")
    self.delete(:username)
    self.delete(:password)
  end

  self
end

#to_json(*a) ⇒ Object



148
149
150
# File 'lib/parse/object.rb', line 148

def to_json(*a)
  as_json.to_json(*a)
end

#to_sObject



152
153
154
# File 'lib/parse/object.rb', line 152

def to_s
  "#{@class_name}:#{@parse_object_id} #{super}"
end

#uriObject



33
34
35
# File 'lib/parse/object.rb', line 33

def uri
  Protocol.class_uri @class_name, @parse_object_id
end