Sha256: dced8e083f56df4a69cae08dbecdc41733ed00c4d54bb84625bee78cd4bfa93c

Contents?: true

Size: 983 Bytes

Versions: 1

Compression:

Stored size: 983 Bytes

Contents

module Teaspoon
  module Registry
    def self.extended(host)
      host.instance_variable_set(:@registry, {})
      host.instance_variable_set(:@options, {})
    end

    def not_found_in_registry(klass)
      @not_found_exception = klass
    end

    def register(name, constant, path, options = {})
      @registry[normalize_name(name)] = proc {
        require path
        constant.constantize
      }

      @options[normalize_name(name)] = options
    end

    def fetch(name)
      if !(driver = @registry[normalize_name(name)])
        raise not_found_exception.new(name: name, available: available.keys)
      end
      
      driver.call
    end

    def matches?(one, two)
      normalize_name(one) == normalize_name(two)
    end

    def available
      @options
    end

    private

    def normalize_name(name)
      name.to_s.gsub(/[-|\s]/, '_').to_sym
    end

    def not_found_exception
      @not_found_exception || Teaspoon::NotFoundInRegistry
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
teaspoon-1.1.5 lib/teaspoon/registry.rb