Class: FeduxOrgStdlib::FileTemplate

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

  1. <current working directory>/templates/<template_file>.tt

  2. $HOME/.config/<application_name>/templates/<template_file>.tt

  3. $HOME/.<application_name>/templates/<template_file>.tt

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

Examples:

Create template with one writer and reader

module MyApplication
  class ClientTemplate < FileTemplate
  end
end

Template yaml file for the classes above: clients.yaml

<%= hello %>
---
option1: 'data2'

Defined Under Namespace

Modules: Exceptions

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • file (String) (defaults to: nil)

    Path where template file is stored.

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fedux_org_stdlib/file_template.rb', line 72

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 ||= (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
    raise Exceptions::TemplateFileNotReadable, JSON.dump(message: e.message, file: @file)
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



55
56
57
# File 'lib/fedux_org_stdlib/file_template.rb', line 55

def content
  @content
end

#fileObject (readonly)

Returns the value of attribute file.



55
56
57
# File 'lib/fedux_org_stdlib/file_template.rb', line 55

def file
  @file
end

#loggerObject (readonly)

Returns the value of attribute logger.



55
56
57
# File 'lib/fedux_org_stdlib/file_template.rb', line 55

def logger
  @logger
end

#output_directoryObject (readonly)

Returns the value of attribute output_directory.



55
56
57
# File 'lib/fedux_org_stdlib/file_template.rb', line 55

def output_directory
  @output_directory
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



55
56
57
# File 'lib/fedux_org_stdlib/file_template.rb', line 55

def working_directory
  @working_directory
end

Instance Method Details

#preferred_template_fileString

Return the path to the preferred template file

Returns:

  • (String)

    The path to the preferred template file



96
97
98
# File 'lib/fedux_org_stdlib/file_template.rb', line 96

def preferred_template_file
  allowed_template_file_paths[1]
end

#proposed_extnameObject



218
219
220
221
222
223
224
# File 'lib/fedux_org_stdlib/file_template.rb', line 218

def proposed_extname
  ext = File.extname(basename)

  return '.erb' if ext.blank?

  ext
end

#proposed_fileObject



214
215
216
# File 'lib/fedux_org_stdlib/file_template.rb', line 214

def proposed_file
  File.join output_directory, proposed_file_name
end