Methods
- append_sources
- instance
- lookup
- prepend_sources
- reset_sources
- sources
- use_application_sources!
- use_component_sources!
Public Instance methods
Add a source to the end of the list.
Convenience method to lookup and instantiate a generator.
Lookup knows how to find generators’ Specs from a list of Sources. Searches the sources, in order, for the first matching name.
# File railties/lib/rails_generator/lookup.rb, line 124
124: def lookup(generator_name)
125: @found ||= {}
126: generator_name = generator_name.to_s.downcase
127: @found[generator_name] ||= cache.find { |spec| spec.name == generator_name }
128: unless @found[generator_name]
129: chars = generator_name.scan(/./).map{|c|"#{c}.*?"}
130: rx = /^#{chars}$/
131: gns = cache.select{|spec| spec.name =~ rx }
132: @found[generator_name] ||= gns.first if gns.length == 1
133: raise GeneratorError, "Pattern '#{generator_name}' matches more than one generator: #{gns.map{|sp|sp.name}.join(', ')}" if gns.length > 1
134: end
135: @found[generator_name] or raise GeneratorError, "Couldn't find '#{generator_name}' generator"
136: end
Add a source to the beginning of the list.
Reset the source list.
The list of sources where we look, in order, for generators.
Use application generators (app, ?).
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.
# File railties/lib/rails_generator/lookup.rb, line 104
104: def use_component_sources!
105: reset_sources
106: if defined? ::RAILS_ROOT
107: sources << PathSource.new(:lib, "#{::RAILS_ROOT}/lib/generators")
108: sources << PathSource.new(:vendor, "#{::RAILS_ROOT}/vendor/generators")
109: Rails.configuration.plugin_paths.each do |path|
110: relative_path = Pathname.new(File.expand_path(path)).relative_path_from(Pathname.new(::RAILS_ROOT))
111: sources << PathSource.new("plugins (#{relative_path})""plugins (#{relative_path})", "#{path}/*/**/{,rails_}generators")
112: end
113: end
114: sources << PathSource.new(:user, "#{Dir.user_home}/.rails/generators")
115: if Object.const_defined?(:Gem)
116: sources << GemGeneratorSource.new
117: sources << GemPathSource.new
118: end
119: sources << PathSource.new(:builtin, "#{File.dirname(__FILE__)}/generators/components")
120: end