Sha256: 800156d1fb8c53716b1cb4baddfc0828445a8f94f5f1389b0ca87f914caf76f8

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# coding: utf-8

module Confuse
  class Definition
    def initialize(&block)
      @default_namespace = :default
      block.call(self)
    end

    def defines?(name)
      !!find_item_by_name(name)
    end

    def default(namespace, key)
      (item = find_item(namespace, key)) && item.default
    end

    def add_namespace(name, &block)
      new_namespace = Namespace.new(&block)
      namespaces[name.to_sym] = new_namespace
    end

    def add_item(name, opts = {})
      namespaces[@default_namespace].add_item(name, opts)
    end

    def namespaces
      @namespaces ||= {
        @default_namespace => Namespace.new(&(Proc.new {}))
      }
    end

    def namespace_and_key(name)
      KeySplitter.new(name).split.find { |(ns, _)| namespaces[ns]  } ||
        [nil, name]
    end

    private

    def find_item_by_name(name)
      namespace, key = namespace_and_key(name)
    end

    def find_item(namespace, key)
      (ns = find_namespace(namespace)) && ns[key]
    end

    def find_namespace(name)
      namespaces[name || @default_namespace]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
confuse-1.0.0 lib/confuse/definition.rb