Sha256: 23541b97c76d090f0b19e689fd9e45cdf055714309ac368e938031f1c16d4ebb

Contents?: true

Size: 1.47 KB

Versions: 18

Compression:

Stored size: 1.47 KB

Contents

# Allows arbitrary wrapping of the job deserialization and job execution procedures
class Sqewer::MiddlewareStack

  # Returns the default middleware stack, which is empty (an instance of None).
  #
  # @return [MiddlewareStack] the default empty stack
  def self.default
    @instance ||= new
  end

  # Creates a new MiddlewareStack. Once created, handlers can be added using `:<<`
  def initialize
    @handlers = []
  end

  # Adds a handler. The handler should respond to :around_deserialization and #around_execution.
  #
  # @param handler[#around_deserializarion, #around_execution] The middleware item to insert
  # @return [void]
  def <<(handler)
    @handlers << handler
    # TODO: cache the wrapping proc
  end

  def around_execution(job, context, &inner_block)
    return yield if @handlers.empty?

    responders = @handlers.select{|e| e.respond_to?(:around_execution) }
    responders.reverse.inject(inner_block) {|outer_block, middleware_object|
      ->{
        middleware_object.public_send(:around_execution, job, context, &outer_block)
      }
    }.call
  end

  def around_deserialization(serializer, message_id, message_body, &inner_block)
    return yield if @handlers.empty?

    responders = @handlers.select{|e| e.respond_to?(:around_deserialization) }
    responders.reverse.inject(inner_block) {|outer_block, middleware_object|
      ->{ middleware_object.public_send(:around_deserialization, serializer, message_id, message_body, &outer_block) }
    }.call
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
sqewer-6.0.6 lib/sqewer/middleware_stack.rb
sqewer-6.0.5 lib/sqewer/middleware_stack.rb
sqewer-6.0.4 lib/sqewer/middleware_stack.rb
sqewer-6.0.3 lib/sqewer/middleware_stack.rb
sqewer-6.0.2 lib/sqewer/middleware_stack.rb
sqewer-6.0.1 lib/sqewer/middleware_stack.rb
sqewer-6.0.0 lib/sqewer/middleware_stack.rb
sqewer-5.1.1 lib/sqewer/middleware_stack.rb
sqewer-5.1.0 lib/sqewer/middleware_stack.rb
sqewer-5.0.9 lib/sqewer/middleware_stack.rb
sqewer-5.0.8 lib/sqewer/middleware_stack.rb
sqewer-5.0.7 lib/sqewer/middleware_stack.rb
sqewer-5.0.6 lib/sqewer/middleware_stack.rb
sqewer-5.0.5 lib/sqewer/middleware_stack.rb
sqewer-5.0.4 lib/sqewer/middleware_stack.rb
sqewer-5.0.3 lib/sqewer/middleware_stack.rb
sqewer-5.0.2 lib/sqewer/middleware_stack.rb
sqewer-5.0.1 lib/sqewer/middleware_stack.rb