Refactoring Generators::Base.
The defaults hash isn't used unless the +class_options+ hash has a particular key, so we don't need to compute it unless this is true. Also moving some code for extracting a module into its own method.
This commit is contained in:
parent
b0a8931b11
commit
ecd4e70a1f
@ -168,15 +168,15 @@ def self.hook_for(*names, &block)
|
||||
as_hook = options.delete(:as) || generator_name
|
||||
|
||||
names.each do |name|
|
||||
defaults = if options[:type] == :boolean
|
||||
{ }
|
||||
elsif [true, false].include?(default_value_for_option(name, options))
|
||||
{ banner: "" }
|
||||
else
|
||||
{ desc: "#{name.to_s.humanize} to be invoked", banner: "NAME" }
|
||||
end
|
||||
|
||||
unless class_options.key?(name)
|
||||
defaults = if options[:type] == :boolean
|
||||
{ }
|
||||
elsif [true, false].include?(default_value_for_option(name, options))
|
||||
{ banner: "" }
|
||||
else
|
||||
{ desc: "#{name.to_s.humanize} to be invoked", banner: "NAME" }
|
||||
end
|
||||
|
||||
class_option(name, defaults.merge!(options))
|
||||
end
|
||||
|
||||
@ -255,12 +255,7 @@ def class_collisions(*class_names) #:nodoc:
|
||||
# Split the class from its module nesting
|
||||
nesting = class_name.split('::')
|
||||
last_name = nesting.pop
|
||||
|
||||
# Extract the last Module in the nesting
|
||||
last = nesting.inject(Object) do |last_module, nest|
|
||||
break unless last_module.const_defined?(nest, false)
|
||||
last_module.const_get(nest)
|
||||
end
|
||||
last = extract_last_module(nesting)
|
||||
|
||||
if last && last.const_defined?(last_name.camelize, false)
|
||||
raise Error, "The name '#{class_name}' is either already used in your application " <<
|
||||
@ -270,6 +265,14 @@ def class_collisions(*class_names) #:nodoc:
|
||||
end
|
||||
end
|
||||
|
||||
# Takes in an array of nested modules and extracts the last module
|
||||
def extract_last_module(nesting)
|
||||
nesting.inject(Object) do |last_module, nest|
|
||||
break unless last_module.const_defined?(nest, false)
|
||||
last_module.const_get(nest)
|
||||
end
|
||||
end
|
||||
|
||||
# Use Rails default banner.
|
||||
def self.banner
|
||||
"rails generate #{namespace.sub(/^rails:/,'')} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]".gsub(/\s+/, ' ')
|
||||
|
Loading…
Reference in New Issue
Block a user