class LC::Object

Represents an individual Parse API object.

Attributes

class_name[R]
created_at[R]
id[R]
parse_object_id[R]
updated_at[R]

Public Class Methods

new(class_name, data = nil) click to toggle source
# File lib/leancloud/object.rb, line 15
def initialize(class_name, data = nil)
  @class_name = class_name
  @op_fields = {}
  if data
    parse data
  end
end

Public Instance Methods

==(other)
Alias for: eql?
array_add(field, value) click to toggle source
# File lib/leancloud/object.rb, line 154
def array_add(field, value)
  array_op(field, Protocol::KEY_ADD, value)
end
array_add_relation(field, value) click to toggle source
# File lib/leancloud/object.rb, line 158
def array_add_relation(field, value)
  array_op(field, Protocol::KEY_ADD_RELATION, value)
end
array_add_unique(field, value) click to toggle source
# File lib/leancloud/object.rb, line 166
def array_add_unique(field, value)
  array_op(field, Protocol::KEY_ADD_UNIQUE, value)
end
array_remove(field, value) click to toggle source
# File lib/leancloud/object.rb, line 170
def array_remove(field, value)
  array_op(field, Protocol::KEY_REMOVE, value)
end
array_remove_relation(field, value) click to toggle source
# File lib/leancloud/object.rb, line 162
def array_remove_relation(field, value)
  array_op(field, Protocol::KEY_REMOVE_RELATION, value)
end
as_json(*a)
Alias for: to_h
decrement(field, amount = 1) click to toggle source

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

# File lib/leancloud/object.rb, line 191
def decrement(field, amount = 1)
  increment(field, -amount)
end
eql?(other) click to toggle source
# File lib/leancloud/object.rb, line 23
def eql?(other)
  LC.object_pointer_equality?(self, other)
end
Also aliased as: ==
get() click to toggle source

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

# File lib/leancloud/object.rb, line 42
def get
  self
end
hash() click to toggle source
# File lib/leancloud/object.rb, line 29
def hash
  LC.object_pointer_hash(self)
end
increment(field, amount = 1) click to toggle source

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

# File lib/leancloud/object.rb, line 175
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 => LC::Increment.new(amount)}.to_json
  data = LC.client.request(self.uri() + '?new=true', :put, body)
  parse data
  self
end
inspect() click to toggle source
# File lib/leancloud/object.rb, line 126
def inspect
  "#{@class_name}:#{@parse_object_id} #{super}"
end
new?() click to toggle source
# File lib/leancloud/object.rb, line 46
def new?
  self["objectId"].nil?
end
parse_delete() click to toggle source

Delete the remote Parse API object.

# File lib/leancloud/object.rb, line 145
def parse_delete
  if @parse_object_id
    response = LC.client.delete self.uri
  end

  self.clear
  self
end
pointer() click to toggle source
# File lib/leancloud/object.rb, line 37
def pointer
  LC::Pointer.new(rest_api_hash) unless new?
end
refresh() click to toggle source

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

# File lib/leancloud/object.rb, line 132
def refresh
  if @parse_object_id
    data = LC.get @class_name, @parse_object_id
    clear
    if data
      parse data
    end
  end

  self
end
rest_api_hash() click to toggle source

full REST api representation of object

# File lib/leancloud/object.rb, line 101
def rest_api_hash
  self.merge(LC::Protocol::KEY_CLASS_NAME => class_name)
end
safe_hash() click to toggle source

representation of object to send on saves

# File lib/leancloud/object.rb, line 86
def safe_hash
  Hash[self.map do |key, value|
    if Protocol::RESERVED_KEYS.include?(key)
      nil
    elsif value.is_a?(Hash) && value[Protocol::KEY_TYPE] == Protocol::TYPE_RELATION
      nil
    elsif value.nil?
      [key, Protocol::DELETE_OP]
    else
      [key, LC.pointerize_value(value)]
    end
  end.compact]
end
save() click to toggle source

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.

# File lib/leancloud/object.rb, line 58
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_hash.to_json
  uri = self.uri
  uri = uri + '?new=true' if method == :put
  data = LC.client.request(uri, method, body)

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

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

  self
end
should_call_to_h?(value) click to toggle source

Handle the addition of Array#to_h in Ruby 2.1

# File lib/leancloud/object.rb, line 106
def should_call_to_h?(value)
  value.respond_to?(:to_h) && !value.kind_of?(Array)
end
to_h(*a) click to toggle source
# File lib/leancloud/object.rb, line 110
def to_h(*a)
  Hash[rest_api_hash.map do |key, value|
    [key, should_call_to_h?(value) ? value.to_h : value]
  end]
end
Also aliased as: as_json, to_hash
to_hash(*a)
Alias for: to_h
to_json(*a) click to toggle source
# File lib/leancloud/object.rb, line 118
def to_json(*a)
  to_h.to_json(*a)
end
to_s() click to toggle source
# File lib/leancloud/object.rb, line 122
def to_s
  "#{@class_name}:#{@parse_object_id} #{super}"
end
update_attributes(data={}) click to toggle source
# File lib/leancloud/object.rb, line 50
def update_attributes(data={})
  data.each_pair { |k,v| self[k] = v }
  save
end
uri() click to toggle source
# File lib/leancloud/object.rb, line 33
def uri
  Protocol.class_uri @class_name, @parse_object_id
end

Private Instance Methods

array_op(field, operation, value) click to toggle source
# File lib/leancloud/object.rb, line 229
def array_op(field, operation, value)
  raise "field #{field} not an array" if self[field] && !self[field].is_a?(Array)

  if @parse_object_id
    @op_fields[field] ||= ArrayOp.new(operation, [])
    raise "only one operation type allowed per array #{field}" if @op_fields[field].operation != operation
    @op_fields[field].objects << LC.pointerize_value(value)
  end

  # parse doesn't return column values on initial POST creation so we must maintain them ourselves
  case operation
  when Protocol::KEY_ADD, Protocol::KEY_ADD_RELATION
    self[field] ||= []
    self[field] << value
  when Protocol::KEY_ADD_UNIQUE
    self[field] ||= []
    self[field] << value unless self[field].include?(value)
  when Protocol::KEY_REMOVE, Protocol::KEY_REMOVE_RELATION
    self[field].delete(value) if self[field]
  end
end
parse(data) click to toggle source

Merge a hash parsed from the JSON representation into this instance. This will extract the reserved fields, merge the hash keys, and then ensure that the reserved fields do not occur in the underlying hash storage.

# File lib/leancloud/object.rb, line 201
def parse(data)
  if !data
    return
  end

  @parse_object_id ||= data[Protocol::KEY_OBJECT_ID]

  if data.has_key? Protocol::KEY_CREATED_AT
    @created_at = DateTime.parse data[Protocol::KEY_CREATED_AT]
  end

  if data.has_key? Protocol::KEY_UPDATED_AT
    @updated_at = DateTime.parse data[Protocol::KEY_UPDATED_AT]
  end

  data.each do |k,v|
    if k.is_a? Symbol
      k = k.to_s
    end

    if k != LC::Protocol::KEY_TYPE
      self[k] = v
    end
  end

  self
end