meddleware 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +29 -0
- data/LICENSE.txt +21 -0
- data/README.md +146 -0
- data/lib/meddleware/stack.rb +192 -0
- data/lib/meddleware/{v2_5.rb → v2_7.rb} +3 -3
- data/lib/meddleware/version.rb +2 -2
- data/lib/meddleware.rb +15 -178
- data/meddleware.gemspec +29 -0
- metadata +12 -31
- data/spec/build_meddleware_spec.rb +0 -479
- data/spec/call_meddleware_chain_spec.rb +0 -74
- data/spec/call_meddleware_spec.rb +0 -266
- data/spec/readme/full_example_spec.rb +0 -60
- data/spec/readme/usage_spec.rb +0 -47
@@ -1,266 +0,0 @@
|
|
1
|
-
describe 'Meddleware#call' do
|
2
|
-
subject { Meddleware.new }
|
3
|
-
|
4
|
-
let(:middleware) { Meddler.new }
|
5
|
-
ruby3 = RUBY_VERSION >= '3'
|
6
|
-
|
7
|
-
it 'works with no block, no stack' do
|
8
|
-
expect(subject.call).to be nil
|
9
|
-
end
|
10
|
-
|
11
|
-
it 'returns the blocks value' do
|
12
|
-
res = subject.call { 123 }
|
13
|
-
expect(res).to be 123
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'works with args' do
|
17
|
-
res = subject.call(:abc) { 123 }
|
18
|
-
expect(res).to be 123
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'passes args through to block' do
|
22
|
-
subject.call(:abc, x: :yz) do |*args, **kwargs|
|
23
|
-
expect(args).to eq [ :abc ]
|
24
|
-
expect(kwargs).to eq x: :yz
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'with a middleware class' do
|
29
|
-
it 'instantiates and calls the middleware' do
|
30
|
-
expect(Meddler).to receive(:new).and_return(middleware)
|
31
|
-
expect(middleware).to receive(:call)
|
32
|
-
|
33
|
-
subject.use Meddler
|
34
|
-
subject.call
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'with a middleware instance' do
|
39
|
-
before do
|
40
|
-
subject.use middleware
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'calls middleware' do
|
44
|
-
expect(middleware).to receive(:call)
|
45
|
-
subject.call
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'calls middleware with args' do
|
49
|
-
expect(middleware).to receive(:call).with(:abc)
|
50
|
-
subject.call(:abc)
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'calls middleware with block' do
|
54
|
-
expect(middleware).to receive(:call) do |&block|
|
55
|
-
expect(block).to be_a Proc
|
56
|
-
end
|
57
|
-
|
58
|
-
subject.call {}
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'calls middleware with args, kwargs, and block' do
|
62
|
-
expect(middleware).to receive(:call) do |a, b:, &block|
|
63
|
-
expect(a).to be :a
|
64
|
-
expect(b).to eq(:c)
|
65
|
-
expect(block).to be_a Proc
|
66
|
-
end
|
67
|
-
|
68
|
-
subject.call(:a, b: :c) {}
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'can return a value' do
|
72
|
-
expect(middleware).to receive(:call) { 123 }
|
73
|
-
|
74
|
-
expect(subject.call).to be 123
|
75
|
-
end
|
76
|
-
|
77
|
-
context 'when middleware calls block without explicit arguments' do
|
78
|
-
before do
|
79
|
-
expect(middleware).to receive(:call) do |&block|
|
80
|
-
block.call
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'implicitly passes along original arguments' do
|
85
|
-
subject.call(:abc, x: :yz) do |*args, **kwargs|
|
86
|
-
expect(args).to eq [ :abc ]
|
87
|
-
expect(kwargs).to eq x: :yz
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context 'when middleware calls block with explicit arguments' do
|
93
|
-
it 'can add arguments' do
|
94
|
-
expect(middleware).to receive(:call) do |&block|
|
95
|
-
block.call(:abc, x: :yz)
|
96
|
-
end
|
97
|
-
|
98
|
-
subject.call do |*args, **kwargs|
|
99
|
-
expect(args).to eq [ :abc ]
|
100
|
-
expect(kwargs).to eq x: :yz
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
it 'can override the arguments passed on' do
|
105
|
-
expect(middleware).to receive(:call) do |&block|
|
106
|
-
block.call(:abc, x: :z)
|
107
|
-
end
|
108
|
-
|
109
|
-
subject.call(123, x: :y) do |*args, **kwargs|
|
110
|
-
expect(args).to eq [ :abc ]
|
111
|
-
expect(kwargs).to eq x: :z
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'can remove arguments' do
|
116
|
-
expect(middleware).to receive(:call) do |&block|
|
117
|
-
block.call(nil)
|
118
|
-
end
|
119
|
-
|
120
|
-
subject.call(:abc, x: :yz) do |arg, **kwargs|
|
121
|
-
expect(arg).to be nil
|
122
|
-
expect(kwargs).to be_empty
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
context 'when middleware meddles with pass-by-ref arguments' do
|
128
|
-
before do
|
129
|
-
expect(middleware).to receive(:call) do |arg, &block|
|
130
|
-
arg[:abc] = 123
|
131
|
-
block.call
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
it 'alters the value for the block' do
|
136
|
-
subject.call({}) do |info|
|
137
|
-
expect(info).to eq({ abc: 123 })
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
it 'alters the value for the caller' do
|
142
|
-
info = {}
|
143
|
-
subject.call(info)
|
144
|
-
expect(info).to eq({ abc: 123 })
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
context 'when middleware meddles with pass-by-value arguments' do
|
149
|
-
before do
|
150
|
-
expect(middleware).to receive(:call) do |arg, &block|
|
151
|
-
arg = 123
|
152
|
-
block.call
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
it 'has no effect...unfortunately' do
|
157
|
-
subject.call(:abc) do |arg|
|
158
|
-
expect(arg).to be :abc
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
context 'with a middleware instance and arguments' do
|
165
|
-
before do
|
166
|
-
subject.use middleware, :abc, x: :yz
|
167
|
-
end
|
168
|
-
|
169
|
-
it 'curries the arguments' do
|
170
|
-
expect(middleware).to receive(:call) do |*args, **kwargs, &block|
|
171
|
-
expect(args).to eq [ :abc ]
|
172
|
-
expect(kwargs).to eq x: :yz
|
173
|
-
end
|
174
|
-
|
175
|
-
subject.call
|
176
|
-
end
|
177
|
-
|
178
|
-
it 'curries and appends extra arguments', if: ruby3 do
|
179
|
-
expect(middleware).to receive(:call) do |*args, **kwargs, &block|
|
180
|
-
expect(args).to eq [ :abc, :def ]
|
181
|
-
expect(kwargs).to eq x: :yz, y: :z
|
182
|
-
end
|
183
|
-
|
184
|
-
subject.call(:def, y: :z)
|
185
|
-
end
|
186
|
-
|
187
|
-
it 'curries and appends, without yielding implicitly' do
|
188
|
-
res = subject.call(:xyz) { 123 }
|
189
|
-
expect(res).to be nil
|
190
|
-
end
|
191
|
-
|
192
|
-
it 'curries and appends, and can yield explicitly' do
|
193
|
-
expect(middleware).to receive(:call) do |*args, &block|
|
194
|
-
block.call
|
195
|
-
end
|
196
|
-
|
197
|
-
res = subject.call(:xyz) { 123 }
|
198
|
-
expect(res).to be 123
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
context 'with a middleware Proc' do
|
203
|
-
it 'passes arguments to the Proc' do
|
204
|
-
fn = proc {|arg| expect(arg).to be :abc }
|
205
|
-
expect(fn).to receive(:call).and_call_original
|
206
|
-
|
207
|
-
subject.use fn
|
208
|
-
subject.call(:abc)
|
209
|
-
end
|
210
|
-
|
211
|
-
it 'curries arguments' do
|
212
|
-
fn = proc {|*args| expect(args).to eq [ :xyz, :abc ] }
|
213
|
-
expect(fn).to receive(:call).and_call_original
|
214
|
-
|
215
|
-
subject.use fn, :xyz
|
216
|
-
subject.call(:abc)
|
217
|
-
end
|
218
|
-
|
219
|
-
it 'can alter arguments' do
|
220
|
-
fn = proc {|data| data[:abc] = 123 }
|
221
|
-
subject.use fn
|
222
|
-
|
223
|
-
data = {}
|
224
|
-
subject.call(data)
|
225
|
-
expect(data).to eq({ abc: 123 })
|
226
|
-
end
|
227
|
-
|
228
|
-
it 'can not abort middleware chain...unfortunately' do
|
229
|
-
fn = proc { return }
|
230
|
-
|
231
|
-
subject.use fn
|
232
|
-
expect {
|
233
|
-
subject.call
|
234
|
-
}.to raise_error(LocalJumpError)
|
235
|
-
end
|
236
|
-
|
237
|
-
it 'calls yield implicitly' do
|
238
|
-
fn = proc {}
|
239
|
-
expect(fn).to receive(:call).and_call_original
|
240
|
-
|
241
|
-
subject.use fn
|
242
|
-
res = subject.call { 123 }
|
243
|
-
expect(res).to be 123
|
244
|
-
end
|
245
|
-
end
|
246
|
-
|
247
|
-
context 'with a middleware Lambda' do
|
248
|
-
it 'does not explicitly call yield' do
|
249
|
-
fn = ->{}
|
250
|
-
expect(fn).to receive(:call).and_call_original
|
251
|
-
|
252
|
-
subject.use fn
|
253
|
-
res = subject.call { 123 }
|
254
|
-
expect(res).to be nil
|
255
|
-
end
|
256
|
-
|
257
|
-
it 'does can explicitly yield' do
|
258
|
-
fn = ->(&block) { block.call }
|
259
|
-
expect(fn).to receive(:call).and_call_original
|
260
|
-
|
261
|
-
subject.use fn
|
262
|
-
res = subject.call { 123 }
|
263
|
-
expect(res).to be 123
|
264
|
-
end
|
265
|
-
end
|
266
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
module MyList
|
2
|
-
# generate an array from 1 to n
|
3
|
-
|
4
|
-
extend self
|
5
|
-
|
6
|
-
def middleware(&block)
|
7
|
-
(@middleware ||= Meddleware.new).tap do
|
8
|
-
@middleware.instance_eval(&block) if block_given?
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def generate(n)
|
13
|
-
# invoke middleware chain
|
14
|
-
middleware.call(n) do |n|
|
15
|
-
# do the actual work of generating your results
|
16
|
-
(1..n).to_a
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
class OneExtra
|
22
|
-
def call(n)
|
23
|
-
# adds one to the argument being passed in
|
24
|
-
yield(n + 1)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
class Doubler
|
29
|
-
def call(*)
|
30
|
-
# modifies the results by doubles each value
|
31
|
-
yield.map {|x| x * 2 }
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
describe MyList do
|
37
|
-
before do
|
38
|
-
MyList.middleware do
|
39
|
-
use OneExtra
|
40
|
-
use Doubler
|
41
|
-
|
42
|
-
# loggers
|
43
|
-
prepend {|x| puts "n starts as #{x}" }
|
44
|
-
append {|x| puts "n ends as #{x}" }
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
after { MyList.middleware.clear }
|
49
|
-
|
50
|
-
it 'calls middleware chain and generates a list' do
|
51
|
-
res = MyList.generate(2)
|
52
|
-
expect(res).to eq [ 2, 4, 6 ]
|
53
|
-
end
|
54
|
-
|
55
|
-
it 'logs to stdout' do
|
56
|
-
expect {
|
57
|
-
MyList.generate(2)
|
58
|
-
}.to output("n starts as 2\nn ends as 3\n").to_stdout
|
59
|
-
end
|
60
|
-
end
|
data/spec/readme/usage_spec.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'meddleware'
|
2
|
-
|
3
|
-
# lib/mywidget.rb
|
4
|
-
class MyWidget
|
5
|
-
def self.middleware(&block)
|
6
|
-
(@middleware ||= Meddleware.new).tap do
|
7
|
-
@middleware.instance_eval(&block) if block_given?
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def do_the_thing
|
12
|
-
# invoke middleware chain
|
13
|
-
MyWidget.middleware.call do
|
14
|
-
# do your thing
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
MyMiddleware = proc {}
|
20
|
-
|
21
|
-
# config/initializers/mywidget.rb
|
22
|
-
MyWidget.middleware do
|
23
|
-
# add a logger
|
24
|
-
use { puts "before the thing" }
|
25
|
-
|
26
|
-
# add another middleware
|
27
|
-
use MyMiddleware
|
28
|
-
end
|
29
|
-
|
30
|
-
|
31
|
-
# use it from whereever
|
32
|
-
# MyWidget.new.do_the_thing
|
33
|
-
|
34
|
-
|
35
|
-
describe MyWidget do
|
36
|
-
it 'adds middleware to the framework' do
|
37
|
-
expect(MyWidget.middleware).to include(MyMiddleware)
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'calls each middleware' do
|
41
|
-
expect(MyMiddleware).to receive(:call)
|
42
|
-
|
43
|
-
expect {
|
44
|
-
MyWidget.new.do_the_thing
|
45
|
-
}.to output("before the thing\n").to_stdout
|
46
|
-
end
|
47
|
-
end
|