Sha256: a25d1a6ecd755133b6f609865be2fbe3e194cdc18cd7045678d42aac5ebd755e

Contents?: true

Size: 950 Bytes

Versions: 1

Compression:

Stored size: 950 Bytes

Contents

require_relative 'lookup_error.rb'
require_relative 'missing_match_block_error.rb'
require_relative 'duplicate_lookup_error.rb'

module Dogviz
  class Registry
    def initialize(context)
      @context = context
      @by_name = {}
      @all = []
    end

    def register(name, thing)
      @all << thing
      if @by_name.has_key?(name)
        @by_name[name] = DuplicateLookupError.new @context, name
      else
        @by_name[name] = thing
      end
    end

    def find(&matcher)
      raise LookupError.new(@context, "need to provide match block") unless block_given?
      @all.find(&matcher)
    end

    def find_all(&matcher)
      raise MissingMatchBlockError.new(@context) unless block_given?
      @all.select(&matcher)
    end

    def lookup(name)
      found = @by_name[name]
      raise LookupError.new(@context, "could not find '#{name}'") if found.nil?
      raise found if found.is_a?(Exception)
      found
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dogviz-0.0.22 lib/dogviz/registry.rb