Class: Containable::Register

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

Overview

Registers dependencies for future evaluation.

Constant Summary collapse

SEPARATOR =
"."

Instance Method Summary collapse

Constructor Details

#initialize(dependencies = Concurrent::Hash.new, separator: SEPARATOR) ⇒ Register

Returns a new instance of Register.



10
11
12
13
14
15
# File 'lib/containable/register.rb', line 10

def initialize dependencies = Concurrent::Hash.new, separator: SEPARATOR
  @dependencies = dependencies
  @separator = separator
  @keys = []
  @depth = 0
end

Instance Method Details

#call(key, value = nil, &block) ⇒ Object Also known as: register



17
18
19
20
21
22
23
24
25
# File 'lib/containable/register.rb', line 17

def call key, value = nil, &block
  namespaced_key = namespacify key
  message = "Dependency is already registered: #{key.inspect}."

  warn "Registration of value is ignored since block takes precedence." if value && block
  fail KeyError, message if dependencies.key? namespaced_key

  dependencies[namespaced_key] = block || value
end

#namespace(name) ⇒ Object



29
30
31
32
33
# File 'lib/containable/register.rb', line 29

def namespace(name, &)
  keys.clear if depth.zero?
  keys.append name
  visit(&)
end