Class: Ashiba::TemplateLoader
- Inherits:
-
Object
- Object
- Ashiba::TemplateLoader
- Defined in:
- lib/ashiba/template_loader.rb
Overview
Loads templates and creates the Template object.
Instance Method Summary collapse
-
#load_template(name) ⇒ Hash
Load the template, including a search in all known locations for templates.
-
#locations ⇒ Array
Return all locations to look for configuration files.
-
#search_template(name) ⇒ String
Search for given template in all known locations.
- #template ⇒ Object
Instance Method Details
#load_template(name) ⇒ Hash
Load the template, including a search in all known locations for templates
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. rescue ::YAML::SyntaxError => e puts e. end # @todo Only for compatibility reasons, should use regular getters later contents['origin'] = config_file contents # @_contents = contents # return template end |
#locations ⇒ Array
Return all locations to look for configuration files.
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.('../../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
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 |
#template ⇒ Object
67 68 69 |
# File 'lib/ashiba/template_loader.rb', line 67 def template Template.new(@_contents) end |