Class: ParseResource::Base
- Inherits:
-
Object
- Object
- ParseResource::Base
- Extended by:
- ActiveModel::Callbacks, ActiveModel::Naming
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Conversion, ActiveModel::Validations
- Defined in:
- lib/base.rb
Direct Known Subclasses
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- HashWithIndifferentAccess =
ActiveSupport::HashWithIndifferentAccess
Class Method Summary collapse
-
.all ⇒ Array
Find all ParseResource::Base objects for that model.
- .class_attributes ⇒ Object
-
.count ⇒ Object
Add this at the end of a method chain to get the count of objects, instead of an Array of objects.
-
.create(attributes = {}) ⇒ ParseResource
Create a ParseResource::Base object.
- .destroy_all ⇒ Object
-
.field(name, val = nil) ⇒ Object
Explicitly adds a field to the model.
-
.fields(*args) ⇒ Object
Add multiple fields in one line.
-
.find(id) ⇒ ParseResource
Find a ParseResource::Base object by ID.
-
.first ⇒ Object
Find the first object.
- .included(base) ⇒ Object
-
.limit(n) ⇒ Object
Limits the number of objects returned.
-
.load!(app_id, master_key) ⇒ Object
Explicitly set Parse.com API keys.
-
.resource ⇒ Object
Creates a RESTful resource sends requests to [base_uri]/.
- .settings ⇒ Object
-
.where(*args) ⇒ Object
Find a ParseResource::Base object by chaining #where method calls.
Instance Method Summary collapse
-
#attributes ⇒ Object
provides access to @attributes for getting and setting.
- #attributes=(n) ⇒ Object
- #create ⇒ Object
-
#create_setters! ⇒ Object
Creates getter and setter methods for model fields.
- #created_at ⇒ Object
- #destroy ⇒ Object
-
#id ⇒ Object
aliasing for idiomatic Ruby.
-
#initialize(attributes = {}, new = true) ⇒ ParseResource::Base
constructor
Instantiates a ParseResource::Base object.
-
#instance_resource ⇒ Object
create RESTful resource for the specific Parse object sends requests to [base_uri]//[objectId].
- #new? ⇒ Boolean
- #persisted? ⇒ Boolean
-
#resource ⇒ Object
delegate from Class method.
- #save ⇒ Object
- #update(attributes = {}) ⇒ Object
- #update_attributes(attributes = {}) ⇒ Object
- #updated_at ⇒ Object
Constructor Details
#initialize(attributes = {}, new = true) ⇒ ParseResource::Base
Instantiates a ParseResource::Base object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/base.rb', line 33 def initialize(attributes = {}, new=true) attributes = HashWithIndifferentAccess.new(attributes) if new @unsaved_attributes = attributes else @unsaved_attributes = {} end self.attributes = {} self.attributes.merge!(attributes) self.attributes unless self.attributes.empty? create_setters! end |
Class Method Details
.all ⇒ Array
Find all ParseResource::Base objects for that model.
165 166 167 |
# File 'lib/base.rb', line 165 def all Query.new(self).all end |
.class_attributes ⇒ Object
196 197 198 |
# File 'lib/base.rb', line 196 def class_attributes @class_attributes ||= {} end |
.count ⇒ Object
Add this at the end of a method chain to get the count of objects, instead of an Array of objects
157 158 159 160 |
# File 'lib/base.rb', line 157 def count #https://www.parse.com/docs/rest#queries-counting Query.new(self).count(1) end |
.create(attributes = {}) ⇒ ParseResource
Create a ParseResource::Base object.
185 186 187 188 |
# File 'lib/base.rb', line 185 def create(attributes = {}) attributes = HashWithIndifferentAccess.new(attributes) new(attributes).save end |
.destroy_all ⇒ Object
190 191 192 193 194 |
# File 'lib/base.rb', line 190 def destroy_all all.each do |object| object.destroy end end |
.field(name, val = nil) ⇒ Object
Explicitly adds a field to the model.
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/base.rb', line 50 def self.field(name, val=nil) class_eval do define_method(name) do @attributes[name] ? @attributes[name] : @unsaved_attributes[name] end define_method("#{name}=") do |val| @attributes[name] = val @unsaved_attributes[name] = val val end end end |
.fields(*args) ⇒ Object
Add multiple fields in one line. Same as ‘#field`, but accepts multiple args.
66 67 68 |
# File 'lib/base.rb', line 66 def self.fields(*args) args.each {|f| field(f)} end |
.find(id) ⇒ ParseResource
Find a ParseResource::Base object by ID
146 147 148 |
# File 'lib/base.rb', line 146 def find(id) where(:objectId => id).first end |
.first ⇒ Object
Find the first object. Fairly random, not based on any specific condition.
171 172 173 |
# File 'lib/base.rb', line 171 def first Query.new(self).limit(1).first end |
.included(base) ⇒ Object
295 296 297 |
# File 'lib/base.rb', line 295 def self.included(base) base.extend(ClassMethods) end |
.limit(n) ⇒ Object
Limits the number of objects returned
177 178 179 |
# File 'lib/base.rb', line 177 def limit(n) Query.new(self).limit(n) end |
.load!(app_id, master_key) ⇒ Object
Explicitly set Parse.com API keys.
107 108 109 |
# File 'lib/base.rb', line 107 def load!(app_id, master_key) @@settings = {"app_id" => app_id, "master_key" => master_key} end |
.resource ⇒ Object
Creates a RESTful resource sends requests to [base_uri]/
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/base.rb', line 123 def resource if @@settings.nil? path = "config/parse_resource.yml" environment = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : ENV["RACK_ENV"] @@settings = YAML.load(ERB.new(File.new(path).read).result)[environment] end if model_name == "User" #https://parse.com/docs/rest#users-signup base_uri = "https://api.parse.com/1/users" else base_uri = "https://api.parse.com/1/classes/#{model_name}" end #refactor to settings['app_id'] etc app_id = @@settings['app_id'] master_key = @@settings['master_key'] RestClient::Resource.new(base_uri, app_id, master_key) end |
.settings ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/base.rb', line 111 def settings if @@settings.nil? path = "config/parse_resource.yml" environment = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : ENV["RACK_ENV"] @@settings = YAML.load(ERB.new(File.new(path).read).result)[environment] end @@settings end |
Instance Method Details
#attributes ⇒ Object
provides access to @attributes for getting and setting
278 279 280 281 |
# File 'lib/base.rb', line 278 def attributes @attributes ||= self.class.class_attributes @attributes end |
#attributes=(n) ⇒ Object
283 284 285 286 |
# File 'lib/base.rb', line 283 def attributes=(n) @attributes = n @attributes end |
#create ⇒ Object
225 226 227 228 229 230 231 232 233 |
# File 'lib/base.rb', line 225 def create resp = self.resource.post(@unsaved_attributes.to_json, :content_type => "application/json") @attributes.merge!(JSON.parse(resp)) @attributes.merge!(@unsaved_attributes) attributes = HashWithIndifferentAccess.new(attributes) @unsaved_attributes = {} create_setters! self end |
#create_setters! ⇒ Object
Creates getter and setter methods for model fields
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/base.rb', line 72 def create_setters! @attributes.each_pair do |k,v| self.class.send(:define_method, "#{k}=") do |val| if k.is_a?(Symbol) k = k.to_s end @attributes[k.to_s] = val @unsaved_attributes[k.to_s] = val val end self.class.send(:define_method, "#{k}") do if k.is_a?(Symbol) k = k.to_s end if @attributes[k.to_s].is_a?(Hash) && !@attributes[k.to_s]["__type"].nil? case @attributes[k.to_s]["__type"] when "Date" return result = @attributes[k.to_s]["iso"] end end return @attributes[k.to_s] end end end |
#created_at ⇒ Object
291 |
# File 'lib/base.rb', line 291 def created_at; self.createdAt; end |
#destroy ⇒ Object
270 271 272 273 274 275 |
# File 'lib/base.rb', line 270 def destroy self.instance_resource.delete @attributes = {} @unsaved_attributes = {} nil end |
#id ⇒ Object
aliasing for idiomatic Ruby
289 |
# File 'lib/base.rb', line 289 def id; self.objectId rescue nil; end |
#instance_resource ⇒ Object
create RESTful resource for the specific Parse object sends requests to [base_uri]//[objectId]
221 222 223 |
# File 'lib/base.rb', line 221 def instance_resource self.class.resource["#{self.id}"] end |
#new? ⇒ Boolean
210 211 212 |
# File 'lib/base.rb', line 210 def new? !persisted? end |
#persisted? ⇒ Boolean
202 203 204 205 206 207 208 |
# File 'lib/base.rb', line 202 def persisted? if id true else false end end |
#resource ⇒ Object
delegate from Class method
215 216 217 |
# File 'lib/base.rb', line 215 def resource self.class.resource end |
#save ⇒ Object
235 236 237 238 239 240 241 242 243 244 |
# File 'lib/base.rb', line 235 def save if valid? run_callbacks :save do new? ? create : update end else false end rescue false end |
#update(attributes = {}) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/base.rb', line 246 def update(attributes = {}) attributes = HashWithIndifferentAccess.new(attributes) @unsaved_attributes.merge!(attributes) put_attrs = @unsaved_attributes put_attrs.delete('objectId') put_attrs.delete('createdAt') put_attrs.delete('updatedAt') put_attrs = put_attrs.to_json resp = self.instance_resource.put(put_attrs, :content_type => "application/json") @attributes.merge!(JSON.parse(resp)) @attributes.merge!(@unsaved_attributes) @unsaved_attributes = {} create_setters! self end |
#update_attributes(attributes = {}) ⇒ Object
266 267 268 |
# File 'lib/base.rb', line 266 def update_attributes(attributes = {}) self.update(attributes) end |
#updated_at ⇒ Object
293 |
# File 'lib/base.rb', line 293 def updated_at; self.updatedAt rescue nil; end |