Sha256: c4531ced40bb223ecb8f22c32bfe42d18a4c9c6f0e24de9505f9454120649164

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require "pathname"
require "anyway/ext/hash"

using RubyNext
using Anyway::Ext::Hash

module Anyway
  module Loaders
    class YAML < Base
      def call(config_path:, **_options)
        base_config = trace!(:yml, path: relative_config_path(config_path).to_s) { load_base_yml(config_path) }

        return base_config unless use_local?

        local_path = local_config_path(config_path)
        local_config = trace!(:yml, path: relative_config_path(local_path).to_s) { load_local_yml(local_path) }

        base_config.deep_merge!(local_config)
      end

      private

      def parse_yml(path)
        return {} unless File.file?(path)
        require "yaml" unless defined?(::YAML)
        if defined?(ERB)
          ::YAML.safe_load(ERB.new(File.read(path)).result, [], [], true)
        else
          ::YAML.load_file(path)
        end
      end

      alias load_base_yml parse_yml
      alias load_local_yml parse_yml

      def local_config_path(path)
        path.sub(/\.yml/, ".local.yml")
      end

      def relative_config_path(path)
        Pathname.new(path).then do |path|
          return path if path.relative?
          path.relative_path_from(Pathname.new(Dir.pwd))
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
anyway_config-2.0.4 lib/anyway/loaders/yaml.rb
anyway_config-2.0.3 lib/anyway/loaders/yaml.rb
anyway_config-2.0.2 lib/anyway/loaders/yaml.rb
anyway_config-2.0.1 lib/anyway/loaders/yaml.rb
anyway_config-2.0.0 lib/anyway/loaders/yaml.rb