Sha256: 75ad104b98ea9203bf359130313c0fa69021c42cdc8deab2279ba49ee8964a05

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# coding: utf-8

module Confuse
  class Config
    def initialize(definition, source)
      @definition = definition
      @source = source
    end

    def respond_to?(name)
      @definition.defines?(name) || super
    end

    def method_missing(name, *_args)
      self[name] if respond_to?(name)
    end

    def [](name)
      namespace, key = @definition.namespace_and_key(name)
      lookup(namespace, key)
    end

    def lookup(namespace, key)
      unless (item = @definition.find_item(namespace, key))
        raise Errors::Undefined.new(key)
      end

      value = @source[namespace, key] || item.default(self)

      item.convert(value)
    end

    # check items have a value. Will raise Undefined error if a required item
    # has no value.
    def check
      @definition.namespaces.each do |(namespace, ns)|
        ns.items.each do |key, _|
          lookup(namespace, key)
        end
      end
    end

    def to_hash
      @definition.to_hash.reduce({}) do |a, (k, v)|
        value_added = v.merge(:value => self[k])
        a.merge({ k => value_added })
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
confuse-1.1.1 lib/confuse/config.rb
confuse-1.1.0 lib/confuse/config.rb