Sha256: 3f170f6730a7641a938795c710f5aa2848e44bee56725ffc49a7851480c41051

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Flipper
  module Middleware
    class SetupEnv
      # Public: Initializes an instance of the SetEnv 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::SetEnv, flipper
      #
      #   # using with a block that yields a flipper instance
      #   use Flipper::Middleware::SetEnv, 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

1 entries across 1 versions & 1 rubygems

Version Path
flipper-0.11.0.beta5 lib/flipper/middleware/setup_env.rb