diff options
author | José Valim <jose.valim@gmail.com> | 2009-06-26 10:40:02 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-06-26 11:59:14 +0200 |
commit | 2f3681dfc24e51698b9b900a5d03950da4508d2c (patch) | |
tree | 2146601abf73dd53441764fc8845c75795a49bac /railties/lib | |
parent | 6bd101188e3396317d93d2d0692587d577ad9662 (diff) | |
download | rails-2f3681dfc24e51698b9b900a5d03950da4508d2c.tar.gz rails-2f3681dfc24e51698b9b900a5d03950da4508d2c.tar.bz2 rails-2f3681dfc24e51698b9b900a5d03950da4508d2c.zip |
Clean up class collisions check and a class method helper.
Diffstat (limited to 'railties/lib')
13 files changed, 56 insertions, 46 deletions
diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb index e5727b8938..ecc71e56c3 100644 --- a/railties/lib/generators/base.rb +++ b/railties/lib/generators/base.rb @@ -45,9 +45,8 @@ module Rails protected - # Check whether the given class names are already taken by Ruby or Rails. - # In the future, expand to check other namespaces such as the rest of - # the user's app. + # Check whether the given class names are already taken by user + # application or Ruby on Rails. # def class_collisions(*class_names) return unless behavior == :invoke diff --git a/railties/lib/generators/named_base.rb b/railties/lib/generators/named_base.rb index 4f173bffce..f29916b5f6 100644 --- a/railties/lib/generators/named_base.rb +++ b/railties/lib/generators/named_base.rb @@ -11,15 +11,43 @@ module Rails alias :file_name :singular_name - def initialize(*args) + class << self + # Add a class collisions name to be checked on class initialization. You + # can supply a hash with a :prefix or :suffix to be tested. + # + # ==== Examples + # + # check_class_collision :suffix => "Observer" + # + # If the generator is invoked with class name Admin, it will check for + # the presence of "AdminObserver". + # + def check_class_collision(options={}) + @class_collisions = options + end + + # Returns the class collisions for this class and retreives one from + # superclass. The from_superclass method used below is from Thor. + # + def class_collisions #:nodoc: + @class_collisions ||= from_superclass(:class_collisions, nil) + end + end + + def initialize(*args) #:nodoc: super assign_names!(self.name) parse_attributes! if respond_to?(:attributes) + + if self.class.class_collisions + value = add_prefix_and_suffix(class_name, self.class.class_collisions) + class_collisions(value) + end end protected - def assign_names!(given_name) + def assign_names!(given_name) #:nodoc: self.name, @class_path, @file_path, @class_nesting, @class_nesting_depth = extract_modules(given_name) @class_name_without_nesting, @singular_name, @plural_name = inflect_names(self.name) @@ -40,7 +68,7 @@ module Rails # Convert attributes hash into an array with GeneratedAttribute objects. # - def parse_attributes! + def parse_attributes! #:nodoc: attributes.map! do |name, type| Rails::Generator::GeneratedAttribute.new(name, type) end @@ -49,7 +77,7 @@ module Rails # Extract modules from filesystem-style or ruby-style path. Both # good/fun/stuff and Good::Fun::Stuff produce the same results. # - def extract_modules(name) + def extract_modules(name) #:nodoc: modules = name.include?('/') ? name.split('/') : name.split('::') name = modules.pop path = modules.map { |m| m.underscore } @@ -62,13 +90,19 @@ module Rails # Receives name and return camelized, underscored and pluralized names. # - def inflect_names(name) + def inflect_names(name) #:nodoc: camel = name.camelize under = camel.underscore plural = under.pluralize [camel, under, plural] end + # Receives a name and add suffix and prefix values frrm hash. + # + def add_prefix_and_suffix(name, hash) #:nodoc: + "#{hash[:prefix]}#{name}#{hash[:suffix]}" + end + end end end diff --git a/railties/lib/generators/rails/helper/helper_generator.rb b/railties/lib/generators/rails/helper/helper_generator.rb index 6f741b0978..2b84581025 100644 --- a/railties/lib/generators/rails/helper/helper_generator.rb +++ b/railties/lib/generators/rails/helper/helper_generator.rb @@ -1,9 +1,7 @@ module Rails module Generators class HelperGenerator < NamedBase - def check_class_collisions - class_collisions "#{class_name}Helper" - end + check_class_collision :suffix => "Helper" def create_helper_files template 'helper.rb', File.join('app/helpers', class_path, "#{file_name}_helper.rb") diff --git a/railties/lib/generators/rails/integration_test/integration_test_generator.rb b/railties/lib/generators/rails/integration_test/integration_test_generator.rb index 00afaf70ab..7071d5ec50 100644 --- a/railties/lib/generators/rails/integration_test/integration_test_generator.rb +++ b/railties/lib/generators/rails/integration_test/integration_test_generator.rb @@ -1,9 +1,7 @@ module Rails module Generators class IntegrationTestGenerator < NamedBase - def check_class_collisions - class_collisions "#{class_name}Test" - end + check_class_collisions :suffix => "Test" def create_test_files template 'integration_test.rb', File.join('test/integration', class_path, "#{file_name}_test.rb") diff --git a/railties/lib/generators/rails/mailer/mailer_generator.rb b/railties/lib/generators/rails/mailer/mailer_generator.rb index 6e7e6e811e..1753166af5 100644 --- a/railties/lib/generators/rails/mailer/mailer_generator.rb +++ b/railties/lib/generators/rails/mailer/mailer_generator.rb @@ -2,10 +2,7 @@ module Rails module Generators class MailerGenerator < NamedBase argument :actions, :type => :array, :default => [], :banner => "method method" - - def check_class_collision - class_collisions class_name - end + check_class_collision def create_mailer_file template "mailer.rb", File.join('app/models', class_path, "#{file_name}.rb") diff --git a/railties/lib/generators/rails/metal/metal_generator.rb b/railties/lib/generators/rails/metal/metal_generator.rb index f8833ecec3..d814b6c38b 100644 --- a/railties/lib/generators/rails/metal/metal_generator.rb +++ b/railties/lib/generators/rails/metal/metal_generator.rb @@ -1,9 +1,7 @@ module Rails module Generators class MetalGenerator < NamedBase - def check_class_collision - class_collisions class_name - end + check_class_collision def create_file template "metal.rb", "app/metal/#{file_name}.rb" diff --git a/railties/lib/generators/rails/observer/observer_generator.rb b/railties/lib/generators/rails/observer/observer_generator.rb index 205ffc8064..ad058a3172 100644 --- a/railties/lib/generators/rails/observer/observer_generator.rb +++ b/railties/lib/generators/rails/observer/observer_generator.rb @@ -1,9 +1,7 @@ module Rails module Generators class ObserverGenerator < NamedBase - def check_class_collision - class_collisions "#{class_name}Observer" - end + check_class_collision :suffix => "Observer" def create_observer_file template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb") diff --git a/railties/lib/generators/rails/performance_test/performance_test_generator.rb b/railties/lib/generators/rails/performance_test/performance_test_generator.rb index bf83b354a8..b8efda8df3 100644 --- a/railties/lib/generators/rails/performance_test/performance_test_generator.rb +++ b/railties/lib/generators/rails/performance_test/performance_test_generator.rb @@ -1,12 +1,11 @@ module Rails module Generators - class PerformanceTestGenerator < NamedBase
- def check_class_collisions - class_collisions "#{class_name}Test" - end + class PerformanceTestGenerator < NamedBase + check_class_collision :suffix => "Test" - def create_test_files
- template 'performance_test.rb', File.join('test/performance', class_path, "#{file_name}_test.rb")
- end
- end
end + def create_test_files + template 'performance_test.rb', File.join('test/performance', class_path, "#{file_name}_test.rb") + end + end + end end diff --git a/railties/lib/generators/rails/plugin/plugin_generator.rb b/railties/lib/generators/rails/plugin/plugin_generator.rb index c3042afa94..30618bdca0 100644 --- a/railties/lib/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/generators/rails/plugin/plugin_generator.rb @@ -7,9 +7,7 @@ module Rails class_option :with_generator, :type => :boolean, :aliases => "-g", :default => false, :desc => "When supplied creates generator base files." - def check_class_collision - class_collisions class_name - end + check_class_collision def create_root self.root = File.expand_path("vendor/plugins/#{file_name}", root) diff --git a/railties/lib/generators/test_unit.rb b/railties/lib/generators/test_unit.rb index 16d097c3c8..c4b8a8702d 100644 --- a/railties/lib/generators/test_unit.rb +++ b/railties/lib/generators/test_unit.rb @@ -3,6 +3,7 @@ require 'generators/named_base' module TestUnit module Generators class Base < Rails::Generators::NamedBase + check_class_collision :suffix => "Test" end end end diff --git a/railties/lib/generators/test_unit/helper/helper_generator.rb b/railties/lib/generators/test_unit/helper/helper_generator.rb index 9d888929d0..866556f6a1 100644 --- a/railties/lib/generators/test_unit/helper/helper_generator.rb +++ b/railties/lib/generators/test_unit/helper/helper_generator.rb @@ -1,9 +1,7 @@ module TestUnit module Generators class HelperGenerator < Base - def check_class_collisions - class_collisions "#{class_name}HelperTest" - end + check_class_collision :suffix => "HelperTest" def create_helper_files template 'helper_test.rb', File.join('test/unit/helpers', class_path, "#{file_name}_helper_test.rb") diff --git a/railties/lib/generators/test_unit/mailer/mailer_generator.rb b/railties/lib/generators/test_unit/mailer/mailer_generator.rb index 5a4499d92c..84e3024427 100644 --- a/railties/lib/generators/test_unit/mailer/mailer_generator.rb +++ b/railties/lib/generators/test_unit/mailer/mailer_generator.rb @@ -3,10 +3,6 @@ module TestUnit class MailerGenerator < Base argument :actions, :type => :array, :default => [], :banner => "method method" - def check_class_collisions - class_collisions "#{class_name}Test" - end - def create_test_files template "unit_test.rb", File.join('test', 'unit', class_path, "#{file_name}_test.rb") end diff --git a/railties/lib/generators/test_unit/observer/observer_generator.rb b/railties/lib/generators/test_unit/observer/observer_generator.rb index f4534840f3..adfd90a086 100644 --- a/railties/lib/generators/test_unit/observer/observer_generator.rb +++ b/railties/lib/generators/test_unit/observer/observer_generator.rb @@ -1,10 +1,6 @@ module TestUnit module Generators class ObserverGenerator < Base - def check_class_collisions - class_collisions class_name, "#{class_name}Test" - end - def create_test_files template 'unit_test.rb', File.join('test', 'unit', class_path, "#{file_name}_observer_test.rb") end |