class Templater::Generator

Attributes

manifold[RW]
arguments[RW]

Instance methods

destination_root[RW]

Instance methods

options[RW]

Instance methods

Public Class Methods

actions() click to toggle source

Returns an Hash that maps the type of action to a list of ActionDescriptions.

Returns

Hash{Symbol=>Array}

A Hash of actions

# File lib/templater/generator.rb, line 50
def actions; @actions ||= {} end
argument(n, name, options={}, &block) click to toggle source

Assign a name to the n:th argument that this generator takes. An accessor with that name will automatically be added to the generator. Options can be provided to ensure the argument conforms to certain requirements. If a block is provided, when an argument is assigned, the block is called with that value and if :invalid is thrown, a proper error is raised

Parameters

n<Integer>

The index of the argument that this describes

name<Symbol>

The name of this argument, an accessor with this name will be created for the argument

options<Hash>

Options for this argument

&block<Proc>

Is evaluated on assignment to check the validity of the argument

Options (opts)

:default<Object>

Specify a default value for this argument

:as<Symbol>

If set to :hash or :array, this argument will 'consume' all remaining arguments and bundle them

Use this only for the last argument to this generator.
:required<Boolean>

If set to true, the generator will throw an error if it initialized without this argument

:desc<Symbol>

Provide a description for this argument

