lib/ruote/exp/ro_persist.rb in ruote-2.1.11 vs lib/ruote/exp/ro_persist.rb in ruote-2.2.0
- old
+ new
@@ -1,7 +1,7 @@
#--
-# Copyright (c) 2005-2010, John Mettraux, jmettraux@gmail.com
+# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com
#
# 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
@@ -40,36 +40,35 @@
#
def initial_persist
r = @context.storage.put(@h, :update_rev => true)
- raise(
- "initial_persist failed for " +
- "#{Ruote.to_storage_id(h.fei)} #{tree.first}"
- ) if r != nil
+ #t = Thread.current.object_id.to_s[-3..-1]
+ #puts "+ per #{h.fei['expid']} #{tree[0]} r#{h._rev} t#{t} -> #{r.class}"
+ #Ruote.p_caller('+ per')
- nil
+ raise_or_return('initial_persist failed', r)
end
def try_persist
r = @context.storage.put(@h)
#t = Thread.current.object_id.to_s[-3..-1]
- #puts "+ per #{h.fei['expid']} #{tree.first} #{h._rev} #{t} -> #{r.class}"
- #Ruote.p_caller('+ per') #if r != nil || h.fei['expid'] == '0_0'
+ #puts "+ per #{h.fei['expid']} #{tree[0]} r#{h._rev} t#{t} -> #{r.class}"
+ #Ruote.p_caller('+ per')
r
end
def try_unpersist
r = @context.storage.delete(@h)
#t = Thread.current.object_id.to_s[-3..-1]
- #puts "- unp #{h.fei['expid']} #{tree.first} #{h._rev} #{t} -> #{r.class}"
- #Ruote.p_caller('- unp') #if r != nil || h.fei['expid'] == '0_0'
+ #puts "- unp #{h.fei['expid']} #{tree[0]} r#{h._rev} t#{t} -> #{r.class}"
+ #Ruote.p_caller('- unp')
return r if r
#if h.has_error
err = @context.storage.get('errors', "err_#{Ruote.to_storage_id(h.fei)}")
@@ -79,59 +78,76 @@
# since it will now be gone, no need to keep track of its errors
nil
end
- #--
- # duplication ahead
- #++
-
def persist_or_raise
- r = try_persist
-
- raise(
- "persist failed for " +
- "#{Ruote.to_storage_id(h.fei)} #{tree.first} #{r.class}"
- ) if r
+ p_or_raise(true)
end
def unpersist_or_raise
- r = try_unpersist
-
- raise(
- "unpersist failed for " +
- "#{Ruote.to_storage_id(h.fei)} #{tree.first} #{r.class}"
- ) if r
+ p_or_raise(false)
end
- alias :persist :persist_or_raise
- alias :unpersist :unpersist_or_raise
+ alias persist persist_or_raise
+ alias unpersist unpersist_or_raise
def do_persist
- do_p(:persist)
+ do_p(true)
end
def do_unpersist
- do_p(:unpersist)
+ do_p(false)
end
protected
- def do_p (pers)
+ def raise_or_return(msg, r)
- case r = self.send("try_#{pers}")
+ msg = msg.is_a?(String) ?
+ msg : (msg ? 'persist' : 'unpersist') + ' failed'
+
+ raise(
+ "#{msg} for " +
+ "#{Ruote.to_storage_id(h.fei)} #{tree[0]} #{tree[1].inspect} " +
+ 'r(' + (r == true ? 'gone' : "rev : #{r['_rev']}") + ')'
+ ) if r
+
+ r
+ end
+
+ # Does persist or unpersist, returns nothing in particular.
+ #
+ # Will raise a runtime error if it fails (ie if it happens, there
+ # is something wrong with the storage implementation or the engine).
+ #
+ def p_or_raise(pers)
+
+ r = pers ? try_persist : try_unpersist
+
+ raise_or_return(pers, r)
+ end
+
+ # Does persist or unpersist, if successful then return true. If the
+ # expression is gone, return false.
+ # If there is a 'fresher' version of the expression, re-attempt and return
+ # false.
+ #
+ def do_p(pers)
+
+ case r = pers ? try_persist : try_unpersist
when true
- false # don't go on
+ false # do not go on
when Hash
self.h = r
self.send("do_#{@msg['action']}", @msg)
- false # don't go on
+ false # do not go on
else
- true # success, please go on
+ true # success, do go on
end
end
end
end