Permalink
Browse files

get the time interval fixed for rails and non-rails setups

  • Loading branch information...
1 parent bd8c8ff commit fc388067c51842ca14d7707c889043b8c1c0b991 @mkristian committed Nov 26, 2012
Showing with 28 additions and 5 deletions.
  1. +2 −0 Rakefile
  2. +2 −2 lib/ixtlan/optimistic/active_record.rb
  3. +24 −3 lib/ixtlan/optimistic/data_mapper.rb
View
@@ -1,3 +1,4 @@
+# -*- mode: ruby -*-
task :default => [ :spec ]
task :spec do
@@ -9,3 +10,4 @@ task :spec do
Dir['spec/*_spec.rb'].each { |f| require File.basename(f.sub(/.rb$/, '')) }
end
+# vim: syntax=Ruby
@@ -16,10 +16,10 @@ def self.optimistic_find(updated_at, *args)
# TODO make it work with different PKs
result = first(:conditions => ["id = ? and updated_at <= ? and updated_at >= ?", args[0], updated_at_date + 0.0005, updated_at_date - 0.0005])
p result
- raise ObjectStaleException.new "#{self.class} with ID=#{args[0]} is stale" unless result
+ raise ObjectStaleException.new "#{self} with ID=#{args[0]} is stale" unless result
result
else
- raise ObjectStaleException.new "no 'updated_at' given. could not dind #{self.class} with ID=#{args[0]}."
+ raise ObjectStaleException.new "no 'updated_at' given. could not dind #{self} with ID=#{args[0]}."
end
end
@@ -10,19 +10,40 @@ 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
- first(:id => args[0], :updated_at.gte => updated_at_date - 0.0005, :updated_at.lte => updated_at_date + 0.0005)
+ 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)
end
end
def self.optimistic_get!(updated_at, *args)
if updated_at
result = self.optimistic_get(updated_at, *args)
- raise ObjectStaleException.new "#{self.class} with ID=#{args[0]} is stale" unless result
+ 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 #{self.class} with ID=#{args[0]}."
+ raise ObjectStaleException.new "no 'updated_at' given. could not dind #{signature(*args)}."
end
end
+
+ private
+ def self.signature(*args)
+ s = ""
+ index = 0
+ properties.key.each do |k|
+ s += " @#{k.name}=#{args[index]}"
+ index += 1
+ end
+ "#{self}(#{s.strip})"
+ end
end
end

0 comments on commit fc38806

Please sign in to comment.