Class | Rails::Generator::Commands::Base |
In: |
vendor/rails/railties/lib/rails_generator/commands.rb
|
Parent: | DelegateClass(Rails::Generator::Base) |
Generator commands delegate Rails::Generator::Base and implement a standard set of actions. Their behavior is defined by the way they respond to these actions: Create brings life; Destroy brings death; List passively observes.
Commands are invoked by replaying (or rewinding) the generator‘s manifest of actions. See Rails::Generator::Manifest and Rails::Generator::Base#manifest method that generator subclasses are required to override.
Commands allows generators to "plug in" invocation behavior, which corresponds to the GoF Strategy pattern.
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 45 45: def dependency(generator_name, args, runtime_options = {}) 46: logger.dependency(generator_name) do 47: self.class.new(instance(generator_name, args, full_options(runtime_options))).invoke! 48: end 49: end
Replay action manifest. RewindBase subclass rewinds manifest.
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 41 41: def invoke! 42: manifest.replay(self) 43: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 72 72: def current_migration_number 73: Dir.glob("#{RAILS_ROOT}/#{@migration_directory}/[0-9]*_*.rb").inject(0) do |max, file_path| 74: n = File.basename(file_path).split('_', 2).first.to_i 75: if n > max then n else max end 76: end 77: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 64 64: def existing_migrations(file_name) 65: Dir.glob("#{@migration_directory}/[0-9]*_*.rb").grep(/[0-9]+_#{file_name}.rb$/) 66: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 87 87: def gsub_file(relative_destination, regexp, *args, &block) 88: path = destination_path(relative_destination) 89: content = File.read(path).gsub(regexp, *args, &block) 90: File.open(path, 'wb') { |file| file.write(content) } 91: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 60 60: def migration_directory(relative_path) 61: directory(@migration_directory = relative_path) 62: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 68 68: def migration_exists?(file_name) 69: not existing_migrations(file_name).empty? 70: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 79 79: def next_migration_number 80: current_migration_number + 1 81: end