Class: SimpleTemplater

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-templater/builder.rb,
lib/simple-templater.rb,
lib/simple-templater/dsl.rb,
lib/simple-templater/helpers.rb,
lib/simple-templater/generator.rb,
lib/simple-templater/discoverer.rb,
lib/simple-templater/hooks/hook.rb,
lib/simple-templater/argv_parsing.rb,
lib/simple-templater/generator_set.rb,
lib/simple-templater/discoverers/gems.rb

Overview

This provides a hook system which programs that use Templater can use to discover generators installed through gems. This requires two separate things, the Templater-using progrma will have to call the #discover! method giving a scope, like this:

Templater::Discovery.discover!("name-of-scope")

Where “name-of-scope” should be a string that uniquely identifies your program. Any gem wishing to then add a generator, that is automatically picked up, will then need to add a simple-templater.scope file at the root of the project (don't forget to add it to the gem's manifest of files).

- lib /
- spec /
- Rakefile
- simple-templater.scope

This file should look something like this:

SimpleTemplater.scope(:rango) do
  root = File.dirname(__FILE__)
  Dir["#{root}/stubs/*"].each do |stub_dir|
    if File.directory?(stub_dir)
      SimpleTemplater.register(:rango, stub_dir)
    end
  end
end

Multiple scopes can be added to the same simple-templater.scope file for use with different generator programs.

Defined Under Namespace

Modules: ArgvParsingMixin, Helpers, Hooks Classes: Builder, DSL, Discoverer, FlatBuilder, Generator, GeneratorSet, RubyGems

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, logger = nil) ⇒ SimpleTemplater

Returns a new instance of SimpleTemplater.


69
70
71
72
# File 'lib/simple-templater.rb', line 69

def initialize(scope, logger = nil)
  @scope      = scope
  @generators = Hash.new
end

Instance Attribute Details

#generatorsObject (readonly)

Returns the value of attribute generators


68
69
70
# File 'lib/simple-templater.rb', line 68

def generators
  @generators
end

#scopeObject (readonly)

Returns the value of attribute scope


68
69
70
# File 'lib/simple-templater.rb', line 68

def scope
  @scope
end

Class Method Details

.discover!(scope) ⇒ Object

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


62
63
64
65
66
# File 'lib/simple-templater.rb', line 62

def self.discover!(scope)
  klass = Discoverer.detect
  discoverer = klass.new(scope)
  discoverer.run
end

.generatorsObject

scope => generator rango: GeneratorSet.new(:project, *paths)


37
38
39
# File 'lib/simple-templater.rb', line 37

def self.generators
  @generators ||= Hash.new
end

.loggerObject


20
21
22
23
24
25
# File 'lib/simple-templater.rb', line 20

def self.logger
  @@logger ||= begin
    require "logger"
    Logger.new(STDOUT)
  end
end

.logger=(logger) ⇒ Object


27
28
29
# File 'lib/simple-templater.rb', line 27

def self.logger=(logger)
  @@logger = logger
end

.register(scope, name, *paths) ⇒ Object


52
53
54
55
# File 'lib/simple-templater.rb', line 52

def self.register(scope, name, *paths)
  self.generators[scope] ||= Array.new
  self.generators[scope].push(GeneratorSet.new(name, *paths))
end

.scope(scope, &block) ⇒ Object

Adds a block of code specific for a certain scope of generators, where the scope would probably be the name of the program running the generator.

Parameters

scope<String>

The name of the scope

block<&Proc>

A block of code to execute provided the scope is correct


47
48
49
50
# File 'lib/simple-templater.rb', line 47

def self.scope(scope, &block)
  self.scopes[scope] ||= Array.new
  self.scopes[scope] << block
end

.scopesObject


31
32
33
# File 'lib/simple-templater.rb', line 31

def self.scopes
  @scopes ||= Hash.new
end

Instance Method Details

#discover!Object


78
79
80
# File 'lib/simple-templater.rb', line 78

def discover!
  SimpleTemplater.discover!(self.scope)
end

#find(name) ⇒ Object


91
92
93
94
95
# File 'lib/simple-templater.rb', line 91

def find(name)
  self.generators.find do |generator|
    generator.name == name.to_sym
  end
end

#loggerObject


74
75
76
# File 'lib/simple-templater.rb', line 74

def logger
  @logger ||= standard_logger
end

#register(name, path) ⇒ Object

templater.register(:project, path)


87
88
89
# File 'lib/simple-templater.rb', line 87

def register(name, path)
  SimpleTemplater.register(self.scope, name, path)
end