Sha256: 9f9a191ec448dc7b84aeb6f924ca21dda4bc617254ff4a9672241e1abac096c3

Contents?: true

Size: 1.64 KB

Versions: 26

Compression:

Stored size: 1.64 KB

Contents

module Foobara
  module Persistence
    class EntityBase
      class Transaction
        module Concerns
          module TransactionTracking
            include Concern

            module ClassMethods
              def install!
                Transaction::StateMachine.register_transition_callback(:after,
                                                                       transition: :open) do |state_machine:, **|
                  transaction = state_machine.owner
                  Transaction.open_transactions << transaction
                end

                Transaction::StateMachine.register_transition_callback(:after, to: :closed) do |state_machine:, **|
                  transaction = state_machine.owner
                  Transaction.open_transactions.delete(transaction)
                end
              end

              def reset_all
                @open_transactions = nil
              end

              def open_transactions
                @open_transactions ||= Set.new
              end

              def open_transaction_for(record)
                # let's check the current_transaction first since that usually will match
                tx = Persistence.current_transaction(record)

                if tx&.tracking?(record)
                  return tx
                else
                  entity_base = record.class.entity_base

                  tx = open_transactions.find do |transaction|
                    transaction.entity_base == entity_base && transaction.tracking?(record)
                  end
                end

                tx
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
foobara-0.0.26 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.25 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.24 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.23 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.22 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.21 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.20 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.19 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.18 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.17 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.16 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.15 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.14 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.13 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.12 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.11 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.10 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.9 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.8 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb
foobara-0.0.7 projects/persistence/src/entity_base/transaction/concerns/transaction_tracking.rb