Class: LC::Object

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

Overview

Represents an individual Parse API object.

Direct Known Subclasses

Installation, Model, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name, data = nil) ⇒ Object

Returns a new instance of Object.



16
17
18
19
20
21
22
# File 'lib/leancloud/object.rb', line 16

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.



11
12
13
# File 'lib/leancloud/object.rb', line 11

def class_name
  @class_name
end

#created_atObject (readonly)

Returns the value of attribute created_at.



12
13
14
# File 'lib/leancloud/object.rb', line 12

def created_at
  @created_at
end

#parse_object_idObject (readonly) Also known as: id

Returns the value of attribute parse_object_id.



10
11
12
# File 'lib/leancloud/object.rb', line 10

def parse_object_id
  @parse_object_id
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



13
14
15
# File 'lib/leancloud/object.rb', line 13

def updated_at
  @updated_at
end

Instance Method Details

#array_add(field, value) ⇒ Object



155
156
157
# File 'lib/leancloud/object.rb', line 155

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

#array_add_relation(field, value) ⇒ Object



159
160
161
# File 'lib/leancloud/object.rb', line 159

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

#array_add_unique(field, value) ⇒ Object



167
168
169
# File 'lib/leancloud/object.rb', line 167

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

#array_remove(field, value) ⇒ Object



171
172
173
# File 'lib/leancloud/object.rb', line 171

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

#array_remove_relation(field, value) ⇒ Object



163
164
165
# File 'lib/leancloud/object.rb', line 163

def array_remove_relation(field, value)
  array_op(field, Protocol::KEY_REMOVE_RELATION, value)
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).



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

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

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

Returns:

  • (Boolean)


24
25
26
# File 'lib/leancloud/object.rb', line 24

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

#getObject

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



43
44
45
# File 'lib/leancloud/object.rb', line 43

def get
  self
end

#hashObject



30
31
32
# File 'lib/leancloud/object.rb', line 30

def hash
  LC.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



176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/leancloud/object.rb', line 176

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

#inspectObject



127
128
129
# File 'lib/leancloud/object.rb', line 127

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

#new?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/leancloud/object.rb', line 47

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

#parse_deleteObject

Delete the remote Parse API object.



146
147
148
149
150
151
152
153
# File 'lib/leancloud/object.rb', line 146

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

  self.clear
  self
end

#pointerObject



38
39
40
# File 'lib/leancloud/object.rb', line 38

def pointer
  LC::Pointer.new(rest_api_hash) unless new?
end

#refreshObject

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



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/leancloud/object.rb', line 133

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_hashObject

full REST api representation of object



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

def rest_api_hash
  self.merge(LC::Protocol::KEY_CLASS_NAME => class_name)
end

#safe_hashObject

representation of object to send on saves



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/leancloud/object.rb', line 87

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

#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.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/leancloud/object.rb', line 59

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) ⇒ Boolean

Handle the addition of Array#to_h in Ruby 2.1

Returns:

  • (Boolean)


107
108
109
# File 'lib/leancloud/object.rb', line 107

def should_call_to_h?(value)
  value.respond_to?(:to_h) && !value.kind_of?(Array)
end

#to_h(*a) ⇒ Object Also known as: as_json, to_hash



111
112
113
114
115
# File 'lib/leancloud/object.rb', line 111

def to_h(*a)
  Hash[rest_api_hash.map do |key, value|
    [key, should_call_to_h?(value) ? value.to_h : value]
  end]
end

#to_json(*a) ⇒ Object



119
120
121
# File 'lib/leancloud/object.rb', line 119

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

#to_sObject



123
124
125
# File 'lib/leancloud/object.rb', line 123

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

#update_attributes(data = {}) ⇒ Object



51
52
53
54
# File 'lib/leancloud/object.rb', line 51

def update_attributes(data={})
  data.each_pair { |k,v| self[k] = v }
  save
end

#uriObject



34
35
36
# File 'lib/leancloud/object.rb', line 34

def uri
  Protocol.class_uri @class_name, @parse_object_id
end