Class: ActiveRecord::Migration::CommandRecorder

Inherits:
Object
  • Object
show all
Defined in:
lib/migration_revert/ext/command_recorder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#revertingObject

Returns the value of attribute reverting.



4
5
6
# File 'lib/migration_revert/ext/command_recorder.rb', line 4

def reverting
  @reverting
end

Instance Method Details

#inverse_of(command, args, &block) ⇒ Object

Returns the inverse of the given command

recorder.inverse_of(:rename_table, [:old, :new])
 # => [:rename_table, [:new, :old]]

This method will raise an IrreversibleMigration exception if it cannot invert the commands.

Raises:

  • (IrreversibleMigration)


36
37
38
39
40
# File 'lib/migration_revert/ext/command_recorder.rb', line 36

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])


21
22
23
24
25
26
27
# File 'lib/migration_revert/ext/command_recorder.rb', line 21

def record(*command, &block)
  if @reverting
    @commands << inverse_of(*command, &block)
  else
    @commands << (command << block)
  end
end

#revertObject



6
7
8
9
10
11
12
13
14
# File 'lib/migration_revert/ext/command_recorder.rb', line 6

def revert
  @reverting = !@reverting
  previous = @commands
  @commands = []
  yield
ensure
  @commands = previous.concat(@commands.reverse)
  @reverting = !@reverting
end