Class: Philiprehberger::Middleware::FrozenStack

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/middleware/stack.rb

Overview

An immutable snapshot of a middleware stack that can execute but not be modified.

Instance Method Summary collapse

Constructor Details

#initialize(entries, groups, disabled_groups, before_hooks, after_hooks, around_hooks) ⇒ FrozenStack

Returns a new instance of FrozenStack.



509
510
511
512
513
514
515
516
# File 'lib/philiprehberger/middleware/stack.rb', line 509

def initialize(entries, groups, disabled_groups, before_hooks, after_hooks, around_hooks)
  @entries = entries.freeze
  @groups = groups.freeze
  @disabled_groups = disabled_groups.freeze
  @before_hooks = before_hooks.freeze
  @after_hooks = after_hooks.freeze
  @around_hooks = around_hooks.freeze
end

Instance Method Details

#[](target_name) ⇒ #call?

Look up an entry by name.

Parameters:

  • target_name (String, Symbol)

    name of the entry

Returns:

  • (#call, nil)

    the middleware callable, or nil if not found



548
549
550
551
# File 'lib/philiprehberger/middleware/stack.rb', line 548

def [](target_name)
  entry = @entries.find { |e| e.name == target_name }
  entry&.middleware
end

#call(env) ⇒ Object

Execute the frozen middleware stack with the given environment.

Parameters:

  • env (Object)

    the environment/context passed through the stack

Returns:

  • (Object)

    the final environment after all middleware have run



522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/philiprehberger/middleware/stack.rb', line 522

def call(env)
  disabled = disabled_middleware_names
  terminal = ->(e) { e }
  chain = @entries.reverse.reduce(terminal) do |next_mw, entry|
    if entry.name && disabled.include?(entry.name)
      next_mw
    else
      build_frozen_step(entry, next_mw)
    end
  end
  chain.call(env)
rescue Philiprehberger::Middleware::Halt
  env
end

#to_aArray<String, Symbol, nil>

Return the list of middleware names.

Returns:

  • (Array<String, Symbol, nil>)

    names of middleware in order



540
541
542
# File 'lib/philiprehberger/middleware/stack.rb', line 540

def to_a
  @entries.map(&:name)
end