Sha256: ba57e66471b5cd7124d2b75881130b3a35de646d163fd15f1a64061d7a212c48

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module Emittance
  ##
  # Module for managing middlewares.
  #
  class Middleware
    @registered_middlewares = []

    class << self
      attr_reader :registered_middlewares

      # @param middleware [Class, Array<#up, #down>] the middleware or array of middlewares you wish to register.
      # @return [Array] the updated list of registered middlewares.
      def register(middleware)
        middlewares = Array(middleware)

        middlewares.each { |mw| registered_middlewares << mw }
      end

      def clear_registrations!
        registered_middlewares.clear
      end

      def up(input_event)
        registered_middlewares.reduce(input_event) { |event, klass| klass.new(event).up }
      end

      def down(input_event)
        registered_middlewares.reduce(input_event) { |event, klass| klass.new(event).down }
      end
    end

    attr_reader :event

    def initialize(event)
      @event = event
    end

    def up
      event
    end

    def down
      event
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
emittance-2.0.0.pre.1 lib/emittance/middleware.rb