Class: Flipper::Middleware::SetupEnv
- Inherits:
-
Object
- Object
- Flipper::Middleware::SetupEnv
- Defined in:
- lib/flipper/middleware/setup_env.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #call!(env) ⇒ Object
-
#initialize(app, flipper_or_block = nil, options = {}) ⇒ SetupEnv
constructor
Public: Initializes an instance of the SetupEnv middleware.
Constructor Details
#initialize(app, flipper_or_block = nil, options = {}) ⇒ 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
(optional, default: Flipper).
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(...) }
# using default configured Flipper instance
Flipper.configure do |config|
config.default { Flipper.new(...) }
end
use Flipper::Middleware::SetupEnv
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/flipper/middleware/setup_env.rb', line 28 def initialize(app, flipper_or_block = nil, = {}) @app = app @env_key = .fetch(:env_key, 'flipper') if flipper_or_block.respond_to?(:call) @flipper_block = flipper_or_block else @flipper = flipper_or_block || Flipper end end |
Instance Method Details
#call(env) ⇒ Object
39 40 41 |
# File 'lib/flipper/middleware/setup_env.rb', line 39 def call(env) dup.call!(env) end |
#call!(env) ⇒ Object
43 44 45 46 |
# File 'lib/flipper/middleware/setup_env.rb', line 43 def call!(env) env[@env_key] ||= flipper @app.call(env) end |