Class: Parse::Object

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

Direct Known Subclasses

Installation, Role, User

Constant Summary collapse

@@parse_class_vs_class_table =
{}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#string_keyed_hash

Constructor Details

#initialize(hash = {}) ⇒ Object

Returns a new instance of 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
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/parse/object.rb', line 111

def initialize hash={}
  body_hash = nil
  hash = string_keyed_hash hash
  if hash.has_key? 'objectId'
    @parse_object_id = hash['objectId']
    @raw_hash = hash
    @updated_hash = {}
    body_hash = @raw_hash
  else
    @raw_hash = {}
    @updated_hash = hash
    body_hash = @updated_hash
  end
  @deleted = false

  hash.each do |k, v|
    if v.is_a? Hash
      body_hash[k] = 
        case v['__type']
        when nil
          Parse::ACL.new v
        when 'Date'
          ParseDate.parse v['iso']
        when 'File'
          ParseFile.new v
        when 'Pointer'
          Parse::Pointer.new v, self
        when 'Relation'
          Parse::Relation.new self, k, v
        when 'GeoPoint'
          Parse::GeoPoint.new v
        else
          v
        end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



304
305
306
307
308
309
310
311
312
# File 'lib/parse/object.rb', line 304

def method_missing name, *args, &block
  if name =~ /^\w+$/ && args.empty? && block.nil?
    get_column name
  elsif name[-1] == '=' && args.size == 1 && block.nil?
    set_column name[0..-2], args.first
  else
    super
  end
end

Class Attribute Details

.auto_camel_caseObject

Returns the value of attribute auto_camel_case.



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

def auto_camel_case
  @auto_camel_case
end

.parse_class_nameObject

Returns the value of attribute parse_class_name.



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

def parse_class_name
  @parse_class_name
end

.parse_clientObject

Returns the value of attribute parse_client.



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

def parse_client
  @parse_client
end

Instance Attribute Details

#aclObject

Returns the value of attribute acl.



109
110
111
# File 'lib/parse/object.rb', line 109

def acl
  @acl
end

#created_atObject

Returns the value of attribute created_at.



109
110
111
# File 'lib/parse/object.rb', line 109

def created_at
  @created_at
end

#parse_object_idObject Also known as: obj_id

Returns the value of attribute parse_object_id.



109
110
111
# File 'lib/parse/object.rb', line 109

def parse_object_id
  @parse_object_id
end

#updated_atObject

Returns the value of attribute updated_at.



109
110
111
# File 'lib/parse/object.rb', line 109

def updated_at
  @updated_at
end

Class Method Details

.count(condition = nil, &block) ⇒ Object



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

def count condition=nil, &block
  opts = {:limit => 0, :count => true}
  if condition
    opts[:where] = condition
  elsif block
    opts[:where] = block
  end
  find(:all, opts).query_count
end

