diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2015-03-02 09:16:50 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2015-03-02 09:17:03 -0800 |
commit | 393e65b4170608593ad82377a9eadc918e85698d (patch) | |
tree | 7d495fbd821981ba3f6bdd9dc7a8f24165b1af2b /activerecord | |
parent | ec51c3fedd16b561d096dcc1a6705fdc02ab7666 (diff) | |
download | rails-393e65b4170608593ad82377a9eadc918e85698d.tar.gz rails-393e65b4170608593ad82377a9eadc918e85698d.tar.bz2 rails-393e65b4170608593ad82377a9eadc918e85698d.zip |
mutate the transaction object to reflect state
this lets us keep singleton instances of "state" values and precalculate
return values of things like `finalized?` and `completed?`.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/transaction.rb | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb index a51dda5483..081ec1528a 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb @@ -1,34 +1,30 @@ module ActiveRecord module ConnectionAdapters class TransactionState - VALID_STATES = Set.new([:committed, :rolledback, nil]) + def finalized?; false; end + def committed?; false; end + def rolledback?; false; end + def completed?; false; end - def initialize(state = nil) - @state = state + class Committed < TransactionState + def finalized?; true; end + def committed?; true; end + def completed?; true; end end - def finalized? - @state - end - - def committed? - @state == :committed + class RolledBack < TransactionState + def finalized?; true; end + def rolledback?; true; end + def completed?; true; end end - def rolledback? - @state == :rolledback - end + ROLLED_BACK = RolledBack.new + COMMITTED = Committed.new + NULL = TransactionState.new - def completed? - committed? || rolledback? - end - - def set_state(state) - unless VALID_STATES.include?(state) - raise ArgumentError, "Invalid transaction state: #{state}" - end - @state = state - end + def self.rolled_back; ROLLED_BACK; end + def self.committed; COMMITTED; end + def self.null; NULL; end end class NullTransaction #:nodoc: @@ -46,7 +42,7 @@ module ActiveRecord def initialize(connection, options) @connection = connection - @state = TransactionState.new + @state = TransactionState.null @records = [] @joinable = options.fetch(:joinable, true) end @@ -56,7 +52,7 @@ module ActiveRecord end def rollback - @state.set_state(:rolledback) + @state = TransactionState.rolled_back end def rolledback? @@ -79,7 +75,7 @@ module ActiveRecord end def commit - @state.set_state(:committed) + @state = TransactionState.committed end def before_commit_records |