Class: SimpleTemplater::RubyGems

Inherits:
Discoverer show all
Defined in:
lib/simple-templater/discoverers/gems.rb

Instance Attribute Summary

Attributes inherited from Discoverer

#scope

Instance Method Summary collapse

Methods inherited from Discoverer

detect, #initialize

Constructor Details

This class inherits a constructor from SimpleTemplater::Discoverer

Instance Method Details

#find_latest_gem_pathsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/simple-templater/discoverers/gems.rb', line 54

def find_latest_gem_paths
  require "rubygems" unless defined?(Gem)
  # Minigems provides a simpler (and much faster) method for finding the
  # latest gems.
  if Gem.respond_to?(:latest_gem_paths)
    Gem.latest_gem_paths
  else
    gems = Gem.cache.inject(Hash.new) do |latest_gems, cache|
      name, gem = cache
      currently_latest = latest_gems[gem.name]
      latest_gems[gem.name] = gem if currently_latest.nil? or gem.version > currently_latest.version
      latest_gems
    end
    gems.values.map { |gem| gem.full_gem_path }
  end
end

#generator_filesObject



71
72
73
74
75
76
77
# File 'lib/simple-templater/discoverers/gems.rb', line 71

def generator_files
  find_latest_gem_paths.inject(Array.new) do |files, gem_path|
    path = ::File.join(gem_path, "simple-templater.scope")
    files << path if ::File.file?(path)
    files
  end
end

#runObject

Searches installed gems for simple-templater.scope files and loads all code blocks in them that match the given scope.

Parameters

scope<String>

The name of the scope to search for



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/simple-templater/discoverers/gems.rb', line 41

def run
  generator_files.each { |file| load file }
  if SimpleTemplater.scopes[self.scope]
    SimpleTemplater.scopes[self.scope].each do |block|
      begin
        block.call
      rescue Exception => exception
        warn "[Scope #{self.scope}] Exception #{exception.class}: #{exception} occured in scope file #{block.inspect}\n#{exception.backtrace.join("\n")}"
      end
    end
  end
end