Sha256: 146ea8c6306544888647c65930d55bac2d5c4a4079d02f808529f353ac69127a

Contents?: true

Size: 1.55 KB

Versions: 12

Compression:

Stored size: 1.55 KB

Contents

require 'tap/support/dependency'

module Tap
  module Support
    
    # Dependencies tracks Executable dependencies and results, and provides
    # for thread-safe resolution of dependencies.
    class Dependencies < Monitor
      
      # Initializes a new Dependencies
      def initialize
        super 
        @resolve_stack = []
      end
      
      # Thread-safe registration of instance as a dependency.  During
      # registration, instance is extended with the Dependency module.
      # Returns self.
      def register(instance)
        synchronize do
          unless instance.kind_of?(Dependency)
            instance.extend Dependency
          end
        end
        self
      end
      
      # Thread-safe resolution of the instance.  Resolve checks for
      # circular dependencies, then yields control to the block,
      # which is responsible for the actual resolution.
      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
      
      # Raised when Dependencies#resolve detects a circular dependency.
      class CircularDependencyError < StandardError
        def initialize(resolve_stack)
          super "circular dependency: [#{resolve_stack.join(', ')}]"
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
bahuvrihi-tap-0.10.8 lib/tap/support/dependencies.rb
bahuvrihi-tap-0.11.0 lib/tap/support/dependencies.rb
bahuvrihi-tap-0.11.1 lib/tap/support/dependencies.rb
bahuvrihi-tap-0.11.2 lib/tap/support/dependencies.rb
bahuvrihi-tap-0.12.0 lib/tap/support/dependencies.rb
tap-0.12.2 lib/tap/support/dependencies.rb
tap-0.11.1 lib/tap/support/dependencies.rb
tap-0.12.1 lib/tap/support/dependencies.rb
tap-0.12.0 lib/tap/support/dependencies.rb
tap-0.12.4 lib/tap/support/dependencies.rb
tap-0.12.3 lib/tap/support/dependencies.rb
tap-0.11.0 lib/tap/support/dependencies.rb