medea 0.3.1 → 0.3.2

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.
@@ -10,7 +10,8 @@ module Medea
10
10
  :format => :search,
11
11
  :filters => {
12
12
  :VERSION0 => nil,
13
- :FILTER => {:HTTP_X_LIST => list_name}}
13
+ :FILTER => {:HTTP_X_LIST => list_name,
14
+ :HTTP_X_ACTION => :POST}}
14
15
 
15
16
  self.filters[:FILTER][:HTTP_X_CLASS] = list_class.name if list_type == :value
16
17
  @list_name = list_name
@@ -24,7 +24,7 @@ module Medea
24
24
  #create a JasonDeferredQuery with no conditions, other than HTTP_X_CLASS=self.name
25
25
  #if mode is set to :eager, we create the JasonDeferredQuery, invoke it's execution and then return it
26
26
  def JasonObject.all(mode=:lazy)
27
- JasonDeferredQuery.new :class => self, :filters => {:VERSION0 => nil, :FILTER => {:HTTP_X_CLASS => self}}
27
+ JasonDeferredQuery.new :class => self, :filters => {:VERSION0 => nil, :FILTER => {:HTTP_X_CLASS => self, :HTTP_X_ACTION => :POST}}
28
28
  end
29
29
 
30
30
  #returns the JasonObject by directly querying the URL
@@ -58,7 +58,7 @@ module Medea
58
58
  #the resolve method takes a key and returns the JasonObject that has that key
59
59
  #This is useful when you have the key, but not the class
60
60
  def JasonObject.resolve(key, mode=:lazy)
61
- q = JasonDeferredQuery.new :filters => {:VERSION0 => nil, :FILTER => {:HTTP_X_KEY => key}}
61
+ q = JasonDeferredQuery.new :filters => {:VERSION0 => nil, :FILTER => {:HTTP_X_KEY => key, :HTTP_X_ACTION => :POST}}
62
62
  q.filters[:FILTER] ||= {}
63
63
  q.filters[:FILTER][:HTTP_X_KEY] = key
64
64
  resp = JSON.parse(RestClient.get(q.to_url))
@@ -129,7 +129,7 @@ module Medea
129
129
 
130
130
  def jason_key
131
131
  #Generate a random UUID for this object.
132
- #since jason urls must start with a letter, we'll use the first letter of the class name
132
+ #since jason urls must start with a letter, we'll use the first letter of the class name
133
133
  @__id ||= "#{self.class.name[0].chr.downcase}#{UUIDTools::UUID::random_create.to_s}"
134
134
  end
135
135
 
@@ -185,43 +185,7 @@ module Medea
185
185
  #no changes? no save!
186
186
  return if @__jason_state == :stale or @__jason_state == :ghost
187
187
 
188
-
189
- payload = self.to_json
190
- post_headers = {
191
- :content_type => 'application/json',
192
- "X-KEY" => self.jason_key,
193
- "X-CLASS" => self.class.name
194
- #also want to add the eTag here!
195
- #may also want to add any other indexable fields that the user specifies?
196
- }
197
- post_headers["IF-MATCH"] = @__jason_etag if @__jason_state == :dirty
198
-
199
- if self.class.owned
200
- #the parent object needs to be defined!
201
- raise "#{self.class.name} cannot be saved without setting a parent and list!" unless self.jason_parent && self.jason_parent_list
202
- end
203
-
204
- post_headers["X-PARENT"] = self.jason_parent.jason_key if self.jason_parent
205
- post_headers["X-LIST"] = self.jason_parent_list if self.jason_parent_list
206
-
207
-
208
- url = JasonDB::db_auth_url + self.class.name + "/" + self.jason_key
209
-
210
- #puts "Posted to JasonDB!"
211
-
212
- #puts "Saving to #{url}"
213
- response = RestClient.post url, payload, post_headers
214
-
215
- if response.code == 201
216
- #save successful!
217
- #store the new eTag for this object
218
- #puts response.raw_headers
219
- #@__jason_etag = response.headers[:location] + ":" + response.headers[:content_md5]
220
- else
221
- raise "POST failed! Could not save object"
222
- end
223
-
224
- @__jason_state = :stale
188
+ persist_changes :post
225
189
  end
226
190
 
227
191
  def delete! cascade=false
@@ -237,9 +201,7 @@ module Medea
237
201
  end
238
202
  end
239
203
  end
240
- url = "#{JasonDB::db_auth_url}#{self.class.name}/#{self.jason_key}"
241
- response = RestClient.delete url
242
- raise "DELETE failed!" unless response.code == 201
204
+ persist_changes :delete
243
205
  end
244
206
 
245
207
  #end object persistence
@@ -272,10 +234,47 @@ module Medea
272
234
  @__jason_state = :stale
273
235
  end
274
236
 
275
- def lazy_load meta
276
- #TODO Implement lazy load
277
-
278
- @__jason_state = :ghost
237
+ def persist_changes method = :post
238
+ payload = self.to_json
239
+
240
+ post_headers = {
241
+ :content_type => 'application/json',
242
+ "X-KEY" => self.jason_key,
243
+ "X-CLASS" => self.class.name
244
+ #also want to add the eTag here!
245
+ #may also want to add any other indexable fields that the user specifies?
246
+ }
247
+ post_headers["IF-MATCH"] = @__jason_etag if @__jason_state == :dirty
248
+
249
+ if self.class.owned
250
+ #the parent object needs to be defined!
251
+ raise "#{self.class.name} cannot be saved without setting a parent and list!" unless self.jason_parent && self.jason_parent_list
252
+ end
253
+
254
+ post_headers["X-PARENT"] = self.jason_parent.jason_key if self.jason_parent
255
+ post_headers["X-LIST"] = self.jason_parent_list if self.jason_parent_list
256
+
257
+ url = JasonDB::db_auth_url + self.class.name + "/" + self.jason_key
258
+
259
+ #puts "Saving to #{url}"
260
+ if method == :post
261
+ response = RestClient.post(url, payload, post_headers)
262
+ elsif method == :delete
263
+ response = RestClient.delete(url, post_headers)
264
+ else
265
+ raise "Unknown method '#{method.to_s}'"
266
+ end
267
+
268
+ if response.code == 201
269
+ #save successful!
270
+ #store the new eTag for this object
271
+ #puts response.raw_headers
272
+ #@__jason_etag = response.headers[:location] + ":" + response.headers[:content_md5]
273
+ else
274
+ raise "#{method.to_s.upcase} failed! Could not persist changes"
275
+ end
276
+
277
+ @__jason_state = :stale
279
278
  end
280
279
 
281
280
  end
data/lib/medea/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Medea
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -29,7 +29,7 @@ describe "JasonObject" do
29
29
  retrieved_user.name.should eq(@user.name)
30
30
  end
31
31
 
32
- it "should track it's state" do
32
+ it "should track its state" do
33
33
  @user.jason_state.should eq(:new)
34
34
  @user.save!
35
35
  @user.name = "Freddy"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: medea
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Jensen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-06 00:00:00 +11:00
18
+ date: 2011-01-07 00:00:00 +11:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency