Class: Aux::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/aux/registry.rb,
lib/aux/registry/entry.rb

Overview

Registry for dependency injection

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



9
10
11
# File 'lib/aux/registry.rb', line 9

def initialize
  @prototypes = ::Concurrent::Map.new
end

Instance Method Details

#key?(key) ⇒ TrueClass, FalseClass

Parameters:

  • key (Symbol, String)

Returns:

  • (TrueClass, FalseClass)


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

def key?(key)
  @prototypes.key?(key.to_s)
end

#register(key, memoize: false, &constructor) ⇒ Object

Parameters:

  • key (Symbol, String)
  • memoize (TrueClass, FalseClass) (defaults to: false)
  • constructor (Proc)


16
17
18
# File 'lib/aux/registry.rb', line 16

def register(key, memoize: false, &constructor)
  @prototypes.put(key.to_s, Entry.new(constructor, memoize))
end

#resolve(key) ⇒ Object

Parameters:

  • key (Symbol, String)

Returns:

  • (Object)


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

def resolve(key)
  @prototypes.fetch(key.to_s).call
end