Class: ActiveRecord::Migration::CommandRecorder

Inherits:
Object
  • Object
show all
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_timestamp

  • create_table

  • remove_timestamps

  • rename_column

  • rename_index

  • rename_table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate = nil) ⇒ CommandRecorder

Returns a new instance of CommandRecorder.



18
19
20
21
# File 'lib/active_record/migration/command_recorder.rb', line 18

def initialize(delegate = nil)
  @commands = []
  @delegate = delegate
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.



97
98
99
100
101
# File 'lib/active_record/migration/command_recorder.rb', line 97

def method_missing(method, *args, &block)
  @delegate.send(method, *args, &block)
rescue NoMethodError => e
  raise e, e.message.sub(/ for #<.*$/, " via proxy for #{@delegate}")
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



16
17
18
# File 'lib/active_record/migration/command_recorder.rb', line 16

def commands
  @commands
end

#delegateObject

Returns the value of attribute delegate.



16
17
18
# File 'lib/active_record/migration/command_recorder.rb', line 16

def delegate
  @delegate
end

Instance Method Details

#inverseObject

Returns a list that represents commands that are the inverse of the commands stored in commands. For example:

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

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



39
40
41
42
43
44
45
# File 'lib/active_record/migration/command_recorder.rb', line 39

def inverse
  @commands.reverse.map { |name, args|
    method = :"invert_#{name}"
    raise IrreversibleMigration unless respond_to?(method, true)
    send(method, args)
  }
end

#record(*command) ⇒ Object

record command. command should be a method name and arguments. For example:

recorder.record(:method_name, [:arg1, arg2])


27
28
29
# File 'lib/active_record/migration/command_recorder.rb', line 27

def record(*command)
  @commands << command
end

#respond_to?(*args) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


47
48
49
# File 'lib/active_record/migration/command_recorder.rb', line 47

def respond_to?(*args) # :nodoc:
  super || delegate.respond_to?(*args)
end