diff options
Diffstat (limited to 'railties/lib/generator')
-rw-r--r-- | railties/lib/generator/base.rb | 34 | ||||
-rw-r--r-- | railties/lib/generator/generators/app/app_generator.rb | 8 | ||||
-rw-r--r-- | railties/lib/generator/generators/metal/USAGE | 8 | ||||
-rw-r--r-- | railties/lib/generator/generators/metal/metal_generator.rb | 17 | ||||
-rw-r--r-- | railties/lib/generator/generators/metal/templates/metal.rb | 12 |
5 files changed, 70 insertions, 9 deletions
diff --git a/railties/lib/generator/base.rb b/railties/lib/generator/base.rb index 8751cb08fb..08f488932b 100644 --- a/railties/lib/generator/base.rb +++ b/railties/lib/generator/base.rb @@ -23,9 +23,16 @@ module Rails # Automatically sets the source root based on the class name. # def self.source_root - @source_root ||= begin - klass_name = self.name.gsub(/^Rails::Generators::/, '') - File.expand_path(File.join(File.dirname(__FILE__), 'generators', klass_name.underscore, 'templates')) + @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'generators', generator_name, 'templates')) + end + + # Convenience method to get the namespace from the class name. + # + def self.namespace(name=nil) + if name + super + else + @namespace ||= "rails:generators:#{generator_name}" end end @@ -34,11 +41,26 @@ module Rails # Use Rails default banner. # def self.banner - "#{$0} #{self.arguments.map(&:usage).join(' ')} [options]" + "#{$0} #{generator_name} #{self.arguments.map(&:usage).join(' ')} [options]" + end + + # Removes the namespaces and get the generator name. For example, + # Rails::Generators::MetalGenerator will return "metal" as generator name. + # + # The name is used to set the namespace (in this case "rails:generators:metal") + # and to set the source root ("generators/metal/templates"). + # + def self.generator_name + @generator_name ||= begin + klass_name = self.name + klass_name.gsub!(/^Rails::Generators::/, '') + klass_name.gsub!(/Generator$/, '') + klass_name.underscore + end end - # Small macro to ruby as an option to the generator with proper default - # value plus an instance helper method. + # Small macro to add ruby as an option to the generator with proper + # default value plus an instance helper method called shebang. # def self.add_shebang_option! require 'rbconfig' diff --git a/railties/lib/generator/generators/app/app_generator.rb b/railties/lib/generator/generators/app/app_generator.rb index 6f5cb5f2c6..3eb9b8c79a 100644 --- a/railties/lib/generator/generators/app/app_generator.rb +++ b/railties/lib/generator/generators/app/app_generator.rb @@ -3,10 +3,8 @@ require 'digest/md5' require 'active_support/secure_random' module Rails::Generators - class App < Base + class AppGenerator < Base DATABASES = %w( mysql oracle postgresql sqlite2 sqlite3 frontbase ibm_db ) - - namespace "rails:app" add_shebang_option! argument :app_path, :type => :string @@ -182,6 +180,10 @@ module Rails::Generators ActiveSupport::SecureRandom.hex(64) end + def self.banner + "#{$0} #{self.arguments.map(&:usage).join(' ')} [options]" + end + def mysql_socket @mysql_socket ||= [ "/tmp/mysql.sock", # default diff --git a/railties/lib/generator/generators/metal/USAGE b/railties/lib/generator/generators/metal/USAGE new file mode 100644 index 0000000000..123ec6c03f --- /dev/null +++ b/railties/lib/generator/generators/metal/USAGE @@ -0,0 +1,8 @@ +Description: + Cast some metal! + +Examples: + `./script/generate metal poller` + + This will create: + Metal: app/metal/poller.rb diff --git a/railties/lib/generator/generators/metal/metal_generator.rb b/railties/lib/generator/generators/metal/metal_generator.rb new file mode 100644 index 0000000000..1c59a4e90a --- /dev/null +++ b/railties/lib/generator/generators/metal/metal_generator.rb @@ -0,0 +1,17 @@ +require File.dirname(__FILE__) + '/../../base' + +module Rails::Generators + class MetalGenerator < Base + argument :file_name, :type => :string + + def create_file + template "metal.rb", "app/metal/#{file_name}.rb" + end + + protected + + def class_name + file_name.classify + end + end +end diff --git a/railties/lib/generator/generators/metal/templates/metal.rb b/railties/lib/generator/generators/metal/templates/metal.rb new file mode 100644 index 0000000000..e94982b69a --- /dev/null +++ b/railties/lib/generator/generators/metal/templates/metal.rb @@ -0,0 +1,12 @@ +# Allow the metal piece to run in isolation +require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails) + +class <%= class_name %> + def self.call(env) + if env["PATH_INFO"] =~ /^\/<%= file_name %>/ + [200, {"Content-Type" => "text/html"}, ["Hello, World!"]] + else + [404, {"Content-Type" => "text/html"}, ["Not Found"]] + end + end +end |