Class: PipeFitter::YamlLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/pipe_fitter/yaml_loader.rb

Instance Method Summary collapse

Constructor Details

#initializeYamlLoader

Returns a new instance of YamlLoader.



7
8
9
# File 'lib/pipe_fitter/yaml_loader.rb', line 7

def initialize
  @search_path = [Pathname.new(".")]
end

Instance Method Details

#include_template(filename, context = {}) ⇒ Object



23
24
25
26
27
# File 'lib/pipe_fitter/yaml_loader.rb', line 23

def include_template(filename, context = {})
  dir = @search_path.find { |p| p.join(filename).exist? }
  path = dir.nil? ? filename : dir.join(filename)
  eval_erb(path, context).gsub("\n", "\n" + " " * (context[:indent] || 0))
end

#load(filename) ⇒ Object



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

def load(filename)
  @search_path.unshift(Pathname.new(filename).dirname)
  text = eval_erb(filename)
  YAML.load(text) || {}
rescue Psych::SyntaxError => e
  text.split("\n").each_with_index do |l, i|
    mark = (e.line == i + 1) ? "*" : " "
    $stderr.puts format("%s%4d| %s", mark, i + 1, l)
  end
  raise e
end