Class: Dogviz::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/dogviz/registry.rb

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Registry

Returns a new instance of Registry.



7
8
9
10
11
# File 'lib/dogviz/registry.rb', line 7

def initialize(context)
  @context = context
  @by_name = {}
  @all = []
end

Instance Method Details

#find(&matcher) ⇒ Object

Raises:



22
23
24
25
# File 'lib/dogviz/registry.rb', line 22

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

#find_all(&matcher) ⇒ Object



27
28
29
30
# File 'lib/dogviz/registry.rb', line 27

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

#lookup(name) ⇒ Object

Raises:



32
33
34
35
36
37
# File 'lib/dogviz/registry.rb', line 32

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

#register(name, thing) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/dogviz/registry.rb', line 13

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