Module: Stockboy::TemplateFile

Defined in:
lib/stockboy/template_file.rb

Overview

Find and read template files from the configured load paths

Class Method Summary collapse

Class Method Details

.find(filename) ⇒ String

Find a named DSL template from configuration.template_load_paths

Parameters:

  • filename (String)

    Template basename to be searched from load paths

Returns:

  • (String)

    The full path to the first matched filename if found



27
28
29
30
# File 'lib/stockboy/template_file.rb', line 27

def self.find(filename)
  sources = template_file_paths(filename)
  Dir.glob(sources).first
end

.read(template_name) ⇒ String

Read template file contents for defining a new job

Parameters:

  • template_name (String)

    The file basename of a predefined template

Returns:

  • (String)

    Job template DSL or nil if nothing is found



15
16
17
18
19
20
# File 'lib/stockboy/template_file.rb', line 15

def self.read(template_name)
  return template_name.read if template_name.is_a? File
  return unless path = find(template_name)

  File.read(path)
end

.template_file_paths(filename) ⇒ Array

Potential locations for finding a template file

Parameters:

  • filename (String)

    Template basename

Returns:

  • (Array)

    filename on each possible load path



37
38
39
40
41
# File 'lib/stockboy/template_file.rb', line 37

def self.template_file_paths(filename)
  filename = "#{filename}.rb" unless filename =~ /\.rb$/
  load_paths = Array(Stockboy.configuration.template_load_paths)
  load_paths.map { |d| File.join(d, filename) }
end