Sha256: 29c8dde0f323c5bc1b4377a820f0dad911947e3265519da250baf0dfd5e30dc2

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 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)
      (i = get_item(key)) && i.value
    end

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

    def create_new_key(key, value)
      if @supress_warnings_flag
        puts "Warning: config includes unknown option '#{key}'"
      end
      @items[key] = ConfigItem.new(key, &(Proc.new {})) 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

1 entries across 1 versions & 1 rubygems

Version Path
confuse-0.0.1 lib/confuse/namespace.rb