Class: Ashiba::TemplateLoader

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

Overview

Loads templates and creates the Template object.

Instance Method Summary collapse

Instance Method Details

#load_template(name) ⇒ Hash

Load the template, including a search in all known locations for templates

Parameters:

  • Name/Path (String)

    of the template

Returns:

  • (Hash)

    Configuration data, only for compatibility reasons

Raises:

  • (Error)

    If file was not found

  • (YAML::SyntaxError)

    If file was invalid



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ashiba/template_loader.rb', line 48

def load_template(name)
  config_file = search_template(name)

  begin
    contents = ::YAML.load_file(config_file)
  rescue Errno::ENOENT => e
    puts e.message
  rescue ::YAML::SyntaxError => e
    puts e.message
  end

  # @todo Only for compatibility reasons, should use regular getters later
  contents['origin'] = config_file
  contents

  # @_contents = contents
  # return template
end

#locationsArray

Return all locations to look for configuration files.

Returns:

  • (Array)

    Possible paths for configuration



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ashiba/template_loader.rb', line 11

def locations
  dirs = []

  # Add any templates in plugin packs
  template_gems = Gem::Specification.latest_specs(true).select { |g| g.name =~ /^ashiba-/ }
  template_gems.each do |spec|
    dirs << File.join(spec.base_dir, 'gems', "#{spec.name}-#{spec.version}", 'templates')
  end

  # Bundled templates last, so they can be overridden by identically named gems
  dirs << File.expand_path('../../templates', __dir__)
end

#search_template(name) ⇒ String

Search for given template in all known locations. May be given a path as well, to directly reference a file

Parameters:

  • Name/Path (String)

    of template

Returns:

  • (String)

    Full path of the template, if found

Raises:

  • (SomeError)

    If no template could be found



30
31
32
33
34
35
36
37
38
39
# File 'lib/ashiba/template_loader.rb', line 30

def search_template(name)
  return name if File.file?(name)

  locations.each do |path|
    expected = "#{path}/#{name}.yaml"
    return expected if File.file?(expected)
  end

  raise "Template #{name} not found. Search path: #{locations.join(', ')}"
end

#templateObject



67
68
69
# File 'lib/ashiba/template_loader.rb', line 67

def template
  Template.new(@_contents)
end