DelegateClass(RubiGen::Base)
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
52: def class_collisions(*class_names)
53: end
# File lib/rubigen/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 lib/rubigen/commands.rb, line 40
40: def invoke!
41: manifest.replay(self)
42: after_generate
43: end
Does nothing for all commands except Create.
# File lib/rubigen/commands.rb, line 56
56: def readme(*args)
57: end
Does nothing for all commands except Create.
# File lib/rubigen/commands.rb, line 60
60: def write_manifest
61: end
# File lib/rubigen/commands.rb, line 64
64: def current_migration_number
65: Dir.glob("#{RAILS_ROOT}/#{@migration_directory}/[0-9]*_*.rb").inject(0) do |max, file_path|
66: n = File.basename(file_path).split('_', 2).first.to_i
67: if n > max then n else max end
68: end
69: end
# File lib/rubigen/commands.rb, line 79
79: def existing_migrations(file_name)
80: Dir.glob("#{@migration_directory}/[0-9]*_*.rb").grep(/[0-9]+_#{file_name}.rb$/)
81: end
# File lib/rubigen/commands.rb, line 95
95: def gsub_file(relative_destination, regexp, *args, &block)
96: path = destination_path(relative_destination)
97: content = File.read(path).gsub(regexp, *args, &block)
98: File.open(path, 'wb') { |file| file.write(content) }
99: end
# File lib/rubigen/commands.rb, line 75
75: def migration_directory(relative_path)
76: directory(@migration_directory = relative_path)
77: end
# File lib/rubigen/commands.rb, line 83
83: def migration_exists?(file_name)
84: not existing_migrations(file_name).empty?
85: end
# File lib/rubigen/commands.rb, line 71
71: def next_migration_number
72: current_migration_number + 1
73: end
# File lib/rubigen/commands.rb, line 87
87: def next_migration_string(padding = 3)
88: if ActiveRecord::Base.timestamped_migrations
89: Time.now.utc.strftime("%Y%m%d%H%M%S")
90: else
91: "%.#{padding}d" % next_migration_number
92: end
93: end
# File lib/rubigen/commands.rb, line 142
142: def diff_cmd
143: ENV['RAILS_DIFF'] || 'diff -u'
144: end
Ask the user interactively whether to force collision.
# File lib/rubigen/commands.rb, line 103
103: def force_file_collision?(destination, src, dst, file_options = {}, &block)
104: stdout.print "overwrite #{destination}? (enter \"h\" for help) [Ynaiqd] "
105: stdout.flush
106: case $stdin.gets.chomp
107: when /\Ad\z/
108: Tempfile.open(File.basename(destination), File.dirname(dst)) do |temp|
109: temp.write render_file(src, file_options, &block)
110: temp.rewind
111: stdout.puts `#{diff_cmd} #{dst} #{temp.path}`
112: end
113: stdout.puts "retrying"
114: raise 'retry diff'
115: when /\Aa\z/
116: stdout.puts "forcing #{spec.name}"
117: options[:collision] = :force
118: when /\Ai\z/
119: stdout.puts "ignoring #{spec.name}"
120: options[:collision] = :skip
121: when /\Aq\z/
122: stdout.puts "aborting #{spec.name}"
123: raise SystemExit
124: when /\An\z/ then :skip
125: when /\Ay\z/ then :force
126: else
127: 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(/^ /, '')
128: raise 'retry'
129: end
130: rescue
131: retry
132: end
# File lib/rubigen/commands.rb, line 146
146: def render_template_part(template_options)
147: # Getting Sandbox to evaluate part template in it
148: part_binding = template_options[:sandbox].call.sandbox_binding
149: part_rel_path = template_options[:insert]
150: part_path = source_path(part_rel_path)
151:
152: # Render inner template within Sandbox binding
153: rendered_part = ERB.new(File.readlines(part_path).join, nil, '-').result(part_binding)
154: begin_mark = template_part_mark(template_options[:begin_mark], template_options[:mark_id])
155: end_mark = template_part_mark(template_options[:end_mark], template_options[:mark_id])
156: begin_mark + rendered_part + end_mark
157: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.