.count!(condition = nil, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/parse/object.rb', line 98

def count! condition=nil, &block
  opts = {:limit => 0, :count => true}
  if condition
    opts[:where] = condition
  elsif block
    opts[:where] = block
  end
  find!(:all, opts).query_count
end

.create(parse_class_name, mod = ::Object) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/parse/object.rb', line 29

def create parse_class_name, mod=::Object
  raise 'already defined' if mod.const_defined? parse_class_name

  if RESERVED_PARSE_CLASS.has_key? parse_class_name.to_s
    eval RESERVED_PARSE_CLASS[parse_class_name.to_s]
  else
    klass = Class.new(Parse::Object)
    klass.parse_class_name = parse_class_name.to_sym
    klass.auto_camel_case = true
    mod.const_set parse_class_name, klass
    register_parse_class klass
  end
end

.find(object_id_or_conditions, opts = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/parse/object.rb', line 51

def find object_id_or_conditions, opts={}
  raw_results = parse_client.find(self.parse_class_name, object_id_or_conditions, opts)
  results = [raw_results].flatten
  results.map! {|hash| self.new hash} # TODO: should be recursive
  if raw_results.is_a? Array
    results.query_count = raw_results.query_count
  end
  results
end

.find!(object_id_or_conditions, opts = {}) ⇒ Object

TODO: need refactoring



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

def find! object_id_or_conditions, opts={}
  raw_results = parse_client.find!(self.parse_class_name, object_id_or_conditions, opts)
  results = [raw_results].flatten
  results.map! {|hash| self.new hash}
  if raw_results.is_a? Array
    results.query_count = raw_results.query_count
  end
  results
end

.find_all(opts = {}) ⇒ Object



80
81
82
# File 'lib/parse/object.rb', line 80

def find_all opts={}
  find :all, opts
end

.find_all!(opts = {}) ⇒ Object



84
85
86
# File 'lib/parse/object.rb', line 84

def find_all! opts={}
  find! :all, opts
end

.find_by_id(object_id, opts = {}) ⇒ Object



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

def find_by_id object_id, opts={}
  find(object_id, opts).first
end

.find_by_id!(object_id, opts = {}) ⇒ Object



76
77
78
# File 'lib/parse/object.rb', line 76

def find_by_id! object_id, opts={}
  find!(object_id, opts).first
end

.register_parse_class(parse_class) ⇒ Object



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

def register_parse_class parse_class
  @@parse_class_vs_class_table[parse_class.parse_class_name] = parse_class
end

.reserved_column?(c) ⇒ Boolean

Returns:

  • (Boolean)


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

def reserved_column? c
  reserved_columns.include? c
end

.reserved_columnsObject



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

def reserved_columns
  %w(objectId)
end

Instance Method Details

#create(hash, use_master_key = false) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/parse/object.rb', line 188

def create hash, use_master_key=false
  check_deleted!
  hash = string_keyed_hash hash
  @updated_hash = @raw_hash.dup.update(@updated_hash).update hash
  @updated_hash.reject! do |k, v|
    v.is_a?(Parse::Relation) && !v.changed?
  end
  method = use_master_key ? :create! : :create
  parse_client.send(method, self.parse_class_name, @updated_hash) do |response|
    @parse_object_id = response['objectId']
    @created_at = ParseDate.parse response['createdAt']
    @updated_at = @created_at
    @raw_hash.update @updated_hash
    @raw_hash.update response
    @updated_hash.clear
  end
end

#create!(hash = {}) ⇒ Object



206
207
208
# File 'lib/parse/object.rb', line 206

def create! hash={}
  create hash, true
end

#delete(use_master_key = false) ⇒ Object



226
227
228
229
230
231
232
233
# File 'lib/parse/object.rb', line 226

def delete use_master_key=false
  raise 'You cannot delete new object' if new?
  check_deleted!
  method = use_master_key ? :delete! : :delete
  parse_client.send(method, parse_class_name, parse_object_id) do |response|
    @deleted = true
  end
end

#delete!Object



235
236
237
# File 'lib/parse/object.rb', line 235

def delete!
  delete true
end

#deleted?Boolean

Returns:

  • (Boolean)


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

def deleted?
  @deleted
end

#get_column(name) ⇒ Object



246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/parse/object.rb', line 246

def get_column name
  name = name.to_s
  ret = @updated_hash[name]
  if ret.nil? && self.class.auto_camel_case
    ret = @updated_hash[name.camelize :lower]
  end
  if ret.nil?
    ret = @raw_hash[name]
    if ret.nil? && self.class.auto_camel_case
      ret = @raw_hash[name.camelize :lower]
    end
  end
  ret
end

#new?Boolean

Returns:

  • (Boolean)


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

def new?
  !@deleted && @raw_hash.empty?
end

#parse_class_nameObject



165
166
167
# File 'lib/parse/object.rb', line 165

def parse_class_name
  self.class.parse_class_name
end

#parse_clientObject



161
162
163
# File 'lib/parse/object.rb', line 161

def parse_client
  self.class.parse_client
end

#pointerObject



266
267
268
# File 'lib/parse/object.rb', line 266

def pointer
  Pointer.new 'className' => parse_class_name, 'objectId' => parse_object_id
end

#save(hash = @updated_hash, use_master_key = false) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/parse/object.rb', line 169

def save hash=@updated_hash, use_master_key=false
  check_deleted!

  unless hash.is_a? Hash
    hash = @updated_hash
    use_master_key = hash 
  end
  hash = string_keyed_hash hash
  if new?
    create hash, use_master_key
  else
    update hash, use_master_key
  end
end

#save!(hash = @updated_hash) ⇒ Object



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

def save! hash=@updated_hash
  save hash, true
end

#set_column(name, value) ⇒ Object



261
262
263
264
# File 'lib/parse/object.rb', line 261

def set_column name, value
  check_deleted!
  @updated_hash[name] = value
end

#to_hObject



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/parse/object.rb', line 270

def to_h
  {"__type" => self.class.name}.update(@raw_hash).update(@updated_hash)
  ret = {}
  {"__type" => self.class.name}.update(@raw_hash).update(@updated_hash).each do |k, v|
    ret[k] = 
      case v
      when Parse::Pointer
        v.to_h
      when Parse::Object
        v.to_h
      when Parse::Relation
        '<Ralations>'
      else
        v.to_s
      end
  end
  ret
=begin
  Hash[
    *({"__type" => self.class.name}.update(@raw_hash.dup).update(@updated_hash.dup).to_a.map do |a|
      [a[0], a[1].to_h]
    end.flatten(1))
  ]
=end
end

#to_json(*args) ⇒ Object



296
297
298
# File 'lib/parse/object.rb', line 296

def to_json *args
  to_h.to_json
end

#to_sObject



300
301
302
# File 'lib/parse/object.rb', line 300

def to_s
  to_h.to_s
end

#update(hash, use_master_key = false) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/parse/object.rb', line 210

def update hash, use_master_key=false
  check_deleted!
  hash = string_keyed_hash hash
  method = use_master_key ? :update! : :update
  parse_client.send(method, parse_class_name, parse_object_id, hash) do |response|
    @raw_hash.update @updated_hash
    @raw_hash.update response
    @updated_at = ParseDate.parse response['updatedAt']
    @updated_hash.clear
  end
end

#update!(hash) ⇒ Object



222
223
224
# File 'lib/parse/object.rb', line 222

def update! hash
  update hash, true
end

#updated?Boolean

Returns:

  • (Boolean)


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

def updated?
  !@deleted && !@updated_hash.empty?
end