Class: Tap::Support::Dependencies

Inherits:
Monitor
  • Object
show all
Defined in:
lib/tap/support/dependencies.rb

Overview

Dependencies tracks Executable dependencies and results, and provides for thread-safe resolution of dependencies.

Defined Under Namespace

Classes: CircularDependencyError

Instance Method Summary collapse

Constructor Details

#initializeDependencies

Initializes a new Dependencies



11
12
13
14
# File 'lib/tap/support/dependencies.rb', line 11

def initialize
  super 
  @resolve_stack = []
end

Instance Method Details

#register(instance) ⇒ Object

Thread-safe registration of instance as a dependency. During registration, instance is extended with the Dependency module. Returns self.



19
20
21
22
23
24
25
26
# File 'lib/tap/support/dependencies.rb', line 19

def register(instance)
  synchronize do
    unless instance.kind_of?(Dependency)
      instance.extend Dependency
    end
  end
  self
end

#resolve(instance) ⇒ Object

Thread-safe resolution of the instance. Resolve checks for circular dependencies, then yields control to the block, which is responsible for the actual resolution.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tap/support/dependencies.rb', line 31

def resolve(instance)
  synchronize do
    if @resolve_stack.include?(instance)
      raise CircularDependencyError.new(@resolve_stack)
    end
    
    # mark the results at the index to prevent
    # infinite loops with circular dependencies
    @resolve_stack.push instance
    yield()
    @resolve_stack.pop
  end
  self
end