lib/leancloud/object.rb in leancloud-ruby-client-0.1.1 vs lib/leancloud/object.rb in leancloud-ruby-client-0.2.0

- old
+ new

@@ -1,11 +1,11 @@ # -*- encoding : utf-8 -*- require 'leancloud/protocol' require 'leancloud/client' require 'leancloud/error' -module AV +module LC # Represents an individual Parse API object. class Object < Hash attr_reader :parse_object_id attr_reader :class_name @@ -20,25 +20,25 @@ parse data end end def eql?(other) - AV.object_pointer_equality?(self, other) + LC.object_pointer_equality?(self, other) end alias == eql? def hash - AV.object_pointer_hash(self) + LC.object_pointer_hash(self) end def uri Protocol.class_uri @class_name, @parse_object_id end def pointer - AV::Pointer.new(rest_api_hash) unless new? + LC::Pointer.new(rest_api_hash) unless new? end # make it easier to deal with the ambiguity of whether you're passed a pointer or object def get self @@ -65,18 +65,18 @@ end body = safe_hash.to_json uri = self.uri uri = uri + '?new=true' if method == :put - data = AV.client.request(uri, method, body) + data = LC.client.request(uri, method, body) if data # array operations can return mutated view of array which needs to be parsed - parse AV.parse_json(class_name, data) + parse LC.parse_json(class_name, data) end - if @class_name == AV::Protocol::CLASS_USER + if @class_name == LC::Protocol::CLASS_USER self.delete("password") self.delete(:username) self.delete(:password) end @@ -91,18 +91,18 @@ elsif value.is_a?(Hash) && value[Protocol::KEY_TYPE] == Protocol::TYPE_RELATION nil elsif value.nil? [key, Protocol::DELETE_OP] else - [key, AV.pointerize_value(value)] + [key, LC.pointerize_value(value)] end end.compact] end # full REST api representation of object def rest_api_hash - self.merge(AV::Protocol::KEY_CLASS_NAME => class_name) + self.merge(LC::Protocol::KEY_CLASS_NAME => class_name) end # Handle the addition of Array#to_h in Ruby 2.1 def should_call_to_h?(value) value.respond_to?(:to_h) && !value.kind_of?(Array) @@ -130,11 +130,11 @@ # Update the fields of the local Parse object with the current # values from the API. def refresh if @parse_object_id - data = AV.get @class_name, @parse_object_id + data = LC.get @class_name, @parse_object_id clear if data parse data end end @@ -143,11 +143,11 @@ end # Delete the remote Parse API object. def parse_delete if @parse_object_id - response = AV.client.delete self.uri + response = LC.client.delete self.uri end self.clear self end @@ -179,12 +179,12 @@ #if !@parse_object_id # # TODO - warn that the object must be stored first # return nil #end - body = {field => AV::Increment.new(amount)}.to_json - data = AV.client.request(self.uri() + '?new=true', :put, body) + body = {field => LC::Increment.new(amount)}.to_json + data = LC.client.request(self.uri() + '?new=true', :put, body) parse data self end # Decrement the given field by an amount, which defaults to 1. Saves immediately to reflect decremented @@ -217,11 +217,11 @@ data.each do |k,v| if k.is_a? Symbol k = k.to_s end - if k != AV::Protocol::KEY_TYPE + if k != LC::Protocol::KEY_TYPE self[k] = v end end self @@ -231,10 +231,10 @@ 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 << AV.pointerize_value(value) + @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