Sha256: a148544bd42a391e1baf2cec2453a5ba8c970aa2a668b26d96dafd7ca885b665

Contents?: true

Size: 764 Bytes

Versions: 1

Compression:

Stored size: 764 Bytes

Contents

module AppConfigLoader
  class ConfigWithIndifferentAccess
    include Enumerable

    def initialize(map, prefix = nil)
      @config_map = map
      @prefix     = prefix
    end

    def get(key)
      # append prefix to the key if needed
      target_key = @prefix ? "#{@prefix}.#{key}" : key.to_s

      # return either nil, the value or another ConfigWithIndifferentAccess depending on
      # what is at the key
      case (entry = @config_map[target_key])
        when ConfigEntry
          entry.value
        when Hash
          self.class.new @config_map, target_key
        else
          nil
      end
    end
    alias_method :[], :get

    def to_a
      @config_map.to_a
    end

    def each(&block)
      @config_map.each(&block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
app_config_loader-1.0.3 lib/app_config_loader/config_with_indifferent_access.rb