module RubiGen::Lookup::ClassMethods
Public Instance Methods
Add a source to the end of the list.
# File lib/rubigen/lookup.rb, line 81 def append_sources(*args) sources.concat(args.flatten) invalidate_cache! end
# File lib/rubigen/lookup.rb, line 106 def application_sources(filters = []) filters.unshift 'app' app_sources = [] app_sources << PathSource.new(:builtin, File.join(File.dirname(__FILE__), %w[.. .. app_generators])) app_sources << filtered_sources(filters) app_sources.flatten end
# File lib/rubigen/lookup.rb, line 139 def filtered_sources(filters) new_sources = [] new_sources << PathFilteredSource.new(:user, "#{Dir.user_home}/.rubigen/", *filters) if Object.const_defined?(:Gem) new_sources << GemPathSource.new(*filters) end new_sources end
Convenience method to lookup and instantiate a generator.
# File lib/rubigen/lookup.rb, line 165 def instance(generator_name, args = [], runtime_options = {}) active.lookup(generator_name).klass.new(args, full_options(runtime_options)) end
Lookup knows how to find generators' Specs from a list of Sources. Searches the sources, in order, for the first matching name.
# File lib/rubigen/lookup.rb, line 150 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
Add a source to the beginning of the list.
# File lib/rubigen/lookup.rb, line 87 def prepend_sources(*args) sources = self.sources reset_sources self.__sources = args.flatten + sources invalidate_cache! end
Reset the source list.
# File lib/rubigen/lookup.rb, line 95 def reset_sources self.__sources = [] invalidate_cache! end
The list of sources where we look, in order, for generators.
# File lib/rubigen/lookup.rb, line 63 def sources if __sources.blank? if superclass == RubiGen::Base superclass_sources = superclass.sources diff = superclass_sources.inject([]) do |mem, source| found = false application_sources.each { |app_source| found ||= true if app_source == source} mem << source unless found mem end self.__sources = diff end active.use_component_sources! if __sources.blank? end __sources end
Use application generators (app, ?).
# File lib/rubigen/lookup.rb, line 101 def use_application_sources!(*filters) reset_sources self.__sources = application_sources(filters) end
Use component generators (test_unit, etc).
-
Current application. If APP_ROOT is defined we know we're generating in the context of this application, so search APP_ROOT/generators.
-
User home directory. Search ~/.rubigen/generators.
-
RubyGems. Search for gems containing /{scope}_generators folder.
-
Builtins. None currently.
Search can be filtered by passing one or more prefixes. e.g. use_component_sources!(:rubygems) means it will also search in the following folders:
-
User home directory. Search ~/.rubigen/rubygems_generators.
-
RubyGems. Search for gems containing /rubygems_generators folder.
# File lib/rubigen/lookup.rb, line 127 def use_component_sources!(*filters) reset_sources new_sources = [] if defined? ::APP_ROOT new_sources << PathSource.new(:root, "#{::APP_ROOT}/generators") new_sources << PathSource.new(:vendor, "#{::APP_ROOT}/vendor/generators") new_sources << PathSource.new(:plugins, "#{::APP_ROOT}/vendor/plugins/*/**/generators") end new_sources << filtered_sources(filters) self.__sources = new_sources.flatten end