Sha256: eda4691b4d39b7db661a3421f4aa130ea6aee9a48834b75f88c4366d6109c19e

Contents?: true

Size: 1.61 KB

Versions: 25

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true
require 'tsort'

module Dynflow
  class Middleware::Resolver

    include TSort
    include Algebrick::TypeCheck

    def initialize(register)
      @register = Type! register, Middleware::Register
    end

    def result
      @result ||= begin
        @deps = normalize_rules(@register.rules)
        self.tsort
      end
    end

    private

    # Takes eliminate :replace and :before rules.
    # Returns hash, that maps middleware classes to their dependencies
    def normalize_rules(rules)
      deps          = Hash.new { |h, k| h[k] = [] }
      substitutions = {}

      # replace before with after on oposite direction and build the
      # substitutions dictionary
      rules.each do |middleware_class, middleware_rules|
        deps[middleware_class].concat(middleware_rules[:after])
        middleware_rules[:before].each do |dependent_class|
          deps[dependent_class] << middleware_class
        end
        middleware_rules[:replace].each do |replaced|
          substitutions[replaced] = middleware_class
        end
      end

      # replace the middleware to be substituted
      substitutions.each do |old, new|
        deps[new].concat(deps[old])
        deps.delete(old)
      end

      # ignore deps, that are not present in the stack
      deps.each do |middleware_class, middleware_deps|
        middleware_deps.reject! { |dep| !deps.has_key?(dep) }
      end

      return deps.delete_if {|klass, _| klass.nil? }
    end

    def tsort_each_node(&block)
      @deps.each_key(&block)
    end

    def tsort_each_child(node, &block)
      @deps.fetch(node).each(&block)
    end

  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
dynflow-1.8.2 lib/dynflow/middleware/resolver.rb
dynflow-1.8.1 lib/dynflow/middleware/resolver.rb
dynflow-1.8.0 lib/dynflow/middleware/resolver.rb
dynflow-1.7.0 lib/dynflow/middleware/resolver.rb
dynflow-1.6.11 lib/dynflow/middleware/resolver.rb
dynflow-1.6.10 lib/dynflow/middleware/resolver.rb
dynflow-1.6.8 lib/dynflow/middleware/resolver.rb
dynflow-1.6.7 lib/dynflow/middleware/resolver.rb
dynflow-1.6.6 lib/dynflow/middleware/resolver.rb
dynflow-1.6.5 lib/dynflow/middleware/resolver.rb
dynflow-1.6.4 lib/dynflow/middleware/resolver.rb
dynflow-1.6.3 lib/dynflow/middleware/resolver.rb
dynflow-1.6.2 lib/dynflow/middleware/resolver.rb
dynflow-1.6.1 lib/dynflow/middleware/resolver.rb
dynflow-1.4.9 lib/dynflow/middleware/resolver.rb
dynflow-1.4.8 lib/dynflow/middleware/resolver.rb
dynflow-1.5.0 lib/dynflow/middleware/resolver.rb
dynflow-1.4.7 lib/dynflow/middleware/resolver.rb
dynflow-1.4.6 lib/dynflow/middleware/resolver.rb
dynflow-1.4.5 lib/dynflow/middleware/resolver.rb