Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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.
  • Loading branch information...
commit ecd4e70a1f9150c90cca8557f4b3a0bf746874ba 1 parent b0a8931
@wangjohn wangjohn authored
Showing with 17 additions and 14 deletions.
  1. +17 −14 railties/lib/rails/generators/base.rb
View
31 railties/lib/rails/generators/base.rb
@@ -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+/, ' ')
Please sign in to comment.
Something went wrong with that request. Please try again.