rory 0.3.16 → 0.3.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -95,8 +95,29 @@ module Rory
95
95
  @db.loggers << logger
96
96
  end
97
97
 
98
+ def use_middleware(*args, &block)
99
+ @middleware << [args, block]
100
+ end
101
+
102
+ def middleware
103
+ @middleware ||= []
104
+ end
105
+
106
+ def dispatcher
107
+ Rory::Dispatcher.rack_app(self)
108
+ end
109
+
110
+ def stack
111
+ builder = Rack::Builder.new
112
+ middleware.each do |args, block|
113
+ builder.use *args, &block
114
+ end
115
+ builder.run dispatcher
116
+ builder
117
+ end
118
+
98
119
  def call(env)
99
- Rory::Dispatcher.new(Rack::Request.new(env), self).dispatch
120
+ stack.call(env)
100
121
  end
101
122
 
102
123
  def logger
@@ -9,6 +9,12 @@ module Rory
9
9
  @app = app
10
10
  end
11
11
 
12
+ def self.rack_app(app)
13
+ Proc.new { |env|
14
+ new(Rack::Request.new(env), app).dispatch
15
+ }
16
+ end
17
+
12
18
  def route
13
19
  @routing[:route] ||= get_route
14
20
  end
data/lib/rory/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rory
2
- VERSION = '0.3.16'
2
+ VERSION = '0.3.17'
3
3
  end
@@ -0,0 +1,13 @@
1
+ class DummyMiddleware
2
+ attr_accessor :prefix
3
+
4
+ def initialize(app, *args, &block)
5
+ @app = app
6
+ @args = args
7
+ block.call(self)
8
+ end
9
+
10
+ def call(env)
11
+ @app.call("#{prefix} #{@args.first}")
12
+ end
13
+ end
@@ -111,4 +111,20 @@ describe Rory::Application do
111
111
  Fixture::Application.require_all_files
112
112
  end
113
113
  end
114
+
115
+ describe '.use_middleware' do
116
+ it 'adds the given middleware to the stack, retaining args and block' do
117
+ require Fixture::Application.root.join('lib', 'dummy_middleware')
118
+ Fixture::Application.use_middleware DummyMiddleware, :puppy do |dm|
119
+ dm.prefix = 'a salubrious'
120
+ end
121
+
122
+ expect(Fixture::Application.instance).to receive(:dispatcher).
123
+ and_return(dispatch_stack_mock = double)
124
+ expect(dispatch_stack_mock).to receive(:call).
125
+ with('a salubrious puppy')
126
+ Fixture::Application.call({})
127
+ Fixture::Application.middleware.clear
128
+ end
129
+ end
114
130
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.16
4
+ version: 0.3.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -211,6 +211,7 @@ files:
211
211
  - spec/fixture_app/controllers/goose/lumpies_controller.rb
212
212
  - spec/fixture_app/controllers/goose/wombat/rabbits_controller.rb
213
213
  - spec/fixture_app/controllers/stub_controller.rb
214
+ - spec/fixture_app/lib/dummy_middleware.rb
214
215
  - spec/fixture_app/views/for_reals/but_wait.html.erb
215
216
  - spec/fixture_app/views/for_reals/srsly.html.erb
216
217
  - spec/fixture_app/views/layouts/surround.html.erb