Class: Flipper::Middleware::Memoizer
- Inherits:
-
Object
- Object
- Flipper::Middleware::Memoizer
- Defined in:
- lib/flipper/middleware/memoizer.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #flipper ⇒ Object
-
#initialize(app, flipper_or_block) ⇒ Memoizer
constructor
Public: Initializes an instance of the UI middleware.
Constructor Details
#initialize(app, flipper_or_block) ⇒ Memoizer
Public: Initializes an instance of the UI middleware.
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::Memoizer, flipper
# using with a block that yields a flipper instance
use Flipper::Middleware::Memoizer, lambda { Flipper.new(...) }
22 23 24 25 26 27 28 29 30 |
# File 'lib/flipper/middleware/memoizer.rb', line 22 def initialize(app, flipper_or_block) @app = app if flipper_or_block.respond_to?(:call) @flipper_block = flipper_or_block else @flipper = flipper_or_block end end |
Instance Method Details
#call(env) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/flipper/middleware/memoizer.rb', line 36 def call(env) original = flipper.adapter.memoizing? flipper.adapter.memoize = true response = @app.call(env) response[2] = Rack::BodyProxy.new(response[2]) { flipper.adapter.memoize = original } response end |
#flipper ⇒ Object
32 33 34 |
# File 'lib/flipper/middleware/memoizer.rb', line 32 def flipper @flipper ||= @flipper_block.call end |