aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/generators/base.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-07-14 22:20:43 +0200
committerJosé Valim <jose.valim@gmail.com>2009-07-14 22:20:43 +0200
commita06c825b464758a0c22f8b089a596e46f1bba5cb (patch)
tree575916106127d59c3f166c6cebbccc9039a41bcc /railties/lib/generators/base.rb
parente96af1eaa22b649e5df816d7931bd3e912e7a6bc (diff)
downloadrails-a06c825b464758a0c22f8b089a596e46f1bba5cb.tar.gz
rails-a06c825b464758a0c22f8b089a596e46f1bba5cb.tar.bz2
rails-a06c825b464758a0c22f8b089a596e46f1bba5cb.zip
Updated vendored Thor to 0.11.1 and update Rails::Generators.
Diffstat (limited to 'railties/lib/generators/base.rb')
-rw-r--r--railties/lib/generators/base.rb128
1 files changed, 23 insertions, 105 deletions
diff --git a/railties/lib/generators/base.rb b/railties/lib/generators/base.rb
index 069c25f94a..30f138c62a 100644
--- a/railties/lib/generators/base.rb
+++ b/railties/lib/generators/base.rb
@@ -104,8 +104,8 @@ module Rails
#
def self.hook_for(*names, &block)
options = names.extract_options!
- as = options.fetch(:as, generator_name)
- verbose = options.fetch(:verbose, :white)
+ in_base = options.delete(:in) || base_name
+ as_hook = options.delete(:as) || generator_name
names.each do |name|
defaults = if options[:type] == :boolean
@@ -116,57 +116,12 @@ module Rails
{ :desc => "#{name.to_s.humanize} to be invoked", :banner => "NAME" }
end
- class_option name, defaults.merge!(options)
- invocations << [ name, base_name, as ]
- invocation_blocks[name] = block if block_given?
-
- # hook_for :test_framework
- #
- # ==== Generates
- #
- # def hook_for_test_framework
- # return unless options[:test_framework]
- #
- # klass_name = options[:test_framework]
- # klass_name = :test_framework if TrueClass === klass_name
- # klass = Rails::Generators.find_by_namespace(klass_name, "rails", "model")
- #
- # if klass
- # say_status :invoke, options[:test_framework], :white
- # shell.padding += 1
- # if block = self.class.invocation_blocks[:test_framework]
- # block.call(self, klass)
- # else
- # invoke klass
- # end
- # shell.padding -= 1
- # else
- # say "Could not find and invoke '#{klass_name}'"
- # end
- # end
- #
- class_eval <<-METHOD, __FILE__, __LINE__
- def hook_for_#{name}
- return unless options[#{name.inspect}]
-
- klass_name = options[#{name.inspect}]
- klass_name = #{name.inspect} if TrueClass === klass_name
- klass = Rails::Generators.find_by_namespace(klass_name, #{base_name.inspect}, #{as.inspect})
+ unless class_options.key?(name)
+ class_option name, defaults.merge!(options)
+ end
- if klass
- say_status :invoke, klass_name, #{verbose.inspect}
- shell.padding += 1
- if block = self.class.invocation_blocks[#{name.inspect}]
- block.call(self, klass)
- else
- invoke klass
- end
- shell.padding -= 1
- else
- say_status :error, "\#{klass_name} [not found]", :red
- end
- end
- METHOD
+ hooks[name] = [ in_base, as_hook ]
+ invoke_from_option name, options, &block
end
end
@@ -177,11 +132,10 @@ module Rails
# remove_hook_for :orm
#
def self.remove_hook_for(*names)
+ remove_invocation *names
+
names.each do |name|
- remove_class_option name
- remove_task name
- invocations.delete_if { |i| i[0] == name }
- invocation_blocks.delete(name)
+ hooks.delete(name)
end
end
@@ -189,7 +143,7 @@ module Rails
#
def self.class_option(name, options={}) #:nodoc:
options[:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless options.key?(:desc)
- options[:aliases] = Rails::Generators.aliases[name] unless options.key?(:aliases)
+ options[:aliases] = Rails::Generators.aliases[name] unless options.key?(:aliases)
options[:default] = Rails::Generators.options[name] unless options.key?(:default)
super(name, options)
end
@@ -199,7 +153,7 @@ module Rails
# Check whether the given class names are already taken by user
# application or Ruby on Rails.
#
- def class_collisions(*class_names)
+ def class_collisions(*class_names) #:nodoc:
return unless behavior == :invoke
class_names.flatten.each do |class_name|
@@ -231,7 +185,7 @@ module Rails
# Use Rails default banner.
#
def self.banner
- "#{$0} #{generator_name} #{self.arguments.map(&:usage).join(' ')} [options]"
+ "#{$0} #{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
end
# Sets the base_name taking into account the current class namespace.
@@ -251,56 +205,20 @@ module Rails
end
end
- # Stores invocations for this class merging with superclass values.
- #
- def self.invocations #:nodoc:
- @invocations ||= from_superclass(:invocations, [])
- end
-
- # Stores invocation blocks used on hook_for and invoke_if.
- #
- def self.invocation_blocks #:nodoc:
- @invocation_blocks ||= from_superclass(:invocation_blocks, {})
- end
-
- # Overwrite class options help to allow invoked generators options to be
- # shown recursively when invoking a generator.
+ # Keep hooks configuration that are used on prepare_for_invocation.
#
- def self.class_options_help(shell, ungrouped_name=nil, extra_group=nil)
- group_options = Thor::CoreExt::OrderedHash.new
-
- get_options_from_invocations(group_options, class_options) do |klass|
- klass.send(:get_options_from_invocations, group_options, class_options)
- end
-
- group_options.merge!(extra_group) if extra_group
- super(shell, ungrouped_name, group_options)
+ def self.hooks #:nodoc:
+ @hooks ||= from_superclass(:hooks, {})
end
- # Get invocations array and merge options from invocations. Those
- # options are added to group_options hash. Options that already exists
- # in base_options are not added twice.
+ # Prepare class invocation to search on Rails namespace if a previous
+ # added hook is being used.
#
- def self.get_options_from_invocations(group_options, base_options)
- invocations.each do |args|
- name, base, generator = args
- option = class_options[name]
-
- klass_name = option.type == :boolean ? name : option.default
- next unless klass_name
-
- klass = Rails::Generators.find_by_namespace(klass_name, base, generator)
- next unless klass
-
- human_name = klass_name.to_s.classify
- group_options[human_name] ||= []
-
- group_options[human_name] += klass.class_options.values.select do |option|
- base_options[option.name.to_sym].nil? && option.group.nil? &&
- !group_options.values.flatten.any? { |i| i.name == option.name }
- end
-
- yield klass if block_given?
+ def self.prepare_for_invocation(name, value) #:nodoc:
+ if value && constants = self.hooks[name]
+ Rails::Generators.find_by_namespace(value, *constants)
+ else
+ super
end
end