class Flipper::Middleware::SetupEnv

Public Class Methods

new(app, flipper_or_block, options = {}) click to toggle source

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(...) }
# File lib/flipper/middleware/setup_env.rb, line 22
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

Public Instance Methods

call(env) click to toggle source
# File lib/flipper/middleware/setup_env.rb, line 33
def call(env)
  env[@env_key] ||= flipper
  @app.call(env)
end

Private Instance Methods

flipper() click to toggle source
# File lib/flipper/middleware/setup_env.rb, line 40
def flipper
  @flipper ||= @flipper_block.call
end