Class: Esse::TemplateLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/esse/template_loader.rb

Constant Summary collapse

EXT_PARSER =
{
  'json' => ->(file) { MultiJson.load(File.read(file)) },
  'yaml' => ->(file) { YAML.load_file(file) },
  'yml' => ->(file) { YAML.load_file(file) },
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(directories, extensions: EXT_PARSER.keys) ⇒ TemplateLoader

Returns a new instance of TemplateLoader.



15
16
17
18
19
20
# File 'lib/esse/template_loader.rb', line 15

def initialize(directories, extensions: EXT_PARSER.keys)
  @directories = Array(directories).map do |dir|
    dir.is_a?(Pathname) ? dir : Pathname.new(dir)
  end
  @extensions = extensions
end

Instance Method Details

#read(*patterns) ⇒ Object

Look for files into the @directories using some file pattern.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/esse/template_loader.rb', line 23

def read(*patterns)
  path = nil
  @directories.each do |dir|
    patterns.find do |pattern|
      path = Dir[dir.join("#{pattern}.{#{@extensions.join(',')}}")].first
      break if path
    end
    break if path
  end
  load(path) if path
end