Sha256: b075559a54cf9785cc30df870556fefd9f61f05411f502024e47905cbf72f575

Contents?: true

Size: 1.24 KB

Versions: 21

Compression:

Stored size: 1.24 KB

Contents

module Flipper
  module Middleware
    class SetupEnv
      # Public: Initializes an instance of the SetupEnv middleware. Allows for
      # lazy initialization of the flipper instance being set in the env by
      # providing a block.
      #
      # app - The app this middleware is included in.
      # flipper_or_block - The Flipper::DSL instance or a block that yields a
      #                    Flipper::DSL instance to use for all operations.
      #
      # Examples
      #
      #   flipper = Flipper.new(...)
      #
      #   # using with a normal flipper instance
      #   use Flipper::Middleware::SetupEnv, flipper
      #
      #   # using with a block that yields a flipper instance
      #   use Flipper::Middleware::SetupEnv, lambda { Flipper.new(...) }
      #
      def initialize(app, flipper_or_block, options = {})
        @app = app
        @env_key = options.fetch(:env_key, 'flipper')

        if flipper_or_block.respond_to?(:call)
          @flipper_block = flipper_or_block
        else
          @flipper = flipper_or_block
        end
      end

      def call(env)
        env[@env_key] ||= flipper
        @app.call(env)
      end

      private

      def flipper
        @flipper ||= @flipper_block.call
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
flipper-0.19.1 lib/flipper/middleware/setup_env.rb
flipper-0.19.0 lib/flipper/middleware/setup_env.rb
flipper-0.18.0 lib/flipper/middleware/setup_env.rb
flipper-0.17.2 lib/flipper/middleware/setup_env.rb
flipper-0.17.1 lib/flipper/middleware/setup_env.rb
flipper-0.16.2 lib/flipper/middleware/setup_env.rb
flipper-0.16.1 lib/flipper/middleware/setup_env.rb
flipper-0.16.0 lib/flipper/middleware/setup_env.rb
flipper-0.15.0 lib/flipper/middleware/setup_env.rb
flipper-0.14.0 lib/flipper/middleware/setup_env.rb
flipper-0.13.0 lib/flipper/middleware/setup_env.rb
flipper-0.13.0.beta1 lib/flipper/middleware/setup_env.rb
flipper-0.12.2 lib/flipper/middleware/setup_env.rb
flipper-0.12.1 lib/flipper/middleware/setup_env.rb
flipper-0.12.0 lib/flipper/middleware/setup_env.rb
flipper-0.11.0 lib/flipper/middleware/setup_env.rb
flipper-0.11.0.rc1 lib/flipper/middleware/setup_env.rb
flipper-0.11.0.beta9 lib/flipper/middleware/setup_env.rb
flipper-0.11.0.beta8 lib/flipper/middleware/setup_env.rb
flipper-0.11.0.beta7 lib/flipper/middleware/setup_env.rb