Module: RubiGen::Lookup::ClassMethods

Defined in:
lib/rubigen/lookup.rb

Instance Method Summary collapse

Instance Method Details

#append_sources(*args) ⇒ Object

Add a source to the end of the list.



80
81
82
83
# File 'lib/rubigen/lookup.rb', line 80

def append_sources(*args)
  sources.concat(args.flatten)
  invalidate_cache!
end

#application_sources(filters = []) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/rubigen/lookup.rb', line 105

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

#filtered_sources(filters) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/rubigen/lookup.rb', line 138

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

#instance(generator_name, args = [], runtime_options = {}) ⇒ Object

Convenience method to lookup and instantiate a generator.



164
165
166
# File 'lib/rubigen/lookup.rb', line 164

def instance(generator_name, args = [], runtime_options = {})
  active.lookup(generator_name).klass.new(args, full_options(runtime_options))
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.



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rubigen/lookup.rb', line 149

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.



86
87
88
89
90
91
# File 'lib/rubigen/lookup.rb', line 86

def prepend_sources(*args)
  sources = self.sources
  reset_sources
  write_inheritable_array(:sources, args.flatten + sources)
  invalidate_cache!
end

#reset_sourcesObject

Reset the source list.



94
95
96
97
# File 'lib/rubigen/lookup.rb', line 94

def reset_sources
  write_inheritable_attribute(:sources, [])
  invalidate_cache!
end

#sourcesObject

The list of sources where we look, in order, for generators.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rubigen/lookup.rb', line 62

def sources
  if read_inheritable_attribute(: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
      write_inheritable_attribute(:sources, diff)
    end
    active.use_component_sources! if read_inheritable_attribute(:sources).blank?
  end
  read_inheritable_attribute(:sources)
end

#use_application_sources!(*filters) ⇒ Object

Use application generators (app, ?).



100
101
102
103
# File 'lib/rubigen/lookup.rb', line 100

def use_application_sources!(*filters)
  reset_sources
  write_inheritable_attribute(:sources, application_sources(filters))
end

#use_component_sources!(*filters) ⇒ Object

Use component generators (test_unit, etc).

  1. Current application. If APP_ROOT is defined we know we’re generating in the context of this application, so search APP_ROOT/generators.

  2. User home directory. Search ~/.rubigen/generators.

  3. RubyGems. Search for gems containing /scope_generators folder.

  4. 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:

  1. User home directory. Search ~/.rubigen/rubygems_generators.

  2. RubyGems. Search for gems containing /rubygems_generators folder.



126
127
128
129
130
131
132
133
134
135
136
# File 'lib/rubigen/lookup.rb', line 126

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)
  write_inheritable_attribute(:sources, new_sources.flatten)
end