Sha256: 089fa511005e356200b189eacf1eabecaf0a2a6e26aa999eed16e9083210e530

Contents?: true

Size: 950 Bytes

Versions: 3

Compression:

Stored size: 950 Bytes

Contents

# frozen_string_literal: true

require "ostruct"
require "deep_merge/core"

module ConfigX
  class Config < OpenStruct
    def initialize(members)
      super()

      members.each do |key, value|
        raise ArgumentError, "option keys should be strings" unless key.respond_to?(:to_s)

        key = key.to_s

        if value.is_a?(Hash)
          value = self.class.new(value)
        elsif value.is_a?(Array)
          value = value.map do |element|
            element.is_a?(Hash) ? self.class.new(element) : element
          end
        end

        self[key] = value.freeze
      end

      freeze
    end

    def with_fallback(fallback)
      DeepMerge.deep_merge!(
        to_h,
        fallback.to_h,
        overwrite_arrays: true
      ).then { Config.new(_1) }
    end

    def to_h
      each_pair.each_with_object({}) do |(key, value), hash|
        hash[key] = value.is_a?(Config) ? value.to_h : value
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
configx-0.3.0 lib/config_x/config.rb
configx-0.2.0 lib/config_x/config.rb
configx-0.1.0 lib/config_x/config.rb