Sha256: 209e652a3b9da3ec7d7867dad43d18fc79e2e7073ab4cbb2279b47edec3e9b24

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

require 'confuse/config_item'

module Confuse
  # A {Namespace} is a container to keep configuration data seperate from the
  # rest of the config.
  class Namespace

    attr_reader :items, :supress_warnings_flag, :strict_flag

    def initialize(&block)
      @items = {}
      @supress_warnings = false
      @strict = false
      instance_eval(&block)
    end

    def define(name, &block)
      @items[name] = ConfigItem.new(name, &block)
    end

    def supress_warnings
      @supress_warnings_flag = true
    end

    def strict
      @strict_flag = true
    end

    def [](key, config = nil)
      value = (i = get_item(key)) && i.value
      if value.respond_to?(:call) && !config.nil?
        value.call(config)
      else
        value
      end
    end

    def []=(key, value)
      item = get_item(key) || create_new_key(key, value)
      item && item.value = value
    end

    def keys
      @items.keys
    end

    def create_new_key(key, value)
      if @supress_warnings_flag
        puts "Warning: config includes unknown option '#{key}'"
      end
      @items[key] = ConfigItem.new(key, &nil) unless @strict_flag
    end

    def get_item(key)
      @items[key]
    end

    def merge!(namespace)
      @strict_flag = namespace.strict
      @supress_warnings_flag = namespace.supress_warnings
      @items.merge! namespace.items
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
confuse-0.1.3 lib/confuse/namespace.rb
confuse-0.1.2 lib/confuse/namespace.rb