Generator commands delegate RubiGen::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 RubiGen::Manifest and RubiGen::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.
Does nothing for all commands except Create.
# File lib/rubigen/commands.rb, line 52 def class_collisions(*class_names) end
# File lib/rubigen/commands.rb, line 45 def dependency(generator_name, args, runtime_options = {}) logger.dependency(generator_name) do self.class.new(instance(generator_name, args, full_options(runtime_options))).invoke! end end
Replay action manifest. RewindBase subclass rewinds manifest.
# File lib/rubigen/commands.rb, line 40 def invoke! manifest.replay(self) after_generate end
Does nothing for all commands except Create.
# File lib/rubigen/commands.rb, line 56 def readme(*args) end
Does nothing for all commands except Create.
# File lib/rubigen/commands.rb, line 60 def write_manifest end
# File lib/rubigen/commands.rb, line 64 def current_migration_number Dir.glob("#{RAILS_ROOT}/#{@migration_directory}/[0-9]*_*.rb").inject(0) do |max, file_path| n = File.basename(file_path).split('_', 2).first.to_i if n > max then n else max end end end
# File lib/rubigen/commands.rb, line 79 def existing_migrations(file_name) Dir.glob("#{@migration_directory}/[0-9]*_*.rb").grep(/[0-9]+_#{file_name}.rb$/) end
# File lib/rubigen/commands.rb, line 95 def gsub_file(relative_destination, regexp, *args, &block) path = destination_path(relative_destination) content = File.read(path).gsub(regexp, *args, &block) File.open(path, 'wb') { |file| file.write(content) } end
# File lib/rubigen/commands.rb, line 75 def migration_directory(relative_path) directory(@migration_directory = relative_path) end
# File lib/rubigen/commands.rb, line 83 def migration_exists?(file_name) not existing_migrations(file_name).empty? end
# File lib/rubigen/commands.rb, line 71 def next_migration_number current_migration_number + 1 end
# File lib/rubigen/commands.rb, line 87 def next_migration_string(padding = 3) if ActiveRecord::Base.timestamped_migrations Time.now.utc.strftime("%Y%m%d%H%M%S") else "%.#{padding}d" % next_migration_number end end
# File lib/rubigen/commands.rb, line 142 def diff_cmd ENV['RAILS_DIFF'] || 'diff -u' end
Ask the user interactively whether to force collision.
# File lib/rubigen/commands.rb, line 103 def force_file_collision?(destination, src, dst, file_options = {}, &block) stdout.print "overwrite #{destination}? (enter \"h\" for help) [Ynaiqd] " stdout.flush case $stdin.gets.chomp when /\Ad\z/ Tempfile.open(File.basename(destination), File.dirname(dst)) do |temp| temp.write render_file(src, file_options, &block) temp.rewind stdout.puts %x#{diff_cmd} #{dst} #{temp.path}` end stdout.puts "retrying" raise 'retry diff' when /\Aa\z/ stdout.puts "forcing #{spec.name}" options[:collision] = :force when /\Ai\z/ stdout.puts "ignoring #{spec.name}" options[:collision] = :skip when /\Aq\z/ stdout.puts "aborting #{spec.name}" raise SystemExit when /\An\z/ then :skip when /\Ay\z/ then :force else stdout.puts " Y - yes, overwrite n - no, do not overwrite a - all, overwrite this and all others i - ignore, skip any conflicts q - quit, abort d - diff, show the differences between the old and the new h - help, show this help ".gsub(/^ /, '') raise 'retry' end rescue retry end
# File lib/rubigen/commands.rb, line 146 def render_template_part(template_options) # Getting Sandbox to evaluate part template in it part_binding = template_options[:sandbox].call.sandbox_binding part_rel_path = template_options[:insert] part_path = source_path(part_rel_path) # Render inner template within Sandbox binding rendered_part = ERB.new(File.readlines(part_path).join, nil, '-').result(part_binding) begin_mark = template_part_mark(template_options[:begin_mark], template_options[:mark_id]) end_mark = template_part_mark(template_options[:end_mark], template_options[:mark_id]) begin_mark + rendered_part + end_mark end
# File lib/rubigen/commands.rb, line 159 def template_part_mark(name, id) "<!--[#{name}:#{id}]-->\n" end