Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

get activerecord and datamapper use the samecheck code

  • Loading branch information...
commit 44a514650184bdcb7f6e88ab3bc6c82280bb1f9d 1 parent 1553e59
Christian Meier authored
23 lib/ixtlan/optimistic/active_record.rb
@@ -19,32 +19,23 @@
19 19 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 20 #
21 21 require 'ixtlan/optimistic/object_stale_exception'
  22 +require 'ixtlan/optimistic/stale_check'
22 23 module Ixtlan
23 24 module Optimistic
24 25 module ActiveRecord
25 26
26 27 def self.included(base)
  28 + base.extend StaleCheck
27 29 base.class_eval do
28   -
29 30 def self.optimistic_find(updated_at, *args)
30   - if updated_at
31   - dummy = self.new
32   - dummy.updated_at = updated_at
33   - updated_at_date = dummy.updated_at
34   - # try different ways to use the date
35   - # TODO maybe there is a nicer way ??
36   - # TODO make it work with different PKs
37   - result = first(:conditions => ["id = ? and updated_at <= ? and updated_at >= ?", args[0], updated_at_date + 0.0005, updated_at_date - 0.0005])
38   -p result
39   - raise ObjectStaleException.new "#{self} with ID=#{args[0]} is stale" unless result
40   - result
41   - else
42   - raise ObjectStaleException.new "no 'updated_at' given. could not dind #{self} with ID=#{args[0]}."
  31 + __check( updated_at )
  32 + result = find( *args )
  33 + if result
  34 + __check_stale( updated_at, result )
43 35 end
44 36 end
45   -
46 37 end
47 38 end
48 39 end
49 40 end
50   -end
  41 +end
21 lib/ixtlan/optimistic/data_mapper.rb
@@ -19,29 +19,14 @@
19 19 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 20 #
21 21 require 'ixtlan/optimistic/object_stale_exception'
  22 +require 'ixtlan/optimistic/stale_check'
22 23 module Ixtlan
23 24 module Optimistic
24 25 module DataMapper
25 26
26 27 def self.included(base)
  28 + base.extend StaleCheck
27 29 base.class_eval do
28   - private
29   -
30   - def self.__check( updated_at )
31   - unless updated_at
32   - raise ObjectStaleException.new "no 'updated_at' given. could not find #{signature(*args)}."
33   - end
34   - end
35   -
36   - def self.__check_stale( updated_at, result )
37   - updated_at_date = new(:updated_at => updated_at).updated_at
38   - if updated_at_date != result.updated_at
39   - raise ObjectStaleException.new "#{self.class} with key #{result.key} is stale for updated at #{updated_at}."
40   - end
41   - result
42   - end
43   -
44   - public
45 30
46 31 def self.optimistic_get(updated_at, *args)
47 32 __check( updated_at )
@@ -60,4 +45,4 @@ def self.optimistic_get!(updated_at, *args)
60 45 end
61 46 end
62 47 end
63   -end
  48 +end
44 lib/ixtlan/optimistic/stale_check.rb
... ... @@ -0,0 +1,44 @@
  1 +#
  2 +# Copyright (C) 2012 mkristian
  3 +#
  4 +# Permission is hereby granted, free of charge, to any person obtaining a copy of
  5 +# this software and associated documentation files (the "Software"), to deal in
  6 +# the Software without restriction, including without limitation the rights to
  7 +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8 +# the Software, and to permit persons to whom the Software is furnished to do so,
  9 +# subject to the following conditions:
  10 +#
  11 +# The above copyright notice and this permission notice shall be included in all
  12 +# copies or substantial portions of the Software.
  13 +#
  14 +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15 +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16 +# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17 +# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18 +# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19 +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20 +#
  21 +require 'ixtlan/optimistic/object_stale_exception'
  22 +module Ixtlan
  23 + module Optimistic
  24 + module StaleCheck
  25 +
  26 + def __check( updated_at )
  27 + unless updated_at
  28 + raise ObjectStaleException.new "no 'updated_at' given. could not find #{signature(*args)}."
  29 + end
  30 + end
  31 +
  32 + def __check_stale( updated_at, result )
  33 + if updated_at.is_a?( String )
  34 + updated_at = DateTime.parse( updated_at.sub(/[.][0-9]+/, '') )
  35 + end
  36 + if updated_at != result.updated_at && updated_at.strftime("%Y:%m:%d %H:%M:%S") != result.updated_at.strftime("%Y:%m:%d %H:%M:%S")
  37 +
  38 + raise ObjectStaleException.new "#{self.class} with key #{result.key} is stale for updated at #{updated_at}."
  39 + end
  40 + result
  41 + end
  42 + end
  43 + end
  44 +end

0 comments on commit 44a5146

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