Class: YMDP::Generator::Templates::Base
- Inherits:
-
Object
- Object
- YMDP::Generator::Templates::Base
- Defined in:
- lib/view.rb
Overview
Basic processing of templates (ERB by default).
Direct Known Subclasses
Instance Attribute Summary collapse
-
#application_path ⇒ Object
Returns the value of attribute application_path.
-
#template_path ⇒ Object
Returns the value of attribute template_path.
-
#view ⇒ Object
Returns the value of attribute view.
Instance Method Summary collapse
-
#append_to_file(file, content) ⇒ Object
Append the content to a file and log that it’s happening.
- #confirm_overwrite(path) ⇒ Object
-
#destination_dir ⇒ Object
The destination directory based on the application_path and type of this object.
-
#destination_filename ⇒ Object
The destination filename based on the view name of this object.
-
#destination_path ⇒ Object
Generate the destination path for this file from its destination directory and its filename.
-
#display_path(path) ⇒ Object
friendlier display of paths.
-
#generate ⇒ Object
Write the template to its destination_path after it has been processed.
-
#initialize(view, params = {}) ⇒ Base
constructor
View name should be a single word that’s valid as a filename.
-
#process_template(content) ⇒ Object
Process this template.
-
#processed_template ⇒ Object
Return the content of this file after it has been processed.
-
#source_filename ⇒ Object
The source filename for the blank template file.
-
#source_path ⇒ Object
Generate the source path from the template directory and the source filename.
-
#write_processed_template ⇒ Object
Write the result of processed_template to the destination path.
Constructor Details
#initialize(view, params = {}) ⇒ Base
View name should be a single word that’s valid as a filename.
36 37 38 39 40 41 42 43 |
# File 'lib/view.rb', line 36 def initialize(view, params={}) @view = view @template_path = params[:template_path] @application_path = params[:application_path] raise ArgumentError.new("template_path is required") unless @template_path raise ArgumentError.new("application_path is required") unless @application_path end |
Instance Attribute Details
#application_path ⇒ Object
Returns the value of attribute application_path.
32 33 34 |
# File 'lib/view.rb', line 32 def application_path @application_path end |
#template_path ⇒ Object
Returns the value of attribute template_path.
32 33 34 |
# File 'lib/view.rb', line 32 def template_path @template_path end |
#view ⇒ Object
Returns the value of attribute view.
32 33 34 |
# File 'lib/view.rb', line 32 def view @view end |
Instance Method Details
#append_to_file(file, content) ⇒ Object
Append the content to a file and log that it’s happening.
89 90 91 92 93 94 |
# File 'lib/view.rb', line 89 def append_to_file(file, content) File.open(file, "a") do |f| $stdout.puts " #{display_path(file)} writing . . ." f.puts content end end |
#confirm_overwrite(path) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/view.rb', line 135 def confirm_overwrite(path) if File.exists?(path) $stdout.puts "File exists: #{File.(path)}" $stdout.print " overwrite? (y/n)" answer = $stdin.gets answer =~ /^y/i else true end end |
#destination_dir ⇒ Object
The destination directory based on the application_path and type of this object.
125 126 127 |
# File 'lib/view.rb', line 125 def destination_dir raise "Define in child" end |
#destination_filename ⇒ Object
The destination filename based on the view name of this object.
119 120 121 |
# File 'lib/view.rb', line 119 def destination_filename raise "Define in child" end |
#destination_path ⇒ Object
Generate the destination path for this file from its destination directory and its filename.
101 102 103 |
# File 'lib/view.rb', line 101 def destination_path "#{destination_dir}/#{destination_filename}" end |
#display_path(path) ⇒ Object
friendlier display of paths
130 131 132 133 |
# File 'lib/view.rb', line 130 def display_path(path) path = File.(path) path.gsub(BASE_PATH, "") end |
#generate ⇒ Object
Write the template to its destination_path after it has been processed.
47 48 49 |
# File 'lib/view.rb', line 47 def generate write_processed_template end |
#process_template(content) ⇒ Object
Process this template. By default templates are processed with ERB.
Override this in a child to change this behavior.
content should be a string, which contains the unprocessed source code of the file.
60 61 62 |
# File 'lib/view.rb', line 60 def process_template(content) ERB.new(content, 0, "%<>").result(binding) end |
#processed_template ⇒ Object
Return the content of this file after it has been processed.
Read the unprocessed content from the source_path and pass it to process_template and return the result.
69 70 71 72 73 74 75 |
# File 'lib/view.rb', line 69 def processed_template unprocessed_content = "" File.open(source_path) do |f| unprocessed_content = f.read end process_template(unprocessed_content) end |
#source_filename ⇒ Object
The source filename for the blank template file.
113 114 115 |
# File 'lib/view.rb', line 113 def source_filename raise "Define in child" end |
#source_path ⇒ Object
Generate the source path from the template directory and the source filename.
107 108 109 |
# File 'lib/view.rb', line 107 def source_path "#{template_path}/#{source_filename}" end |
#write_processed_template ⇒ Object
Write the result of processed_template to the destination path.
81 82 83 84 85 |
# File 'lib/view.rb', line 81 def write_processed_template if confirm_overwrite(destination_path) append_to_file(destination_path, processed_template) end end |