Class: Ettin::Sources::YamlSource

Inherits:
Ettin::Source show all
Defined in:
lib/ettin/sources/yaml_source.rb

Overview

Config data from a yaml file

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ettin::Source

for, register, register_default, registry

Constructor Details

#initialize(path) ⇒ YamlSource

Returns a new instance of YamlSource.



18
19
20
# File 'lib/ettin/sources/yaml_source.rb', line 18

def initialize(path)
  @path = path
end

Class Method Details

.handles?(_target) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ettin/sources/yaml_source.rb', line 14

def self.handles?(_target)
  true
end

Instance Method Details

#loadObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ettin/sources/yaml_source.rb', line 22

def load
  return {} unless File.exist?(path)
  begin
    YAML.safe_load(ERB.new(File.read(path)).result) || {}
  rescue Psych::SyntaxError => e
    raise "YAML syntax error occurred while parsing #{@path}. " \
      "Please note that YAML must be consistently indented using " \
      "spaces. Tabs are not allowed. " \
      "Error: #{e.message}"
  end
end