Class: Begin::Template
- Inherits:
-
Object
- Object
- Begin::Template
- Defined in:
- lib/begin/template.rb
Overview
Represents an installed template on the user’s machine.
Direct Known Subclasses
Constant Summary collapse
- CONFIG_NAME =
'.begin.yml'.freeze
Instance Method Summary collapse
- #config ⇒ Object
- #config_path ⇒ Object
- #ensure_name_not_empty(source_path, expanded_name) ⇒ Object
- #ensure_no_back_references(source_path, expanded_path, target_dir) ⇒ Object
- #ensure_no_conflicts(paths, source_path, target_path) ⇒ Object
-
#initialize(path) ⇒ Template
constructor
A new instance of Template.
- #process_file(source_path, target_path, context) ⇒ Object
- #process_files(paths, context) ⇒ Object
- #process_path_name(source_path, target_dir, context) ⇒ Object
- #process_path_names(source_dir, target_dir, context) ⇒ Object
- #process_path_names_in_dir(source, target, paths, working_set, context) ⇒ Object
- #run(target_dir, context) ⇒ Object
- #uninstall ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(path) ⇒ Template
Returns a new instance of Template.
14 15 16 17 |
# File 'lib/begin/template.rb', line 14 def initialize(path) @path = path @path.ensure_dir_exists end |
Instance Method Details
#config ⇒ Object
33 34 35 |
# File 'lib/begin/template.rb', line 33 def config Config.from_file config_path end |
#config_path ⇒ Object
29 30 31 |
# File 'lib/begin/template.rb', line 29 def config_path Path.new CONFIG_NAME, @path, 'Config' end |
#ensure_name_not_empty(source_path, expanded_name) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/begin/template.rb', line 64 def ensure_name_not_empty(source_path, ) return unless .empty? err = "Mustache evaluation resulted in an empty file name...\n" err += "... whilst evaluating: #{source_path}" raise err end |
#ensure_no_back_references(source_path, expanded_path, target_dir) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/begin/template.rb', line 44 def ensure_no_back_references(source_path, , target_dir) return if target_dir.contains? err = 'Backward-reference detected in expanded ' \ "template path. Details to follow.\n" err += "Source Path: #{source_path}\n" err += "Expanded Path: #{}\n" err += "Expected Parent: #{target_dir}\n" raise err end |
#ensure_no_conflicts(paths, source_path, target_path) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/begin/template.rb', line 54 def ensure_no_conflicts(paths, source_path, target_path) return unless paths.key? target_path err = "File path collision detected. Details to follow.\n" err += "(1) Source File: #{source_path}\n" err += "(1) ..Writes To: #{target_path}\n" err += "(2) Source File: #{paths[target_path]}\n" err += "(2) ..Writes To: #{target_path}\n" raise err end |
#process_file(source_path, target_path, context) ⇒ Object
99 100 101 102 |
# File 'lib/begin/template.rb', line 99 def process_file(source_path, target_path, context) contents = File.read source_path File.write target_path, Mustache.render(contents, context) end |
#process_files(paths, context) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/begin/template.rb', line 104 def process_files(paths, context) paths.each do |target, source| target.make_parent_dirs if source.directory? target.make_dir else process_file source, target, context end end end |
#process_path_name(source_path, target_dir, context) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/begin/template.rb', line 71 def process_path_name(source_path, target_dir, context) = Mustache.render source_path.basename, context ensure_name_not_empty source_path, = Path.new , target_dir, 'Target' ensure_no_back_references source_path, , target_dir end |
#process_path_names(source_dir, target_dir, context) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/begin/template.rb', line 89 def process_path_names(source_dir, target_dir, context) paths = {} working_set = [[source_dir, target_dir]] until working_set.empty? source, target = working_set.pop process_path_names_in_dir source, target, paths, working_set, context end paths end |
#process_path_names_in_dir(source, target, paths, working_set, context) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/begin/template.rb', line 79 def process_path_names_in_dir(source, target, paths, working_set, context) source.dir_contents.each do |entry| source_path = Path.new entry, '.', 'Source' target_path = process_path_name source_path, target, context ensure_no_conflicts paths, source_path, target_path paths[target_path] = source_path working_set.push [source_path, target_path] if source_path.directory? end end |
#run(target_dir, context) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/begin/template.rb', line 37 def run(target_dir, context) target_dir = Path.new target_dir, '.', 'Directory' target_dir.ensure_dir_exists paths = process_path_names @path, target_dir, context process_files paths, context end |
#uninstall ⇒ Object
19 20 21 22 |
# File 'lib/begin/template.rb', line 19 def uninstall # Must be implemented in base class raise NotImplementedError end |
#update ⇒ Object
24 25 26 27 |
# File 'lib/begin/template.rb', line 24 def update # Must be implemented in base class raise NotImplementedError end |