Method: Parse::Object#initialize

Defined in:
lib/parse/model/object.rb

#new(id) ⇒ Parse::Object #new(hash = {}) ⇒ Parse::Object

Note:

Should only be called with Parse::Object subclasses.

The main constructor for subclasses. It can take different parameter types including a String and a JSON hash. Assume a Post class that inherits from Parse::Object:

Overloads:

  • #new(id) ⇒ Parse::Object

    Create a new object with an objectId. This method is useful for creating an unfetched object (pointer-state).

    Examples:

    Post.new "1234"
    

    Parameters:

    • The object id.

  • #new(hash = {}) ⇒ Parse::Object

    Create a new object with Parse JSON hash.

    Examples:

    # JSON hash from Parse
    Post.new({"className" => "Post", "objectId" => "1234", "title" => "My Title"})
    
    post = Post.new title: "My Title"
    post.title # => "My Title"
    

    Parameters:

    • (defaults to: {})

      the hash representing the object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/parse/model/object.rb', line 304

def initialize(opts = {})
  if opts.is_a?(String) #then it's the objectId
    @id = opts.to_s
  elsif opts.is_a?(Hash)
    #if the objectId is provided we will consider the object pristine
    #and not track dirty items
    dirty_track = opts[Parse::Model::OBJECT_ID] || opts[:objectId] || opts[:id]
    apply_attributes!(opts, dirty_track: !dirty_track)
  end

  # if no ACLs, then apply the class default acls
  # ACL.typecast will auto convert of Parse::ACL
  self.acl = self.class.default_acls.as_json if self.acl.nil?

  clear_changes! if @id.present? #then it was an import

  # do not apply defaults on a pointer because it will stop it from being
  # a pointer and will cause its field to be autofetched (for sync)
  apply_defaults! unless pointer?
  # do not call super since it is Pointer subclass
end