Skip to content
Browse files

refactored and raise stale exception on optimistic_get if object foun…

…d and stale
  • Loading branch information...
1 parent 341402a commit 7653b196ec4d05dce255573b0e75d18132ea9cd7 @mkristian committed Dec 7, 2012
Showing with 72 additions and 42 deletions.
  1. +45 −35 lib/ixtlan/optimistic/data_mapper.rb
  2. +27 −7 spec/datamapper_spec.rb
View
80 lib/ixtlan/optimistic/data_mapper.rb
@@ -1,53 +1,63 @@
+#
+# Copyright (C) 2012 mkristian
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy of
+# this software and associated documentation files (the "Software"), to deal in
+# the Software without restriction, including without limitation the rights to
+# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+# the Software, and to permit persons to whom the Software is furnished to do so,
+# subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+#
require 'ixtlan/optimistic/object_stale_exception'
module Ixtlan
module Optimistic
module DataMapper
def self.included(base)
base.class_eval do
-
- def self.optimistic_get(updated_at, *args)
- if updated_at
- updated_at_date = new(:updated_at => updated_at).updated_at
- # TODO make it work with different PKs
- cond = {:updated_at.gte => updated_at_date.ago(1), :updated_at.lte => updated_at_date.next }
- index = 0
- properties.key.each do |k|
- cond[k.name] = args[index]
- index += 1
- end
- first(cond)
+ private
+
+ def self.__check( updated_at )
+ unless updated_at
+ raise ObjectStaleException.new "no 'updated_at' given. could not find #{signature(*args)}."
end
end
-
- def self.optimistic_get!(updated_at, *args)
- if updated_at
- result = self.optimistic_get(updated_at, *args)
- unless result
- # TODO make the whole thing with one query
- self.get!(*args) # check existence
- raise ObjectStaleException.new "#{signature(*args)} is stale"
- end
- result
- else
- raise ObjectStaleException.new "no 'updated_at' given. could not dind #{signature(*args)}."
+
+ def self.__check_stale( updated_at, result )
+ updated_at_date = new(:updated_at => updated_at).updated_at
+ if updated_at_date != result.updated_at
+ raise ObjectStaleException.new "#{self.class} with key #{result.key} is stale for updated at #{updated_at}."
end
+ result
end
- private
- def self.signature(*args)
- s = ""
- index = 0
- properties.key.each do |k|
- s += " @#{k.name}=#{args[index]}"
- index += 1
+ public
+
+ def self.optimistic_get(updated_at, *args)
+ __check( updated_at )
+ result = get( *args )
+ if result
+ __check_stale( updated_at, result )
end
- "#{self}(#{s.strip})"
+ end
+
+ def self.optimistic_get!(updated_at, *args)
+ __check( updated_at )
+ result = get!( *args )
+ __check_stale( updated_at, result )
end
end
-
end
-
end
end
-end
+end
View
34 spec/datamapper_spec.rb
@@ -23,16 +23,36 @@ class A
subject { A.create :name => 'huffalump' }
- it 'should load' do
- A.optimistic_get!(subject.updated_at.to_s, subject.id).must_equal subject
- end
+ describe "#optimistic_get" do
+
+ it 'should load' do
+ A.optimistic_get(subject.updated_at.to_s, subject.id).must_equal subject
+ end
+
+ it 'should fail with stale exception' do
+ lambda { A.optimistic_get((subject.updated_at - 1000).to_s, subject.id) }.must_raise Ixtlan::Optimistic::ObjectStaleException
+ end
+
+ it 'should fail with nil' do
+ A.optimistic_get(subject.updated_at.to_s, subject.id + 987).must_be_nil
+ end
- it 'should fail with nil' do
- A.optimistic_get((subject.updated_at - 1000).to_s, subject.id).must_be_nil
end
- it 'should fail with exception' do
- lambda { A.optimistic_get!((subject.updated_at - 1000).to_s, subject.id) }.must_raise Ixtlan::Optimistic::ObjectStaleException
+ describe "#optimistic_get!" do
+
+ it 'should load' do
+ A.optimistic_get!(subject.updated_at.to_s, subject.id).must_equal subject
+ end
+
+ it 'should fail with not-found exception' do
+ lambda { A.optimistic_get!(subject.updated_at.to_s, subject.id + 987) }.must_raise DataMapper::ObjectNotFoundError
+ end
+
+ it 'should fail with stale exception' do
+ lambda { A.optimistic_get!((subject.updated_at - 1000).to_s, subject.id) }.must_raise Ixtlan::Optimistic::ObjectStaleException
+ end
+
end
end

0 comments on commit 7653b19

Please sign in to comment.
Something went wrong with that request. Please try again.