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 60 60: def current_migration_number 61: Dir.glob("#{RAILS_ROOT}/#{@migration_directory}/[0-9]*_*.rb").inject(0) do |max, file_path| 62: n = File.basename(file_path).split('_', 2).first.to_i 63: if n > max then n else max end 64: end 65: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 75 75: def existing_migrations(file_name) 76: Dir.glob("#{@migration_directory}/[0-9]*_*.rb").grep(/[0-9]+_#{file_name}.rb$/) 77: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 91 91: def gsub_file(relative_destination, regexp, *args, &block) 92: path = destination_path(relative_destination) 93: content = File.read(path).gsub(regexp, *args, &block) 94: File.open(path, 'wb') { |file| file.write(content) } 95: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 71 71: def migration_directory(relative_path) 72: directory(@migration_directory = relative_path) 73: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 79 79: def migration_exists?(file_name) 80: not existing_migrations(file_name).empty? 81: end
# File vendor/rails/railties/lib/rails_generator/commands.rb, line 67 67: def next_migration_number 68: current_migration_number + 1 69: end