lib/neo4j/rails/transaction.rb in neo4j-1.0.0.beta.4 vs lib/neo4j/rails/transaction.rb in neo4j-1.0.0.beta.5
- old
+ new
@@ -9,21 +9,58 @@
# class MyController < ActionController::Base
# around_filter Neo4j::Rails::Transaction, :only => [:edit, :delete]
# ...
# end
class Transaction
- def self.filter(*)
- begin
- tx = Neo4j::Transaction.new
- yield
- tx.success
- rescue Exception
- tx.failure unless tx.nil?
- raise
- ensure
- tx.finish unless tx.nil?
+ class << self
+ def new
+ finish if Thread.current[:neo4j_transaction]
+ Thread.current[:neo4j_transaction] = Neo4j::Transaction.new
end
+
+ def current
+ Thread.current[:neo4j_transaction]
+ end
+
+ def running?
+ Thread.current[:neo4j_transaction] != nil
+ end
+
+ def fail?
+ Thread.current[:neo4j_transaction_fail] != nil
+ end
+
+ def fail
+ Thread.current[:neo4j_transaction_fail] = true
+ end
+
+ def success
+ Thread.current[:neo4j_transaction_fail] = nil
+ end
+
+ def finish
+ tx = Thread.current[:neo4j_transaction]
+ tx.success unless Thread.current[:neo4j_transaction_fail]
+ tx.finish
+ Thread.current[:neo4j_transaction] = nil
+ Thread.current[:neo4j_transaction_fail] = nil
+ end
+
+ def filter(*, &block)
+ run &block
+ end
+
+ def run
+ begin
+ new
+ yield
+ rescue
+ fail
+ raise
+ ensure
+ finish
+ end
+ end
end
end
end
-
end
\ No newline at end of file