Class: Limelight::Templates::Templater
- Inherits:
-
Object
- Object
- Limelight::Templates::Templater
- Defined in:
- lib/limelight/templates/templater.rb
Overview
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.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#logger ⇒ Object
See TemplaterLogger.
-
#source_root ⇒ Object
readonly
Returns the value of attribute source_root.
-
#target_root ⇒ Object
readonly
Returns the value of attribute target_root.
Class Method Summary collapse
-
.clarify(path) ⇒ Object
Carifies a path as relative or absolute.
-
.source_dir ⇒ Object
Return the default source_root for Limelight related file templates.
Instance Method Summary collapse
-
#directory(path) ⇒ Object
Creates a deirectory.
-
#file(target, source, tokens = {}) ⇒ Object
Creates the specified file from the specified file template.
-
#initialize(target_root, source_root = Templater.source_dir) ⇒ Templater
constructor
New instances Templater require a target_root.
Constructor Details
#initialize(target_root, source_root = Templater.source_dir) ⇒ Templater
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
47 48 49 50 51 |
# File 'lib/limelight/templates/templater.rb', line 47 def initialize(target_root, source_root=Templater.source_dir) @logger = TemplaterLogger.new @target_root = Templater.clarify(target_root) @source_root = source_root end |
Instance Attribute Details
#logger ⇒ Object
See TemplaterLogger
40 41 42 |
# File 'lib/limelight/templates/templater.rb', line 40 def logger @logger end |
#source_root ⇒ Object (readonly)
Returns the value of attribute source_root.
36 37 38 |
# File 'lib/limelight/templates/templater.rb', line 36 def source_root @source_root end |
#target_root ⇒ Object (readonly)
Returns the value of attribute target_root.
36 37 38 |
# File 'lib/limelight/templates/templater.rb', line 36 def target_root @target_root end |
Class Method Details
.clarify(path) ⇒ Object
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'
30 31 32 33 34 |
# File 'lib/limelight/templates/templater.rb', line 30 def self.clarify(path) return path if path[0..0] == '.' return path if path == File.(path) return File.join(".", path) end |
.source_dir ⇒ Object
Return the default source_root for Limelight related file templates.
$LIMELIGHT_LIB$/limelight/templates/sources
20 21 22 |
# File 'lib/limelight/templates/templater.rb', line 20 def self.source_dir return File.join(File.dirname(__FILE__), "sources") end |
Instance Method Details
#directory(path) ⇒ Object
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
67 68 69 70 |
# File 'lib/limelight/templates/templater.rb', line 67 def directory(path) full_path = File.join(@target_root, path) establish_directory(full_path) end |
#file(target, source, tokens = {}) ⇒ Object
Creates the specified file from the specified file template. The file will be created withint 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, ...
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/limelight/templates/templater.rb', line 88 def file(target, source, tokens = {}) target_path = File.join(@target_root, target) source_source = File.join(@source_root, source) establish_directory(File.dirname(target_path)) if File.exists?(target_path) @logger.file_already_exists(target_path) else @logger.creating_file(target_path) content = IO.read(source_source) content = replace_tokens(content, tokens) File.open(target_path, 'w') { |file| file.write content } end end |