Sha256: b95321facabd302166de923a3b1fdabdb1f319307feffe9f49f102c1944bd75f

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

module Dynflow
  class Middleware::World
    include Algebrick::TypeCheck

    def initialize
      @register = Middleware::Register.new
      clear_cache!
    end

    def use(*args)
      clear_cache!
      @register.use(*args)
    end

    def execute(method, action_or_class, *args, &block)
      Match! method, :delay, :plan, :run, :finalize, :plan_phase, :finalize_phase, :present, :hook
      if Child? action_or_class, Dynflow::Action
        action = nil
        action_class = action_or_class
      elsif Type? action_or_class, Dynflow::Action
        action = action_or_class
        action_class = action.class
      else
        Algebrick::TypeCheck.error action_or_class, 'is not instance or child class', Dynflow::Action
      end

      classes = middleware_classes(action_class)
      stack   = Middleware::Stack.build(classes, method, action, &block)
      stack.call(*args)
    end

    def clear_cache!
      @middleware_classes_cache = {}
    end

    private

    def cumulate_register(action_class, register = Middleware::Register.new)
      register.merge!(@register)
      unless action_class == Dynflow::Action
        cumulate_register(action_class.superclass, register)
      end
      register.merge!(action_class.middleware)
      return register
    end

    def middleware_classes(action_class)
      unless @middleware_classes_cache.key?(action_class)
        register = cumulate_register(action_class)
        resolver = Dynflow::Middleware::Resolver.new(register)
        @middleware_classes_cache[action_class] = resolver.result
      end
      return @middleware_classes_cache[action_class]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dynflow-1.9.0 lib/dynflow/middleware/world.rb
dynflow-1.8.3 lib/dynflow/middleware/world.rb