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 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 76 76: def gsub_file(relative_destination, regexp, *args, &block) 77: path = destination_path(relative_destination) 78: content = File.read(path).gsub(regexp, *args, &block) 79: File.open(path, 'wb') { |file| file.write(content) } 80: 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