Class: Parse::Object
- Inherits:
-
Hash
- Object
- Hash
- Parse::Object
- Defined in:
- lib/parse/object.rb
Overview
Represents an individual Parse API object.
Direct Known Subclasses
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
- #as_json(*a) ⇒ Object
-
#decrement(field, amount = 1) ⇒ Object
Decrement the given field by an amount, which defaults to 1.
-
#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.
- #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_json ⇒ Object
-
#save ⇒ Object
Write the current state of the local object to the API.
- #to_json(*a) ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(class_name, data = nil) ⇒ Object
Returns a new instance of Object.
15 16 17 18 19 20 |
# File 'lib/parse/object.rb', line 15 def initialize(class_name, data = nil) @class_name = class_name 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
#as_json(*a) ⇒ Object
104 105 106 107 108 |
# File 'lib/parse/object.rb', line 104 def as_json(*a) Hash[self.map do |key, value| [key, value.nil? ? Protocol::DELETE_OP : value] end] end |
#decrement(field, amount = 1) ⇒ Object
Decrement the given field by an amount, which defaults to 1. A synonym for increment(field, -amount).
162 163 164 165 166 167 168 |
# File 'lib/parse/object.rb', line 162 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 |
#increment(field, amount = 1) ⇒ Object
Increment the given field by an amount, which defaults to 1.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/parse/object.rb', line 139 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 |
#new? ⇒ Boolean
61 62 63 |
# File 'lib/parse/object.rb', line 61 def new? self["objectId"].nil? end |
#parse_delete ⇒ Object
Delete the remote Parse API object.
129 130 131 132 133 134 135 136 |
# File 'lib/parse/object.rb', line 129 def parse_delete if @parse_object_id response = Parse.client.delete self.uri end self.clear self end |
#pointer ⇒ Object
26 27 28 |
# File 'lib/parse/object.rb', line 26 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.
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/parse/object.rb', line 116 def refresh if @parse_object_id data = Parse.get @class_name, @parse_object_id clear if data parse data end end self end |
#safe_json ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/parse/object.rb', line 65 def safe_json 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.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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/parse/object.rb', line 86 def save method = @parse_object_id ? :put : :post 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
110 111 112 |
# File 'lib/parse/object.rb', line 110 def to_json(*a) as_json.to_json(*a) end |