Sha256: c8fbb741a6f86a2a01769edb41d95ee3b2d11b9b87ebb458d600e6bd29011d55

Contents?: true

Size: 1.47 KB

Versions: 73

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  # 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.
  class ConfigurationFile # :nodoc:
    class FormatError < StandardError; end

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

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

    def parse(context: nil, **options)
      source = render(context)
      if YAML.respond_to?(:unsafe_load)
        YAML.unsafe_load(source, **options) || {}
      else
        YAML.load(source, **options) || {}
      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

    private
      def read(content_path)
        require "yaml"
        require "erb"

        File.read(content_path).tap do |content|
          if content.include?("\u00A0")
            warn "#{content_path} contains invisible non-breaking spaces, you may want to remove those"
          end
        end
      end

      def render(context)
        erb = ERB.new(@content).tap { |e| e.filename = @content_path }
        context ? erb.result(context) : erb.result
      end
  end
end

Version data entries

73 entries across 69 versions & 10 rubygems

Version Path
activesupport-7.2.0.beta2 lib/active_support/configuration_file.rb
activesupport-7.1.3.4 lib/active_support/configuration_file.rb
activesupport-7.0.8.4 lib/active_support/configuration_file.rb
activesupport-7.2.0.beta1 lib/active_support/configuration_file.rb
activesupport-7.1.3.2 lib/active_support/configuration_file.rb
activesupport-7.1.3.1 lib/active_support/configuration_file.rb
activesupport-7.0.8.1 lib/active_support/configuration_file.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/activesupport-7.1.3/lib/active_support/configuration_file.rb
activesupport-7.1.3 lib/active_support/configuration_file.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/configuration_file.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/configuration_file.rb
activesupport-7.1.2 lib/active_support/configuration_file.rb
activesupport-7.1.1 lib/active_support/configuration_file.rb
activesupport-7.1.0 lib/active_support/configuration_file.rb
activesupport-7.1.0.rc2 lib/active_support/configuration_file.rb
activesupport-7.1.0.rc1 lib/active_support/configuration_file.rb
activesupport-7.1.0.beta1 lib/active_support/configuration_file.rb
activesupport-7.0.8 lib/active_support/configuration_file.rb
activesupport-7.0.7.2 lib/active_support/configuration_file.rb
activesupport-7.0.7.1 lib/active_support/configuration_file.rb