Sha256: 8d821e7958c1190734420423e948b415e6d70244d0d8dd355bd16acc483e87e1

Contents?: true

Size: 1.57 KB

Versions: 24

Compression:

Stored size: 1.57 KB

Contents

module Neo4j
  module Rails

    # This method is typically used in an Rails ActionController as a filter method.
    # It will guarantee that an transaction is create before your action is called,
    # and that the transaction is finished after your action is called.
    #
    # Example:
    #  class MyController < ActionController::Base
    #      around_filter Neo4j::Rails::Transaction, :only => [:edit, :delete]
    #      ...
    #  end
    class Transaction
      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 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
            ret = yield self
          rescue
            fail
            raise
          ensure
            finish
          end
          ret
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
neo4j-1.2.1-java lib/neo4j/rails/transaction.rb
neo4j-1.2.0-java lib/neo4j/rails/transaction.rb
neo4j-1.1.4-java lib/neo4j/rails/transaction.rb
neo4j-1.1.3-java lib/neo4j/rails/transaction.rb
neo4j-1.1.2-java lib/neo4j/rails/transaction.rb
neo4j-1.1.1-java lib/neo4j/rails/transaction.rb
neo4j-1.1.0-java lib/neo4j/rails/transaction.rb
neo4j-1.1.0.beta.3-java lib/neo4j/rails/transaction.rb
neo4j-1.1.0.beta.2-java lib/neo4j/rails/transaction.rb
neo4j-1.1.0.beta.1-java lib/neo4j/rails/transaction.rb
neo4j-1.0.0-java lib/neo4j/rails/transaction.rb
neo4j-1.0.0.beta.32-java lib/neo4j/rails/transaction.rb
neo4j-1.0.0.beta.31-java lib/neo4j/rails/transaction.rb
neo4j-1.0.0.beta.30-java lib/neo4j/rails/transaction.rb
neo4j-1.0.0.beta.29-java lib/neo4j/rails/transaction.rb
neo4j-1.0.0.beta.28-java lib/neo4j/rails/transaction.rb
neo4j-1.0.0.beta.27-java lib/neo4j/rails/transaction.rb
neo4j-1.0.0.beta.26-java lib/neo4j/rails/transaction.rb
neo4j-1.0.0.beta.25-java lib/neo4j/rails/transaction.rb
neo4j-1.0.0.beta.24-java lib/neo4j/rails/transaction.rb