Class: FeduxOrgStdlib::TemplateDirectory
- Inherits:
-
Object
- Object
- FeduxOrgStdlib::TemplateDirectory
- Defined in:
- lib/fedux_org_stdlib/template_directory.rb,
lib/fedux_org_stdlib/template_directory/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_directory>
-
$HOME/.config/<application_name>/templates/<template_directory>
-
$HOME/.<application_name>/templates/<template_directory>
-
/etc/<application_name>/templates/<template_directory>
Please keep in mind
-
application_name: Module of your class, e.g. “MyApplication” becomes “my_application”
-
template_directory: Plural name of your class and “TemplateDirectory” strip off, e.g “ClientTemplateDirectory” becomes “clients.d”
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_directory
-
template_directory_basename
-
application_name
If you want the class to look for your template file at a different place overwrite the following method
-
allowed_template_directory_paths
Below you find some examples for the usage of the class:
Defined Under Namespace
Modules: Exceptions
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#template_files ⇒ Object
readonly
Returns the value of attribute template_files.
-
#working_directory ⇒ Object
readonly
Returns the value of attribute working_directory.
Instance Method Summary collapse
-
#initialize(directory: nil, logger: FeduxOrgStdlib::Logging::Logger.new, working_directory: Dir.getwd) ⇒ AppTemplate
constructor
Create a new instance of template.
-
#preferred_template_directory ⇒ String
Return the path to the preferred template file.
Constructor Details
#initialize(directory: nil, logger: FeduxOrgStdlib::Logging::Logger.new, working_directory: Dir.getwd) ⇒ AppTemplate
Create a new instance of template
It tries to find a suitable template directory. If it doesn’t find one the template is empty
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/fedux_org_stdlib/template_directory.rb', line 67 def initialize( directory: nil, logger: FeduxOrgStdlib::Logging::Logger.new, working_directory: Dir.getwd ) @logger = logger @working_directory = working_directory @directory ||= (directory || available_template_directory) fail Exceptions::NoTemplateDirectoryFound, "No template directory found at #{allowed_template_directory_paths.to_list}, therefor I'm stop working as there are methods which depend on an available template directory path." unless @directory begin @template_files = Dir.glob(File.join(@directory, '**', '*')).keep_if { |o| FileTest.file? o } rescue StandardError => e raise Exceptions::TemplateDirectoryNotReadable, JSON.dump(message: e., file: @directory) end end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
50 51 52 |
# File 'lib/fedux_org_stdlib/template_directory.rb', line 50 def directory @directory end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
50 51 52 |
# File 'lib/fedux_org_stdlib/template_directory.rb', line 50 def logger @logger end |
#template_files ⇒ Object (readonly)
Returns the value of attribute template_files.
50 51 52 |
# File 'lib/fedux_org_stdlib/template_directory.rb', line 50 def template_files @template_files end |
#working_directory ⇒ Object (readonly)
Returns the value of attribute working_directory.
50 51 52 |
# File 'lib/fedux_org_stdlib/template_directory.rb', line 50 def working_directory @working_directory end |
Instance Method Details
#preferred_template_directory ⇒ String
Return the path to the preferred template file
89 90 91 |
# File 'lib/fedux_org_stdlib/template_directory.rb', line 89 def preferred_template_directory allowed_template_directory_paths[1] end |