Class: Config::Sources::YAMLSource
- Inherits:
-
Object
- Object
- Config::Sources::YAMLSource
- Defined in:
- lib/config/sources/yaml_source.rb
Instance Attribute Summary collapse
-
#evaluate_erb ⇒ Object
readonly
Returns the value of attribute evaluate_erb.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(path, evaluate_erb: Config.evaluate_erb_in_yaml) ⇒ YAMLSource
constructor
A new instance of YAMLSource.
-
#load ⇒ Object
returns a config hash from the YML file.
Constructor Details
#initialize(path, evaluate_erb: Config.evaluate_erb_in_yaml) ⇒ YAMLSource
Returns a new instance of YAMLSource.
10 11 12 13 |
# File 'lib/config/sources/yaml_source.rb', line 10 def initialize(path, evaluate_erb: Config.evaluate_erb_in_yaml) @path = path.to_s @evaluate_erb = !!evaluate_erb end |
Instance Attribute Details
#evaluate_erb ⇒ Object (readonly)
Returns the value of attribute evaluate_erb.
8 9 10 |
# File 'lib/config/sources/yaml_source.rb', line 8 def evaluate_erb @evaluate_erb end |
#path ⇒ Object
Returns the value of attribute path.
7 8 9 |
# File 'lib/config/sources/yaml_source.rb', line 7 def path @path end |
Instance Method Details
#load ⇒ Object
returns a config hash from the YML file
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/config/sources/yaml_source.rb', line 16 def load if @path and File.exist?(@path) file_contents = IO.read(@path) file_contents = ERB.new(file_contents).result if evaluate_erb result = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(file_contents) : YAML.load(file_contents) end 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.}" end |