Class: ActiveSupport::ConfigurationFile

Inherits:
Object
  • Object
show all
Defined in:
lib/active_support/configuration_file.rb

Overview

Reads a YAML configuration file, evaluating any ERB, then parsing the resulting YAML.

Warns in case of YAML confusing characters, like invisible non-breaking spaces.

Defined Under Namespace

Classes: FormatError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content_path) ⇒ ConfigurationFile

Returns a new instance of ConfigurationFile.



12
13
14
15
# File 'lib/active_support/configuration_file.rb', line 12

def initialize(content_path)
  @content_path = content_path.to_s
  @content = read content_path
end

Class Method Details

.parse(content_path, **options) ⇒ Object



17
18
19
# File 'lib/active_support/configuration_file.rb', line 17

def self.parse(content_path, **options)
  new(content_path).parse(**options)
end

Instance Method Details

#parse(context: nil, **options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_support/configuration_file.rb', line 21

def parse(context: nil, **options)
  source = @content.include?("<%") ? render(context) : @content

  if source == @content
    if YAML.respond_to?(:unsafe_load)
      YAML.unsafe_load_file(@content_path, **options) || {}
    else
      YAML.load_file(@content_path, **options) || {}
    end
  else
    if YAML.respond_to?(:unsafe_load)
      YAML.unsafe_load(source, **options) || {}
    else
      YAML.load(source, **options) || {}
    end
  end
rescue Psych::SyntaxError => error
  raise "YAML syntax error occurred while parsing #{@content_path}. " \
        "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
        "Error: #{error.message}"
end