Sha256: 0b162e26075b54630d3621623efb53a9415a5196edb47c3f2068f3c55909bb92

Contents?: true

Size: 906 Bytes

Versions: 5

Compression:

Stored size: 906 Bytes

Contents

require "scorpion/rack/env"

module Scorpion
  module Rack
    class Middleware

      ENV_KEY = "scorpion.instance".freeze

      def initialize( app, nest = nil )
        @app  = app
        @nest = nest
      end

      def call( env )
        # If we don't have a nest yet, build one from the configured global
        # scorpion.
        @nest ||= Scorpion.instance.build_nest

        conceived = false
        env[ENV_KEY] ||=
          begin
            conceived = true
            prepare_scorpion( nest.conceive, env )
          end

        @app.call(env).tap do
          free_scorpion( env ) if conceived
        end
      end

      private

        attr_reader :nest

        def prepare_scorpion( scorpion, env )
          scorpion.hunt_for Rack::Env, return: env
          scorpion
        end

        def free_scorpion( env )
          env[ENV_KEY].destroy
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
scorpion-ioc-1.0.10 lib/scorpion/rack/middleware.rb
scorpion-ioc-1.0.8 lib/scorpion/rack/middleware.rb
scorpion-ioc-1.0.7 lib/scorpion/rack/middleware.rb
scorpion-ioc-1.0.6 lib/scorpion/rack/middleware.rb
scorpion-ioc-1.0.4 lib/scorpion/rack/middleware.rb