Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow lazily setting the flipper instance.

  • Loading branch information...
commit f12eaee6fcf4470bbe4352004c8f2a4337e79d02 1 parent 630dd58
@jnunemaker authored
Showing with 32 additions and 3 deletions.
  1. +12 −3 lib/flipper/ui/middleware.rb
  2. +20 −0 spec/flipper-ui/middleware_spec.rb
View
15 lib/flipper/ui/middleware.rb
@@ -9,9 +9,14 @@
module Flipper
module UI
class Middleware
- def initialize(app, flipper)
+ def initialize(app, flipper_or_block)
@app = app
- @flipper = flipper
+
+ if flipper_or_block.respond_to?(:call)
+ @flipper_block = flipper_or_block
+ else
+ @flipper = flipper_or_block
+ end
@action_collection = ActionCollection.new
@action_collection.add UI::Actions::File
@@ -22,6 +27,10 @@ def initialize(app, flipper)
@action_collection.add UI::Actions::Index
end
+ def flipper
+ @flipper ||= @flipper_block.call
+ end
+
def call(env)
dup.call!(env)
end
@@ -33,7 +42,7 @@ def call!(env)
if action_class.nil?
@app.call(env)
else
- action_class.run(@flipper, request)
+ action_class.run(flipper, request)
end
end
end
View
20 spec/flipper-ui/middleware_spec.rb
@@ -26,6 +26,26 @@
end.to_app
}
+ describe "Initializing middleware lazily with a block" do
+ let(:app) {
+ middleware = described_class
+ instance = flipper
+
+ Rack::Builder.new do
+ use middleware, lambda { instance }
+
+ map "/" do
+ run lambda {|env| [404, {}, []] }
+ end
+ end.to_app
+ }
+
+ it "works" do
+ get '/flipper/features'
+ last_response.status.should be(200)
+ end
+ end
+
describe "GET /flipper" do
before do
flipper[:stats].enable
Please sign in to comment.
Something went wrong with that request. Please try again.