Class: Parse::Object
- Inherits:
-
Hash
- Object
- Hash
- Parse::Object
- Defined in:
- lib/parse/object.rb
Overview
Represents an individual Parse API object.
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#parse_object_id ⇒ Object
(also: #id)
readonly
Returns the value of attribute parse_object_id.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Instance Method Summary collapse
- #array_add(field, value) ⇒ Object
- #array_add_relation(field, value) ⇒ Object
- #array_add_unique(field, value) ⇒ Object
- #array_remove(field, value) ⇒ Object
- #as_json(*a) ⇒ Object
-
#decrement(field, amount = 1) ⇒ Object
Decrement the given field by an amount, which defaults to 1.
-
#get ⇒ Object
make it easier to deal with the ambiguity of whether you’re passed a pointer or object.
-
#increment(field, amount = 1) ⇒ Object
Increment the given field by an amount, which defaults to 1.
-
#initialize(class_name, data = nil) ⇒ Object
constructor
A new instance of Object.
- #inspect ⇒ Object
- #new? ⇒ Boolean
-
#parse_delete ⇒ Object
Delete the remote Parse API object.
- #pointer ⇒ Object
-
#refresh ⇒ Object
Update the fields of the local Parse object with the current values from the API.
- #safe_hash ⇒ Object
- #safe_json ⇒ Object
-
#save ⇒ Object
Write the current state of the local object to the API.
- #to_json(*a) ⇒ Object
- #to_s ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(class_name, data = nil) ⇒ Object
Returns a new instance of Object.
15 16 17 18 19 20 21 |
# File 'lib/parse/object.rb', line 15 def initialize(class_name, data = nil) @class_name = class_name @op_fields = {} if data parse data end end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
10 11 12 |
# File 'lib/parse/object.rb', line 10 def class_name @class_name end |
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
11 12 13 |
# File 'lib/parse/object.rb', line 11 def created_at @created_at end |
#parse_object_id ⇒ Object (readonly) Also known as: id
Returns the value of attribute parse_object_id.
9 10 11 |
# File 'lib/parse/object.rb', line 9 def parse_object_id @parse_object_id end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
12 13 14 |
# File 'lib/parse/object.rb', line 12 def updated_at @updated_at end |
Instance Method Details
#array_add(field, value) ⇒ Object
168 169 170 |
# File 'lib/parse/object.rb', line 168 def array_add(field, value) array_op(field, Protocol::KEY_ADD, value) end |
#array_add_relation(field, value) ⇒ Object
172 173 174 |
# File 'lib/parse/object.rb', line 172 def array_add_relation(field, value) array_op(field, Protocol::KEY_ADD_RELATION, value) end |
#array_add_unique(field, value) ⇒ Object
176 177 178 |
# File 'lib/parse/object.rb', line 176 def array_add_unique(field, value) array_op(field, Protocol::KEY_ADD_UNIQUE, value) end |
#array_remove(field, value) ⇒ Object
180 181 182 |
# File 'lib/parse/object.rb', line 180 def array_remove(field, value) array_op(field, Protocol::KEY_REMOVE, value) end |
#as_json(*a) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/parse/object.rb', line 120 def as_json(*a) Hash[self.map do |key, value| value = if value value.respond_to?(:as_json) ? value.as_json : value else Protocol::DELETE_OP end [key, value] end] 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).
208 209 210 211 212 213 214 |
# File 'lib/parse/object.rb', line 208 def decrement(field, amount = 1) #increment field, -amount body = {field => Parse::Decrement.new(amount)}.to_json data = Parse.client.request(self.uri, :put, body) parse data self end |
#get ⇒ Object
make it easier to deal with the ambiguity of whether you’re passed a pointer or object
32 33 34 |
# File 'lib/parse/object.rb', line 32 def get self end |
#increment(field, amount = 1) ⇒ Object
Increment the given field by an amount, which defaults to 1. Saves immediately to reflect incremented
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/parse/object.rb', line 185 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 #if amount != 0 # op = amount > 0 ? Protocol::OP_INCREMENT : Protocol::OP_DECREMENT # body = "{\"#{field}\": {\"#{Protocol::KEY_OP}\": \"#{op}\", \"#{Protocol::KEY_AMOUNT}\" : #{amount.abs}}}" # data = Parse.client.request( self.uri, :put, body) # parse data #end #self body = {field => Parse::Increment.new(amount)}.to_json data = Parse.client.request(self.uri, :put, body) parse data self end |
#inspect ⇒ Object
140 141 142 |
# File 'lib/parse/object.rb', line 140 def inspect "#{@class_name}:#{@parse_object_id} #{super}" end |
#new? ⇒ Boolean
67 68 69 |
# File 'lib/parse/object.rb', line 67 def new? self["objectId"].nil? end |
#parse_delete ⇒ Object
Delete the remote Parse API object.
159 160 161 162 163 164 165 166 |
# File 'lib/parse/object.rb', line 159 def parse_delete if @parse_object_id response = Parse.client.delete self.uri end self.clear self end |
#pointer ⇒ Object
27 28 29 |
# File 'lib/parse/object.rb', line 27 def pointer Parse::Pointer.new(self.merge(Parse::Protocol::KEY_CLASS_NAME => class_name)) unless new? end |
#refresh ⇒ Object
Update the fields of the local Parse object with the current values from the API.
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/parse/object.rb', line 146 def refresh if @parse_object_id data = Parse.get @class_name, @parse_object_id clear if data parse data end end self end |
#safe_hash ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/parse/object.rb', line 71 def safe_hash without_reserved = self.dup Protocol::RESERVED_KEYS.each { |k| without_reserved.delete(k) } without_relations = without_reserved without_relations.each { |k,v| if v.is_a? Hash if v[Protocol::KEY_TYPE] == Protocol::TYPE_RELATION without_relations.delete(k) end end } without_relations end |
#safe_json ⇒ Object
87 88 89 |
# File 'lib/parse/object.rb', line 87 def safe_json safe_hash.to_json end |
#save ⇒ Object
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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/parse/object.rb', line 96 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_json data = Parse.client.request(self.uri, method, body) if data parse data end if @class_name == Parse::Protocol::CLASS_USER self.delete("password") self.delete(:username) self.delete(:password) end self end |
#to_json(*a) ⇒ Object
132 133 134 |
# File 'lib/parse/object.rb', line 132 def to_json(*a) as_json.to_json(*a) end |
#to_s ⇒ Object
136 137 138 |
# File 'lib/parse/object.rb', line 136 def to_s "#{@class_name}:#{@parse_object_id} #{super}" end |