Class: Containable::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/containable/resolver.rb

Overview

Resolves previously registered dependencies.

Instance Method Summary collapse

Constructor Details

#initialize(dependencies = Concurrent::Hash.new) ⇒ Resolver

Returns a new instance of Resolver.



8
9
10
# File 'lib/containable/resolver.rb', line 8

def initialize dependencies = Concurrent::Hash.new
  @dependencies = dependencies
end

Instance Method Details

#call(key) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/containable/resolver.rb', line 12

def call key
  normalized_key = key.to_s

  value = dependencies.fetch normalized_key do
    fail KeyError, "Unable to resolve dependency: #{key.inspect}."
  end

  value.is_a?(Proc) && value.arity.zero? ? dependencies[normalized_key] = value.call : value
end