Class: ActiveRecord::Migration::CommandRecorder
- Inherits:
-
Object
- Object
- ActiveRecord::Migration::CommandRecorder
- Includes:
- StraightReversions, JoinTable
- Defined in:
- lib/active_record/migration/command_recorder.rb
Overview
ActiveRecord::Migration::CommandRecorder
records commands done during a migration and knows how to reverse those commands. The CommandRecorder knows how to invert the following commands:
-
add_column
-
add_index
-
add_timestamps
-
create_table
-
create_join_table
-
remove_timestamps
-
rename_column
-
rename_index
-
rename_table
Defined Under Namespace
Modules: StraightReversions
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#delegate ⇒ Object
Returns the value of attribute delegate.
-
#reverting ⇒ Object
Returns the value of attribute reverting.
Instance Method Summary collapse
-
#change_table(table_name, options = {}) {|delegate.update_table_definition(table_name, self)| ... } ⇒ Object
:nodoc:.
-
#initialize(delegate = nil) ⇒ CommandRecorder
constructor
A new instance of CommandRecorder.
-
#inverse_of(command, args, &block) ⇒ Object
Returns the inverse of the given command.
-
#record(*command, &block) ⇒ Object
record
command
. -
#respond_to?(*args) ⇒ Boolean
:nodoc:.
-
#revert ⇒ Object
While executing the given block, the recorded will be in reverting mode.
Constructor Details
#initialize(delegate = nil) ⇒ CommandRecorder
Returns a new instance of CommandRecorder.
21 22 23 24 25 |
# File 'lib/active_record/migration/command_recorder.rb', line 21 def initialize(delegate = nil) @commands = [] @delegate = delegate @reverting = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
Forwards any missing method call to the target.
188 189 190 191 192 193 194 |
# File 'lib/active_record/migration/command_recorder.rb', line 188 def method_missing(method, *args, &block) if @delegate.respond_to?(method) @delegate.send(method, *args, &block) else super end end |
Instance Attribute Details
#commands ⇒ Object
Returns the value of attribute commands.
19 20 21 |
# File 'lib/active_record/migration/command_recorder.rb', line 19 def commands @commands end |
#delegate ⇒ Object
Returns the value of attribute delegate.
19 20 21 |
# File 'lib/active_record/migration/command_recorder.rb', line 19 def delegate @delegate end |
#reverting ⇒ Object
Returns the value of attribute reverting.
19 20 21 |
# File 'lib/active_record/migration/command_recorder.rb', line 19 def reverting @reverting end |
Instance Method Details
#change_table(table_name, options = {}) {|delegate.update_table_definition(table_name, self)| ... } ⇒ Object
:nodoc:
90 91 92 |
# File 'lib/active_record/migration/command_recorder.rb', line 90 def change_table(table_name, = {}) # :nodoc: yield delegate.update_table_definition(table_name, self) end |
#inverse_of(command, args, &block) ⇒ Object
Returns the inverse of the given command. For example:
recorder.inverse_of(:rename_table, [:old, :new])
# => [:rename_table, [:new, :old]]
This method will raise an IrreversibleMigration
exception if it cannot invert the command
.
63 64 65 66 67 |
# File 'lib/active_record/migration/command_recorder.rb', line 63 def inverse_of(command, args, &block) method = :"invert_#{command}" raise IrreversibleMigration unless respond_to?(method, true) send(method, args, &block) end |
#record(*command, &block) ⇒ Object
record command
. command
should be a method name and arguments. For example:
recorder.record(:method_name, [:arg1, :arg2])
48 49 50 51 52 53 54 |
# File 'lib/active_record/migration/command_recorder.rb', line 48 def record(*command, &block) if @reverting @commands << inverse_of(*command, &block) else @commands << (command << block) end end |
#respond_to?(*args) ⇒ Boolean
:nodoc:
69 70 71 |
# File 'lib/active_record/migration/command_recorder.rb', line 69 def respond_to?(*args) # :nodoc: super || delegate.respond_to?(*args) end |
#revert ⇒ Object
While executing the given block, the recorded will be in reverting mode. All commands recorded will end up being recorded reverted and in reverse order. For example:
recorder.revert{ recorder.record(:rename_table, [:old, :new]) }
# same effect as recorder.record(:rename_table, [:new, :old])
34 35 36 37 38 39 40 41 42 |
# File 'lib/active_record/migration/command_recorder.rb', line 34 def revert @reverting = !@reverting previous = @commands @commands = [] yield ensure @commands = previous.concat(@commands.reverse) @reverting = !@reverting end |