Sha256: 7b59679bcde331b502b84ca9ddd63f18554e068758d650937d26328ab895867a

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

require 'singleton'
require 'sugar-high/kind_of'

module CanTango
  class Configuration
    module Registry
      class Hash < Base
        def types= *types
          raise "This is a Hash registry!"
        end

        def types
          [Hash]
        end

        def value_methods
          []
        end

        def value_types
          []
        end

        def clean!
          registered = Hashie::Mash.new
        end
        alias_method :clear!, :clean!

        def default!
          @registered = default
        end

        def << hash
          raise "Must be a hash" if !hash.is_a? Hash
          registered.merge!(hash) and return if value_methods.empty? && value_types.empty?
          hash.each_pair do |key, value|
            registered[key] = value if value_api.all?{|m| value.respond_to(m)} && value.any_kind_of?(value_types)
          end
        end

        def [] label
          raise "Must be a label" if !label.kind_of_label?
          registered[label.to_s]
        end

        def []= label, value
          raise "Must be a label" if !label.kind_of_label?
          registered[label.to_s] = value
        end

        def register hash
          raise "Must be a hash" if !hash.is_a? Hash
          registered.merge! hash
        end

        def unregister name
          @registered = {} if name == :all
          @registered.delete(name)
        end

        def registered
          @registered ||= default
        end

        def registered_names
          registered.keys
        end

        def registered_values
          registered.values
        end

        def registered? label
          registered_names.map(&:to_s).include? label.to_s
        end
        alias_method :registered_name, :registered?

        def registered_value? value
          registered_values.include? value
        end

        def default
          @default ||= Hashie::Mash.new
        end

        def default= hash
          @default = Hashie::Mash.new hash
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cantango-config-0.1.1 lib/cantango/configuration/registry/hash.rb