diff options
author | wangjohn <wangjohn@mit.edu> | 2013-08-28 15:05:29 -0500 |
---|---|---|
committer | wangjohn <wangjohn@mit.edu> | 2013-08-28 15:22:05 -0500 |
commit | ecd4e70a1f9150c90cca8557f4b3a0bf746874ba (patch) | |
tree | 381d0be83df956894caca138db3e64542797a77c | |
parent | b0a8931b11ac1343445b1b12e270d030042bf05b (diff) | |
download | rails-ecd4e70a1f9150c90cca8557f4b3a0bf746874ba.tar.gz rails-ecd4e70a1f9150c90cca8557f4b3a0bf746874ba.tar.bz2 rails-ecd4e70a1f9150c90cca8557f4b3a0bf746874ba.zip |
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.
-rw-r--r-- | railties/lib/rails/generators/base.rb | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb index 7e938fab47..8aec8bc8f9 100644 --- a/railties/lib/rails/generators/base.rb +++ b/railties/lib/rails/generators/base.rb @@ -168,15 +168,15 @@ module Rails 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 @@ module Rails # 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 @@ module Rails 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+/, ' ') |