Module: Rails::Generator::Lookup::ClassMethods
- Defined in:
- lib/rails_generator/lookup.rb
Instance Method Summary collapse
-
#append_sources(*args) ⇒ Object
Add a source to the end of the list.
-
#instance(generator_name, args = [], runtime_options = {}) ⇒ Object
Convenience method to lookup and instantiate a generator.
-
#lookup(generator_name) ⇒ Object
Lookup knows how to find generators’ Specs from a list of Sources.
-
#prepend_sources(*args) ⇒ Object
Add a source to the beginning of the list.
-
#reset_sources ⇒ Object
Reset the source list.
-
#sources ⇒ Object
The list of sources where we look, in order, for generators.
-
#use_application_sources! ⇒ Object
Use application generators (app, ?).
-
#use_component_sources! ⇒ Object
Use component generators (model, controller, etc).
Instance Method Details
#append_sources(*args) ⇒ Object
Add a source to the end of the list.
70 71 72 73 |
# File 'lib/rails_generator/lookup.rb', line 70 def append_sources(*args) sources.concat(args.flatten) invalidate_cache! end |
#instance(generator_name, args = [], runtime_options = {}) ⇒ Object
Convenience method to lookup and instantiate a generator.
139 140 141 |
# File 'lib/rails_generator/lookup.rb', line 139 def instance(generator_name, args = [], = {}) lookup(generator_name).klass.new(args, ()) end |
#lookup(generator_name) ⇒ Object
Lookup knows how to find generators’ Specs from a list of Sources. Searches the sources, in order, for the first matching name.
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rails_generator/lookup.rb', line 124 def lookup(generator_name) @found ||= {} generator_name = generator_name.to_s.downcase @found[generator_name] ||= cache.find { |spec| spec.name == generator_name } unless @found[generator_name] chars = generator_name.scan(/./).map{|c|"#{c}.*?"} rx = /^#{chars}$/ gns = cache.select{|spec| spec.name =~ rx } @found[generator_name] ||= gns.first if gns.length == 1 raise GeneratorError, "Pattern '#{generator_name}' matches more than one generator: #{gns.map{|sp|sp.name}.join(', ')}" if gns.length > 1 end @found[generator_name] or raise GeneratorError, "Couldn't find '#{generator_name}' generator" end |
#prepend_sources(*args) ⇒ Object
Add a source to the beginning of the list.
76 77 78 79 |
# File 'lib/rails_generator/lookup.rb', line 76 def prepend_sources(*args) write_inheritable_array(:sources, args.flatten + sources) invalidate_cache! end |
#reset_sources ⇒ Object
Reset the source list.
82 83 84 85 |
# File 'lib/rails_generator/lookup.rb', line 82 def reset_sources write_inheritable_attribute(:sources, []) invalidate_cache! end |
#sources ⇒ Object
The list of sources where we look, in order, for generators.
65 66 67 |
# File 'lib/rails_generator/lookup.rb', line 65 def sources read_inheritable_attribute(:sources) or use_component_sources! end |
#use_application_sources! ⇒ Object
Use application generators (app, ?).
88 89 90 91 |
# File 'lib/rails_generator/lookup.rb', line 88 def use_application_sources! reset_sources sources << PathSource.new(:builtin, "#{File.dirname(__FILE__)}/generators/applications") end |
#use_component_sources! ⇒ Object
Use component generators (model, controller, etc).
-
Rails application. If RAILS_ROOT is defined we know we’re generating in the context of a Rails application, so search RAILS_ROOT/generators.
-
Look in plugins, either for generators/ or rails_generators/ directories within each plugin
-
User home directory. Search ~/.rails/generators.
-
RubyGems. Search for gems named *_generator, and look for generators within any RubyGem’s /rails_generators/<generator_name>_generator.rb file.
-
Builtins. Model, controller, mailer, scaffold, and so on.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rails_generator/lookup.rb', line 104 def use_component_sources! reset_sources if defined? ::RAILS_ROOT sources << PathSource.new(:lib, "#{::RAILS_ROOT}/lib/generators") sources << PathSource.new(:vendor, "#{::RAILS_ROOT}/vendor/generators") Rails.configuration.plugin_paths.each do |path| relative_path = Pathname.new(File.(path)).relative_path_from(Pathname.new(::RAILS_ROOT)) sources << PathSource.new(:"plugins (#{relative_path})", "#{path}/*/**/{,rails_}generators") end end sources << PathSource.new(:user, "#{Dir.user_home}/.rails/generators") if Object.const_defined?(:Gem) sources << GemGeneratorSource.new sources << GemPathSource.new end sources << PathSource.new(:builtin, "#{File.dirname(__FILE__)}/generators/components") end |