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.
[ show source ]
# File railties/lib/rails_generator/lookup.rb, line 70 70: def append_sources(*args) 71: sources.concat(args.flatten) 72: invalidate_cache! 73: end
Convenience method to lookup and instantiate a generator.
[ show source ]
# File railties/lib/rails_generator/lookup.rb, line 139 139: def instance(generator_name, args = [], runtime_options = {}) 140: lookup(generator_name).klass.new(args, full_options(runtime_options)) 141: end
Lookup knows how to find generators’ Specs from a list of Sources. Searches the sources, in order, for the first matching name.
[ show source ]
# 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.
[ show source ]
# File railties/lib/rails_generator/lookup.rb, line 76 76: def prepend_sources(*args) 77: write_inheritable_array(:sources, args.flatten + sources) 78: invalidate_cache! 79: end
Reset the source list.
[ show source ]
# File railties/lib/rails_generator/lookup.rb, line 82 82: def reset_sources 83: write_inheritable_attribute(:sources, []) 84: invalidate_cache! 85: end
The list of sources where we look, in order, for generators.
[ show source ]
# File railties/lib/rails_generator/lookup.rb, line 65 65: def sources 66: read_inheritable_attribute(:sources) or use_component_sources! 67: end
Use application generators (app, ?).
[ show source ]
# File railties/lib/rails_generator/lookup.rb, line 88 88: def use_application_sources! 89: reset_sources 90: sources << PathSource.new(:builtin, "#{File.dirname(__FILE__)}/generators/applications") 91: end
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.
[ show source ]
# 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