Sha256: f2e7d4dfa20fc8eb75b6503a648d8b46d9d42ca660b028d3a69cba2d6e9ab0e6

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require "deep_merge/core"

module ConfigX
  class Builder
    class << self
      def source(source, **args)
        case source
        in Source then source
        in Hash then HashSource.new(source)
        in String then FileSource.new(source)
        in Pathname then FileSource.new(source)
        in ENV then EnvSource.new(ENV, **args)
        end
      end

      # @see #initialize
      def load(...)
        new(...).load
      end
    end

    # @example
    #   ConfigX.new("production").load
    #   ConfigX.new.load
    #
    def initialize
      @sources = []
    end

    attr_reader :sources

    def add_source(source, **args)
      sources << self.class.source(source, **args)
      self
    end

    # Loads config in the following order:
    #   1. Reads default config
    #   2. Reads all the config files provided in the order
    #   3. Reads environment variables
    def load
      Config.new(read_from_sources)
    end

    def ==(other)
      other.is_a?(self.class) && other.sources == sources
    end

    private def read_from_sources
      sources.each_with_object({}) do |source, config|
        DeepMerge.deep_merge!(source.load, config, overwrite_arrays: true)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
configx-0.5.0 lib/config_x/builder.rb
configx-0.4.0 lib/config_x/builder.rb
configx-0.3.0 lib/config_x/builder.rb
configx-0.2.0 lib/config_x/builder.rb
configx-0.1.0 lib/config_x/builder.rb