parse_resource 1.6.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,286 +0,0 @@
1
- require "rubygems"
2
- require "bundler/setup"
3
- require "active_model"
4
- require "erb"
5
- require "rest-client"
6
- require "json"
7
- require "active_support/hash_with_indifferent_access"
8
-
9
- class ParseResource
10
- # ParseResource provides an easy way to use Ruby to interace with a Parse.com backend
11
- # Usage:
12
- # class Post < ParseResource
13
- # fields :title, :author, :body
14
- # end
15
-
16
- include ActiveModel::Validations
17
- include ActiveModel::Conversion
18
- include ActiveModel::AttributeMethods
19
- extend ActiveModel::Naming
20
- extend ActiveModel::Callbacks
21
- HashWithIndifferentAccess = ActiveSupport::HashWithIndifferentAccess
22
-
23
- # define_model_callbacks :initialize, :find, :only => :after
24
- define_model_callbacks :save, :create, :update, :destroy
25
-
26
-
27
- # Instantiates a ParseResource object
28
- #
29
- # @params [Hash], [Boolean] a `Hash` of attributes and a `Boolean` that should be false only if the object already exists
30
- # @return [ParseResource] an object that subclasses `Parseresource`
31
- def initialize(attributes = {}, new=true)
32
- attributes = HashWithIndifferentAccess.new(attributes)
33
- if new
34
- @unsaved_attributes = attributes
35
- else
36
- @unsaved_attributes = {}
37
- end
38
- self.attributes = {}
39
- self.attributes.merge!(attributes)
40
- self.attributes unless self.attributes.empty?
41
- create_setters!
42
- end
43
-
44
- # Explicitly adds a field to the model.
45
- #
46
- # @param [Symbol] name the name of the field, eg `:author`.
47
- # @param [Boolean] val the return value of the field. Only use this within the class.
48
- def self.field(name, val=nil)
49
- class_eval do
50
- define_method(name) do
51
- @attributes[name] ? @attributes[name] : @unsaved_attributes[name]
52
- end
53
- define_method("#{name}=") do |val|
54
- @attributes[name] = val
55
- @unsaved_attributes[name] = val
56
- val
57
- end
58
- end
59
- end
60
-
61
- # Add multiple fields in one line. Same as `#field`, but accepts multiple args.
62
- #
63
- # @param [Array] *args an array of `Symbol`s, `eg :author, :body, :title`.
64
- def self.fields(*args)
65
- args.each {|f| field(f)}
66
- end
67
-
68
- # Creates getter and setter methods for model fields
69
- #
70
- def create_setters!
71
- @attributes.each_pair do |k,v|
72
- self.class.send(:define_method, "#{k}=") do |val|
73
- if k.is_a?(Symbol)
74
- k = k.to_s
75
- end
76
- @attributes[k.to_s] = val
77
- @unsaved_attributes[k.to_s] = val
78
- val
79
- end
80
- self.class.send(:define_method, "#{k}") do
81
- if k.is_a?(Symbol)
82
- k = k.to_s
83
- end
84
-
85
- @attributes[k.to_s]
86
- end
87
- end
88
- end
89
-
90
- class << self
91
- def has_one(child_name)
92
- class_eval do
93
-
94
- define_method("#{child_name}") do
95
- child_name
96
- end
97
-
98
- define_method("#{child_name}=") do |child_object|
99
- [child_object, child_name]
100
- end
101
-
102
- end
103
- end
104
-
105
- def belongs_to(name)
106
- class_eval do
107
-
108
- define_method("#{parent_name}") do
109
- name
110
- end
111
-
112
- define_method("#{parent_name}=") do |parent_object|
113
- [parent_name, parent_object]
114
- end
115
-
116
- end
117
- end
118
-
119
-
120
- @@settings ||= nil
121
-
122
- # Explicitly set Parse.com API keys.
123
- #
124
- # @param [String] app_id the Application ID of your Parse database
125
- # @param [String] master_key the Master Key of your Parse database
126
- def load!(app_id, master_key)
127
- @@settings = {"app_id" => app_id, "master_key" => master_key}
128
- end
129
-
130
- # Creates a RESTful resource
131
- # sends requests to [base_uri]/[classname]
132
- #
133
- def resource
134
- if @@settings.nil?
135
- path = "config/parse_resource.yml"
136
- environment = defined?(Rails) && Rails.respond_to?(:env) ? Rails.env : ENV["RACK_ENV"]
137
- @@settings = YAML.load(ERB.new(File.new(path).read).result)[environment]
138
- end
139
- base_uri = "https://api.parse.com/1/classes/#{model_name}"
140
- app_id = @@settings['app_id']
141
- master_key = @@settings['master_key']
142
- RestClient::Resource.new(base_uri, app_id, master_key)
143
- end
144
-
145
- # Find a ParseResource object by ID
146
- #
147
- # @param [String] id the ID of the Parse object you want to find.
148
- # @return [ParseResource] an object that subclasses ParseResource.
149
- def find(id)
150
- where(:objectId => id).first
151
- end
152
-
153
- # Find a ParseResource object by a `Hash` of conditions.
154
- #
155
- # @param [Hash] parameters a `Hash` of conditions.
156
- # @return [Array] an `Array` of objects that subclass `ParseResource`.
157
- def where(parameters)
158
- resp = resource.get(:params => {:where => parameters.to_json})
159
- results = JSON.parse(resp)['results']
160
- results.map {|r| model_name.constantize.new(r, false)}
161
- end
162
-
163
- # Find all ParseResource objects for that model.
164
- #
165
- # @return [Array] an `Array` of objects that subclass `ParseResource`.
166
- def all
167
- resp = resource.get
168
- results = JSON.parse(resp)['results']
169
- results.map {|r| model_name.constantize.new(r, false)}
170
- end
171
-
172
- # Create a ParseResource object.
173
- #
174
- # @param [Hash] attributes a `Hash` of attributes
175
- # @return [ParseResource] an object that subclasses `ParseResource`. Or returns `false` if object fails to save.
176
- def create(attributes = {})
177
- attributes = HashWithIndifferentAccess.new(attributes)
178
- new(attributes).save
179
- end
180
-
181
- # Find the first object. Fairly random, not based on any specific condition.
182
- #
183
- def first
184
- all.first
185
- end
186
-
187
- def class_attributes
188
- @class_attributes ||= {}
189
- end
190
-
191
- end
192
-
193
- def persisted?
194
- if id
195
- true
196
- else
197
- false
198
- end
199
- end
200
-
201
- def new?
202
- !persisted?
203
- end
204
-
205
- # delegate from Class method
206
- def resource
207
- self.class.resource
208
- end
209
-
210
- # create RESTful resource for the specific Parse object
211
- # sends requests to [base_uri]/[classname]/[objectId]
212
- def instance_resource
213
- self.class.resource["#{self.id}"]
214
- end
215
-
216
- def create
217
- resp = self.resource.post(@unsaved_attributes.to_json, :content_type => "application/json")
218
- @attributes.merge!(JSON.parse(resp))
219
- @attributes.merge!(@unsaved_attributes)
220
- attributes = HashWithIndifferentAccess.new(attributes)
221
- @unsaved_attributes = {}
222
- create_setters!
223
- self
224
- end
225
-
226
- def save
227
- if valid?
228
- run_callbacks :save do
229
- new? ? create : update
230
- end
231
- else
232
- false
233
- end
234
- rescue false
235
- end
236
-
237
- def update(attributes = {})
238
- attributes = HashWithIndifferentAccess.new(attributes)
239
- @unsaved_attributes.merge!(attributes)
240
-
241
- put_attrs = @unsaved_attributes
242
- put_attrs.delete('objectId')
243
- put_attrs.delete('createdAt')
244
- put_attrs.delete('updatedAt')
245
- put_attrs = put_attrs.to_json
246
-
247
- resp = self.instance_resource.put(put_attrs, :content_type => "application/json")
248
-
249
- @attributes.merge!(JSON.parse(resp))
250
- @attributes.merge!(@unsaved_attributes)
251
- @unsaved_attributes = {}
252
- create_setters!
253
-
254
- self
255
- end
256
-
257
- def update_attributes(attributes = {})
258
- self.update(attributes)
259
- end
260
-
261
- def destroy
262
- self.instance_resource.delete
263
- @attributes = {}
264
- @unsaved_attributes = {}
265
- nil
266
- end
267
-
268
- # provides access to @attributes for getting and setting
269
- def attributes
270
- @attributes ||= self.class.class_attributes
271
- @attributes
272
- end
273
-
274
- def attributes=(n)
275
- @attributes = n
276
- @attributes
277
- end
278
-
279
- # aliasing for idiomatic Ruby
280
- def id; self.objectId rescue nil; end
281
-
282
- def created_at; self.createdAt; end
283
-
284
- def updated_at; self.updatedAt rescue nil; end
285
-
286
- end