# File lib/templater/generator.rb, line 108
def argument(n, name, options={}, &block)
  self.arguments[n] = ArgumentDescription.new(name.to_sym, options, &block)
  class_eval <<-CLASS
    def #{name}
      get_argument(#{n})
    end

    def #{name}=(arg)
      set_argument(#{n}, arg)
    end
  CLASS
end
arguments() click to toggle source

Returns an array of hashes, where each hash describes a single argument.

Returns

# File lib/templater/generator.rb, line 18
def arguments; @arguments ||= []; end
desc(text = nil) click to toggle source

If the argument is omitted, simply returns the description for this generator, otherwise sets the description to the passed string.

Parameters

text<String>

A description

Returns

String

The description for this generator

# File lib/templater/generator.rb, line 85
def desc(text = nil)
  @text = text.realign_indentation if text
  return @text
end
directories() click to toggle source

Returns an array of ActionDescriptions, where each describes a single directory.

Returns

Array

A list of file descriptions.

# File lib/templater/generator.rb, line 68
def directories; actions[:directories] ||= []; end
directory(name, *args, &block) click to toggle source

Adds a directory that is copied directly. This method is usedful when globbing is not exactly what you want, or you need to access options to decide what source or destination should be.

Parameters

name<Symbol>

The name of this template

source<String>

The source template, can be omitted

destination<String>

The destination where the result will be put.

options<Hash>

Options for this template

&block<Proc>

A block to execute when the generator is instantiated

Options

:before<Symbol>

Name of a method to execute before this template is invoked

:after<Symbol>

Name of a method to execute after this template is invoked

# File lib/templater/generator.rb, line 277
def directory(name, *args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  source, destination = args
  source, destination = source, source if args.size == 1

  directories << ActionDescription.new(name, options) do |generator|
    directory = Actions::Directory.new(generator, name, source, destination, options)
    generator.instance_exec(directory, &block) if block
    directory
  end
end
directory_list(list) click to toggle source

An easy way to add many non-rendering templates to a generator. The provided list can be either an array of Strings or a Here-Doc with templates on individual lines.

Parameters

list<String|Array>

A list of non-rendering templates to be added to this generator

Examples

class MyGenerator < Templater::Generator
  directory_list <<-LIST
    path/to/directory
    another/directory
  LIST
  directory_list ['a/third/directory', 'and/a/fourth']
end
# File lib/templater/generator.rb, line 373
def directory_list(list)
  list = list.to_lines if list.is_a?(String)
  list.each do |item|
    item = item.to_s.chomp.strip
    self.directory(item.gsub(/[\.\/]/, '_').to_sym, item)
  end
end
empty_directories() click to toggle source

Returns an array of ActionDescriptions, where each describes a single empty directory created by generator.

Returns

Array

A list of empty directory descriptions.

# File lib/templater/generator.rb, line 74
def empty_directories; actions[:empty_directories] ||= []; end
empty_directory(name, *args, &block) click to toggle source

Adds an empty directory that will be created when the generator is run.

Parameters

name<Symbol>

The name of this empty directory

destination<String>

The destination where the empty directory will be created

options<Hash>

Options for this empty directory

&block<Proc>

A block to execute when the generator is instantiated

Options

:before<Symbol>

Name of a method to execute before this template is invoked

:after<Symbol>

Name of a method to execute after this template is invoked

# File lib/templater/generator.rb, line 300
def empty_directory(name, *args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  destination = args.first

  empty_directories << ActionDescription.new(name, options) do |generator|
    directory = Actions::EmptyDirectory.new(generator, name, destination, options)
    generator.instance_exec(directory, &block) if block
    directory
  end
end
file(name, *args, &block) click to toggle source

Adds a template that is not rendered using ERB, but copied directly. Unlike ::template this will not append a 't' to the source, otherwise it works identically.

Parameters

name<Symbol>

The name of this template

source<String>

The source template, can be omitted

destination<String>

The destination where the result will be put.

options<Hash>

Options for this template

&block<Proc>

A block to execute when the generator is instantiated

Options

:before<Symbol>

Name of a method to execute before this template is invoked

:after<Symbol>

Name of a method to execute after this template is invoked

# File lib/templater/generator.rb, line 251
def file(name, *args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  source, destination = args
  source, destination = source, source if args.size == 1

  files << ActionDescription.new(name, options) do |generator|
    file = Actions::File.new(generator, name, source, destination, options)
    generator.instance_exec(file, &block) if block
    file
  end
end
file_list(list) click to toggle source

An easy way to add many non-rendering templates to a generator. The provided list can be either an array of Strings or a Here-Doc with templates on individual lines.

Parameters

list<String|Array>

A list of non-rendering templates to be added to this generator

Examples

class MyGenerator < Templater::Generator
  file_list <<-LIST
    path/to/file.jpg
    another/file.html.erb
  LIST
  file_list ['a/third/file.gif', 'and/a/fourth.rb']
end
# File lib/templater/generator.rb, line 350
def file_list(list)
  list = list.to_lines if list.is_a?(String)
  list.each do |item|
    item = item.to_s.chomp.strip
    self.file(item.gsub(/[\.\/]/, '_').to_sym, item)
  end
end
files() click to toggle source

Returns an array of ActionDescriptions, where each describes a single file.

Returns

Array

A list of file descriptions.

# File lib/templater/generator.rb, line 62
def files; actions[:files] ||= []; end
first_argument(*args) click to toggle source

A shorthand method for adding the first argument, see Templater::Generator.argument

# File lib/templater/generator.rb, line 21
def first_argument(*args); argument(0, *args); end
fourth_argument(*args) click to toggle source

A shorthand method for adding the fourth argument, see Templater::Generator.argument

# File lib/templater/generator.rb, line 30
def fourth_argument(*args); argument(3, *args); end
generators() click to toggle source

Returns a list of the classes of all generators (recursively) that are invoked together with this one.

Returns

Array

an array of generator classes.

# File lib/templater/generator.rb, line 414
def generators
  generators = [self]
  if manifold
    generators += invocations.map do |i|
      generator = manifold.generator(i.name)
      generator ? generator.generators : nil
    end
  end
  generators.flatten.compact
end
glob!(dir = nil, template_extensions = %w(rb css js erb html yml Rakefile TODO LICENSE README), options={}) click to toggle source

Search a directory for templates and files and add them to this generator. Any file whose extension matches one of those provided in the template_extensions parameter is considered a template and will be rendered with ERB, all others are considered normal files and are simply copied.

A hash of options can be passed which will be assigned to each file and template. All of these options are matched against the options passed to the generator.

Parameters

source<String>

The directory to search in, relative to the ::source_root, if omitted

the source root itself is searched.
template_destination>

A list of extensions. If a file has one of these

extensions, it is considered a template and will be rendered with ERB.
options<Hash{Symbol=>Object}>

A list of options.

# File lib/templater/generator.rb, line 396
def glob!(dir = nil, template_extensions = %w(rb css js erb html yml Rakefile TODO LICENSE README), options={})
  ::Dir[::File.join(source_root, dir.to_s, '**/*')].each do |action|
    unless ::File.directory?(action)
      action = action.sub("#{source_root}/", '')

      if template_extensions.include?(::File.extname(action.sub(/\.%.+%$/,''))[1..-1]) or template_extensions.include?(::File.basename(action))
        template(action.downcase.gsub(/[^a-z0-9]+/, '_').to_sym, action, action)
      else
        file(action.downcase.gsub(/[^a-z0-9]+/, '_').to_sym, action, action)
      end
    end
  end
end
invoke(name, options={}, &block) click to toggle source

Adds an invocation of another generator to this generator. This allows the interface to invoke any templates in that target generator. This requires that the generator is part of a manifold. The name provided is the name of the target generator in this generator's manifold.

A hash of options can be passed, all of these options are matched against the options passed to the generator.

If a block is given, the generator class is passed to the block, and it is expected that the block yields an instance. Otherwise the target generator is instantiated with the same options and arguments as this generator.

Parameters

name<Symbol>

The name in the manifold of the generator that is to be invoked

options<Hash>

A hash of requirements that are matched against the generator options

&block<Proc>

A block to execute when the generator is instantiated

Examples

class MyGenerator < Templater::Generator
  invoke :other_generator
end

class MyGenerator < Templater::Generator
  def random
    rand(100000).to_s
  end

  # invoke :other_generator with some
  invoke :other_generator do |generator|
    generator.new(destination_root, options, random)
  end
end

class MyGenerator < Templater::Generator
  option :animal
  # other_generator will be invoked only if the option 'animal' is set to 'bear'
  invoke :other_generator, :animal => :bear
end
# File lib/templater/generator.rb, line 183
def invoke(name, options={}, &block)
  self.invocations << InvocationDescription.new(name.to_sym, options, &block)
end
new(destination_root, options = {}, *args) click to toggle source

Create a new generator. Checks the list of arguments agains the requirements set using argument.

Parameters

#destination_root<String>

The destination, where the generated files will be put.

options<Hash{Symbol => Symbol}>

Options given to this generator.

*arguments<String>

The list of arguments. These must match the declared requirements.

Raises

Templater::ArgumentError

If the arguments are invalid

# File lib/templater/generator.rb, line 452
def initialize(destination_root, options = {}, *args)
  @destination_root = destination_root
  @arguments = []
  @options = options

  # Initialize options to their default values.
  self.class.options.each do |option|
    @options[option.name] ||= option.options[:default]
  end

  args.each_with_index do |arg, n|
    set_argument(n, arg)
  end

  self.class.arguments.each_with_index do |argument, i|
    # Initialize arguments to their default values.
    @arguments[i] ||= argument.options[:default]
    # Check if all arguments are valid.
    argument.valid?(@arguments[i])
  end
end
option(name, options={}) click to toggle source

Adds an accessor with the given name to this generator, also automatically fills that value through the options hash that is provided when the generator is initialized.

Parameters

name<Symbol>

The name of this option, an accessor with this name will be created for the option

options<Hash>

Options for this option (how meta!)

Options (opts)

:default<Object>

Specify a default value for this option

:as<Symbol>

If set to :boolean provides a hint to the interface using this generator.

:desc<Symbol>

Provide a description for this option

# File lib/templater/generator.rb, line 132
def option(name, options={})
  self.options << Description.new(name.to_sym, options)
  class_eval <<-CLASS
    def #{name}
      get_option(:#{name})
    end

    def #{name}=(arg)
      set_option(:#{name}, arg)
    end
  CLASS
end
options() click to toggle source

Returns an array of options, where each hash describes a single option.

Returns

# File lib/templater/generator.rb, line 37
def options; @options ||= []; end
second_argument(*args) click to toggle source

A shorthand method for adding the second argument, see Templater::Generator.argument

# File lib/templater/generator.rb, line 24
def second_argument(*args); argument(1, *args); end
source_root() click to toggle source

This should return the directory where source templates are located. This method must be overridden in any Generator inheriting from Templater::Source.

Raises

Templater::SourceNotSpecifiedError

Always raises this error, so be sure to override this method.

# File lib/templater/generator.rb, line 430
def source_root
  raise Templater::SourceNotSpecifiedError, "Subclasses of Templater::Generator must override the source_root method, to specify where source templates are located."
end
template(name, *args, &block) click to toggle source

Adds a template to this generator. Templates are named and can later be retrieved by that name. Templates have a source and a destination. When a template is invoked, the source file is rendered, passing through ERB, and the result is copied to the destination. Source and destination can be specified in different ways, and are always assumed to be relative to ::source_root and destination_root.

If only a destination is given, the source is assumed to be the same destination, only appending the letter 't', so a destination of 'app/model.rb', would assume a source of 'app/model.rbt'

Source and destination can be set in a block, which makes it possible to call instance methods to determine the correct source and/or desination.

A hash of options can be passed, these options are matched against the options passed to the generator. Some special options, for callbacks for example, are also supported, see below.

Parameters

name<Symbol>

The name of this template

source<String>

The source template, can be omitted

destination<String>

The destination where the result will be put.

options<Hash>

Options for this template

&block<Proc>

A block to execute when the generator is instantiated

Options

:before<Symbol>

Name of a method to execute before this template is invoked

:after<Symbol>

Name of a method to execute after this template is invoked

Examples

class MyGenerator < Templater::Generator
  def random
    rand(100000).to_s
  end

  template :one, 'template.rb' # source will be inferred as 'template.rbt'
  template :two, 'source.rbt', 'template.rb' # source expicitly given
  template :three do
    source('source.rbt')
    destination("#{random}.rb")
  end
end
# File lib/templater/generator.rb, line 226
def template(name, *args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  source, destination = args
  source, destination = source + 't', source if args.size == 1

  templates << ActionDescription.new(name, options) do |generator|
    template = Actions::Template.new(generator, name, source, destination, options)
    generator.instance_exec(template, &block) if block
    template
  end
end
template_list(list) click to toggle source

An easy way to add many templates to a generator, each item in the list is added as a template. The provided list can be either an array of Strings or a Here-Doc with templates on individual lines.

Parameters

list<String|Array>

A list of templates to be added to this generator

Examples

class MyGenerator < Templater::Generator
  template_list <<-LIST
    path/to/template1.rb
    another/template.css
  LIST
  template_list ['a/third/template.rb', 'and/a/fourth.js']
end
# File lib/templater/generator.rb, line 327
def template_list(list)
  list = list.to_lines if list.is_a?(String)
  list.each do |item|
    item = item.to_s.chomp.strip
    self.template(item.gsub(/[\.\/]/, '_').to_sym, item)
  end
end
templates() click to toggle source

Returns an array of ActionDescriptions, where each describes a single template.

Returns

Array

A list of template descriptions.

# File lib/templater/generator.rb, line 56
def templates; actions[:templates] ||= []; end
third_argument(*args) click to toggle source

A shorthand method for adding the third argument, see Templater::Generator.argument

# File lib/templater/generator.rb, line 27
def third_argument(*args); argument(2, *args); end

Public Instance Methods

actions(type=nil) click to toggle source

Finds and returns all templates and files for this generators whose options match its options.

Parameters

type<Symbol>

The type of actions to look up (optional)

Returns

[Templater::Actions::*]

The found templates and files.

# File lib/templater/generator.rb, line 548
def actions(type=nil)
  actions = type ? self.class.actions[type] : self.class.actions.values.flatten
  actions.inject([]) do |actions, description|
    actions << description.compile(self) if match_options?(description.options)
    actions
  end
end
after_deletion() click to toggle source
# File lib/templater/generator.rb, line 608
def after_deletion
  # override in subclasses if necessary
end
after_generation() click to toggle source
# File lib/templater/generator.rb, line 604
def after_generation
  # override in subclasses if necessary
end
after_run() click to toggle source
# File lib/templater/generator.rb, line 600
def after_run
  # override in subclasses if necessary
end
all_actions(type=nil) click to toggle source

Finds and returns all templates and files for this generators and any of those generators it invokes, whose options match that generator's options.

Returns

[Templater::Actions::File, Templater::Actions::Template]

The found templates and files.

# File lib/templater/generator.rb, line 561
def all_actions(type=nil)
  all_actions = actions(type)
  all_actions += invocations.map { |i| i.all_actions(type) }
  all_actions.flatten
end
empty_directories() click to toggle source

Finds and returns all empty directories generator creates.

Returns

[Templater::Actions::File]

The found files.

# File lib/templater/generator.rb, line 526
def empty_directories
  actions(:empty_directories)
end
empty_directory(name) click to toggle source

Finds and returns all empty directories whose options match the generator options.

Returns

[Templater::Actions::EmptyDirectory]

The found empty directories that generator creates.

# File lib/templater/generator.rb, line 502
def empty_directory(name)
  empty_directories.find { |d| d.name == name }
end
file(name) click to toggle source

Finds and returns the file of the given name. If that file's options don't match the generator options, returns nil.

Parameters

name<Symbol>

The name of the file to look up.

Returns

Templater::Actions::File

The found file.

# File lib/templater/generator.rb, line 494
def file(name)
  files.find { |f| f.name == name }
end
files() click to toggle source

Finds and returns all files whose options match the generator options.

Returns

[Templater::Actions::File]

The found files.

# File lib/templater/generator.rb, line 518
def files
  actions(:files)
end
invocations() click to toggle source

Finds and returns all templates whose options match the generator options.

Returns

[Templater::Generator]

The found templates.

# File lib/templater/generator.rb, line 534
def invocations
  return [] unless self.class.manifold

  self.class.invocations.map do |invocation|
    invocation.get(self) if match_options?(invocation.options)
  end.compact
end
invoke!() click to toggle source

Invokes the templates for this generator

# File lib/templater/generator.rb, line 568
def invoke!
  all_actions.each { |t| t.invoke! }
end
render!() click to toggle source

Renders all actions in this generator. Use this to verify that rendering templates raises no errors.

Returns

[String]

The results of the rendered actions

# File lib/templater/generator.rb, line 576
def render!
  all_actions.map { |t| t.render }
end
source_root() click to toggle source

Returns this generator's source root

Returns

String

The source root of this generator.

Raises

Templater::SourceNotSpecifiedError

IF the ::source_root class method has not been overridden.

# File lib/templater/generator.rb, line 587
def source_root
  self.class.source_root
end
template(name) click to toggle source

Finds and returns the template of the given name. If that template's options don't match the generator options, returns nil.

Parameters

name<Symbol>

The name of the template to look up.

Returns

Templater::Actions::Template

The found template.

# File lib/templater/generator.rb, line 482
def template(name)
  templates.find { |t| t.name == name }
end
templates() click to toggle source

Finds and returns all templates whose options match the generator options.

Returns

[Templater::Actions::Template]

The found templates.

# File lib/templater/generator.rb, line 510
def templates
  actions(:templates)
end

Protected Instance Methods

get_argument(n) click to toggle source
# File lib/templater/generator.rb, line 624
def get_argument(n)
  @arguments[n]
end
get_option(name) click to toggle source
# File lib/templater/generator.rb, line 632
def get_option(name)
  @options[name]
end
match_options?(options) click to toggle source
# File lib/templater/generator.rb, line 636
def match_options?(options)
  options.all? do |key, value|
    key.to_sym.in?(Templater::ACTION_RESERVED_OPTIONS) or self.send(key) == value
  end
end
set_argument(n, value) click to toggle source
# File lib/templater/generator.rb, line 614
def set_argument(n, value)
  expected = self.class.arguments[n]
  raise Templater::TooManyArgumentsError, "This generator does not take this many Arguments" if expected.nil?

  value = expected.extract(value)

  expected.valid?(value)
  @arguments[n] = value
end
set_option(name, value) click to toggle source
# File lib/templater/generator.rb, line 628
def set_option(name, value)
  @options[name] = value
end