Class: FeduxOrgStdlib::TemplateDirectory

Inherits:
Object
  • Object
show all
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:

  1. <current working directory>/templates/<template_directory>

  2. $HOME/.config/<application_name>/templates/<template_directory>

  3. $HOME/.<application_name>/templates/<template_directory>

  4. /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:

Examples:

Create template with one writer and reader

module MyApplication
  class ClientTemplateDirectory < FileTemplate
  end
end

Defined Under Namespace

Modules: Exceptions

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • directory (String) (defaults to: nil)

    Path where template directory is stored.

Raises:

  • (Exceptions::TemplateFileNotReadable)

    If an avaiable template directory could not be read by the template engine



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.message, file: @directory)
  end
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



50
51
52
# File 'lib/fedux_org_stdlib/template_directory.rb', line 50

def directory
  @directory
end

#loggerObject (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_filesObject (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_directoryObject (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_directoryString

Return the path to the preferred template file

Returns:

  • (String)

    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