class FeduxOrgStdlib::FileTemplate

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

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:

If you want the class to look for your template file at a different place overwrite the following method

Below you find some examples for the usage of the class:

@example Create template with one writer and reader

module MyApplication
  class ClientTemplate < FileTemplate
  end
end

@example Template yaml file for the classes above: clients.yaml

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

Attributes

content[R]
file[R]
logger[R]
output_directory[R]
working_directory[R]

Public Class Methods

new( file: nil, logger: FeduxOrgStdlib::Logging::Logger.new, working_directory: Dir.getwd, output_directory: nil ) click to toggle source

Create a new instance of template

It tries to find a suitable template file. If it doesn't find one the template is empty

@param [String] file

Path where template file is stored.

@raise [Exceptions::TemplateFileNotReadable]

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

@return [AppTemplate]

The template instance. If the resulting data structure created by the
template_engine does not respond to `:[]` an empty template object will be
created.
# 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

Private Class Methods

reserved_key_words() click to toggle source
# File lib/fedux_org_stdlib/file_template.rb, line 192
def self.reserved_key_words
  (methods | instance_methods | private_methods | private_instance_methods) - (Class.methods | Class.private_methods) | [:to_s]
end

Public Instance Methods

preferred_template_file() click to toggle source

Return the path to the preferred template file @return [String]

The path to the preferred template file
# File lib/fedux_org_stdlib/file_template.rb, line 96
def preferred_template_file
  allowed_template_file_paths[1]
end
proposed_extname() click to toggle source
# 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_file() click to toggle source
# File lib/fedux_org_stdlib/file_template.rb, line 214
def proposed_file
  File.join output_directory, proposed_file_name
end

Private Instance Methods

allowed_template_file_paths() click to toggle source

The paths where to look for the template file

@return [Array]

A list of paths where the template object should look for its template
file.
# File lib/fedux_org_stdlib/file_template.rb, line 161
def allowed_template_file_paths
  paths = []

  paths << resolve_path(working_directory, 'templates', template_file)
  paths << resolve_path('~', '.config', application_name, 'templates', template_file)
  paths << resolve_path('~', format('.%s', application_name), 'templates', template_file)
  paths << resolve_path('/etc', application_name, 'templates', template_file)
  paths << resolve_path(fallback_template_directory, template_file) if fallback_template_directory

  paths
end
application_name() click to toggle source

The name of your application

@return [String]

This will strip of the class part of fully qualified class name and
converted it to a path.

@example Determine application name

class MyApplication::MyTemplate; end

This will be converted to

my_application
# File lib/fedux_org_stdlib/file_template.rb, line 152
def application_name
  module_name.underscore
end
available_template_file() click to toggle source
# File lib/fedux_org_stdlib/file_template.rb, line 188
def available_template_file
  allowed_template_file_paths.map { |f| Dir.glob(f).first }.compact.find { |f| ::File.exist?(f) }
end
basename() click to toggle source
# File lib/fedux_org_stdlib/file_template.rb, line 196
def basename
  File.basename(file, '.tt')
end
class_name() click to toggle source
# File lib/fedux_org_stdlib/file_template.rb, line 180
def class_name
  self.class.name.to_s.demodulize
end
extname() click to toggle source
# File lib/fedux_org_stdlib/file_template.rb, line 200
def extname
  File.extname(basename)
end
extname?(*ext) click to toggle source
# File lib/fedux_org_stdlib/file_template.rb, line 204
def extname?(*ext)
  ext.any? { |e| e == extname }
end
fallback_template_directory() click to toggle source

Use this path as fall back path

# File lib/fedux_org_stdlib/file_template.rb, line 178
def fallback_template_directory; end
module_name() click to toggle source
# File lib/fedux_org_stdlib/file_template.rb, line 184
def module_name
  self.class.to_s.deconstantize
end
proposed_file_name() click to toggle source
# File lib/fedux_org_stdlib/file_template.rb, line 208
def proposed_file_name
  template_name
end
resolve_path(*path) click to toggle source
# File lib/fedux_org_stdlib/file_template.rb, line 173
def resolve_path(*path)
  ::File.expand_path(::File.join(*path))
end
template_file() click to toggle source

The name of the template file

@return [String]

The name of the template file. It defaults to `<template_name>.yaml`. If
you want to use a different file name you need to overwrite this
method.
# File lib/fedux_org_stdlib/file_template.rb, line 108
def template_file
  "#{template_name}#{template_file_suffix}"
end
template_file_suffix() click to toggle source

The suffix of the template file

@return [String]

The suffix of the template file
# File lib/fedux_org_stdlib/file_template.rb, line 116
def template_file_suffix
  '*.tt'
end
template_name() click to toggle source

The base name of the template

@return [String]

This one returns the base name of the template file (without the file
extension). It uses the class name of the template class

@example Determine the base name of the template

class ClientTemplate; end

This will result in `client` as base name for the template file.

# File lib/fedux_org_stdlib/file_template.rb, line 131
def template_name
  unless (name = class_name.sub(/Template/, '').underscore.singularize).blank?
    return name
  end

  fail Exceptions::ClassNameIsMissing, JSON.dump(klass: class_name)
end