Class Limelight::Templates::Templater
In: lib/limelight/templates/templater.rb
Parent: Object

A class to create directories and file templates. An instance of Templater must be provided with a target_root and a source_root. The target_root designates a root directory in which all directories and files will be created. The source_root designated a directory where all the file template can be found.

A file template is a plain text file. It may optionally contain token markers in the format !-TOKEN_NAME-!. When a file template is installed by the templater, all the token margers will be replaced by tokens provided in a hash.

Methods

clarify   directory   file   new   source_dir  

Attributes

logger  [RW]  See TemplaterLogger
source_root  [R] 
target_root  [R] 

Public Class methods

Carifies a path as relative or absolute. Essentially if makes sure a path begins with a . if it‘s not an absolute path.

  Templater.clarity('some/path') -> './some/path'
  Templater.clarity('/root/path') -> '/root/path'

[Source]

    # File lib/limelight/templates/templater.rb, line 33
33:       def self.clarify(path)
34:         return path if path[0..0] == '.'
35:         return path if path == File.expand_path(path)
36:         return File.join(".", path)
37:       end

New instances Templater require a target_root. The source_root may optionally be provided. source_root defaults to Templater.source_dir

The logger is initializes as a TemplaterLogger

[Source]

    # File lib/limelight/templates/templater.rb, line 50
50:       def initialize(target_root, source_root=Templater.source_dir)
51:         @logger = TemplaterLogger.new
52:         @target_root = Templater.clarify(target_root)
53:         @source_root = source_root
54:       end

Return the default source_root for Limelight related file templates.

$LIMELIGHT_LIB$/limelight/templates/sources

[Source]

    # File lib/limelight/templates/templater.rb, line 23
23:       def self.source_dir
24:         return File.join(File.dirname(__FILE__), "sources")
25:       end

Public Instance methods

Creates a deirectory. If the specified directory‘s parent directory is missing, it will be created as will its parent directory, and so on.

After the following call,

  templater.directory("dir1/dir2/dir3/dir4")

The following directories will exist, inside the target_root, whether they existed prior to the call or not.

  dir1
  dir1/dir2
  dir1/dir2/dir3
  dir1/dir2/dir3/dir4

[Source]

    # File lib/limelight/templates/templater.rb, line 70
70:       def directory(path)
71:         full_path = File.join(@target_root, path)
72:         establish_directory(full_path)
73:       end

Creates the specified file from the specified file template. The file will be created within the target_root. All parent diretories will be created if needed. The source paramter should be a path pointing to a file template in the source_root directory.

Assume the the file src/default.txt.template exists in the source_root with the following content.

  !-SCORES-! score and !-YEARS-! years ago, ...

When the following command is executed,

  templater.file('dir/foo.txt', 'src/default.txt.template', :SCORES => "Four", :YEARS => "seven")

The file dir/foo.txt will exist in the target_root with the following content.

  Four score and seven years ago, ...

[Source]

     # File lib/limelight/templates/templater.rb, line 91
 91:       def file(target, source, tokens = {})
 92:         target_path = File.join(@target_root, target)
 93:         source_source = File.join(@source_root, source)
 94: 
 95:         establish_directory(File.dirname(target_path))
 96: 
 97:         if File.exists?(target_path)
 98:           @logger.file_already_exists(target_path)
 99:         else
100:           @logger.creating_file(target_path)
101:           content = IO.read(source_source)
102:           content = replace_tokens(content, tokens)
103:           File.open(target_path, 'w') { |file| file.write content }
104:         end
105:       end

[Validate]