Class: FeduxOrgStdlib::FileTemplate
- Inherits:
-
Object
- Object
- FeduxOrgStdlib::FileTemplate
- Defined in:
- lib/fedux_org_stdlib/file_template.rb,
lib/fedux_org_stdlib/file_template/exceptions.rb
Overview
This class makes a template file available as an object. You can use whatever template language you prefer. It’s up to you to compile the template with a suitable template parser.
By default it will look for a suitable template file in the given order:
-
<current working directory>/templates/<template_file>.tt
-
$HOME/.config/<application_name>/templates/<template_file>.tt
-
$HOME/.<application_name>/templates/<template_file>.tt
-
/etc/<application_name>/templates/<template_file>.tt
Please keep in mind
-
application_name: Module of your class, e.g. “MyApplication” becomes “my_application”
-
template_file: Singular name of your class and “Template” strip off, e.g “ClientTemplate” becomes “client.tt”
Most conventions defined by me are implemented as separate methods. If one convention is not suitable for your use case, just overwrite the method.
If you prefer to use a different path to the template file or name of the template file one of the following methods needs to be overwritten:
-
template_file
-
template_name
-
application_name
If you want the class to look for your template file at a different place overwrite the following method
-
allowed_template_file_paths
Below you find some examples for the usage of the class:
Defined Under Namespace
Modules: Exceptions
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#output_directory ⇒ Object
readonly
Returns the value of attribute output_directory.
-
#working_directory ⇒ Object
readonly
Returns the value of attribute working_directory.
Instance Method Summary collapse
-
#initialize(file: nil, logger: FeduxOrgStdlib::Logging::Logger.new, working_directory: Dir.getwd, output_directory: nil) ⇒ AppTemplate
constructor
Create a new instance of template.
-
#preferred_template_file ⇒ String
Return the path to the preferred template file.
- #proposed_extname ⇒ Object
- #proposed_file ⇒ Object
Constructor Details
#initialize(file: nil, logger: FeduxOrgStdlib::Logging::Logger.new, working_directory: Dir.getwd, output_directory: nil) ⇒ AppTemplate
Create a new instance of template
It tries to find a suitable template file. If it doesn’t find one the template is empty
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fedux_org_stdlib/file_template.rb', line 73 def initialize( file: nil, logger: FeduxOrgStdlib::Logging::Logger.new, working_directory: Dir.getwd, output_directory: nil ) @logger = logger @working_directory = working_directory @output_directory = output_directory || working_directory @file ||= available_template_file fail Exceptions::NoTemplateFileFound, "No template file found at #{allowed_template_file_paths.to_list}, therefor I'm stop working as there are methods which depend on an available template file path." unless @file begin @content = File.read(@file).chomp rescue StandardError => e fail Exceptions::TemplateFileNotReadable, JSON.dump(message: e., file: @file) end end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
56 57 58 |
# File 'lib/fedux_org_stdlib/file_template.rb', line 56 def content @content end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
56 57 58 |
# File 'lib/fedux_org_stdlib/file_template.rb', line 56 def file @file end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
56 57 58 |
# File 'lib/fedux_org_stdlib/file_template.rb', line 56 def logger @logger end |
#output_directory ⇒ Object (readonly)
Returns the value of attribute output_directory.
56 57 58 |
# File 'lib/fedux_org_stdlib/file_template.rb', line 56 def output_directory @output_directory end |
#working_directory ⇒ Object (readonly)
Returns the value of attribute working_directory.
56 57 58 |
# File 'lib/fedux_org_stdlib/file_template.rb', line 56 def working_directory @working_directory end |
Instance Method Details
#preferred_template_file ⇒ String
Return the path to the preferred template file
97 98 99 |
# File 'lib/fedux_org_stdlib/file_template.rb', line 97 def preferred_template_file allowed_template_file_paths[1] end |
#proposed_extname ⇒ Object
220 221 222 223 224 225 226 |
# File 'lib/fedux_org_stdlib/file_template.rb', line 220 def proposed_extname ext = File.extname(basename) return '.erb' if ext.blank? ext end |
#proposed_file ⇒ Object
216 217 218 |
# File 'lib/fedux_org_stdlib/file_template.rb', line 216 def proposed_file File.join output_directory, proposed_file_name end |