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'
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.
16 17 18 19 |
# File 'lib/begin/template.rb', line 16 def initialize(path) @path = path @path.ensure_dir_exists end |
Instance Method Details
#config ⇒ Object
35 36 37 |
# File 'lib/begin/template.rb', line 35 def config Config.from_file config_path end |
#config_path ⇒ Object
31 32 33 |
# File 'lib/begin/template.rb', line 31 def config_path Path.new CONFIG_NAME, @path, 'Config' end |
#ensure_name_not_empty(source_path, expanded_name) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/begin/template.rb', line 68 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
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/begin/template.rb', line 46 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
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/begin/template.rb', line 57 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
104 105 106 107 |
# File 'lib/begin/template.rb', line 104 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
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/begin/template.rb', line 109 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
76 77 78 79 80 81 82 |
# File 'lib/begin/template.rb', line 76 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
94 95 96 97 98 99 100 101 102 |
# File 'lib/begin/template.rb', line 94 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
84 85 86 87 88 89 90 91 92 |
# File 'lib/begin/template.rb', line 84 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
39 40 41 42 43 44 |
# File 'lib/begin/template.rb', line 39 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
21 22 23 24 |
# File 'lib/begin/template.rb', line 21 def uninstall # Must be implemented in base class raise NotImplementedError end |
#update ⇒ Object
26 27 28 29 |
# File 'lib/begin/template.rb', line 26 def update # Must be implemented in base class raise NotImplementedError end |