modware 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/modware/stack.rb +43 -32
- data/lib/modware/version.rb +1 -1
- data/spec/execution_spec.rb +7 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69f274970332a9e8b996853befecaee2a5d386c4
|
4
|
+
data.tar.gz: 6f4274479fb7fbe02d085e9e468960bb078f65b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29a50dfea8185c607d62cc677516eb4109b762dc190cb41c070a0a30a8919691148e93b4b3c68e508b94623754a9b26a127553f7a0cca88547340dc9146f9636
|
7
|
+
data.tar.gz: 2296664bee1116d4b0a201a93f0b88d4c37b811da374c540588cfb309a297838bef4a91ca2c7c9a9b9740ba5c29fdf35469bffa530a5e311c2a36759e4eb3e6d
|
data/README.md
CHANGED
data/lib/modware/stack.rb
CHANGED
@@ -8,70 +8,81 @@ module Modware
|
|
8
8
|
when Class then env
|
9
9
|
else KeyStruct[*env]
|
10
10
|
end
|
11
|
-
@
|
11
|
+
@middleware_mods = []
|
12
12
|
end
|
13
13
|
|
14
14
|
def add(mod)
|
15
|
-
|
16
|
-
@middlewares.last._next = middleware if @middlewares.any?
|
17
|
-
@middlewares << middleware
|
15
|
+
@middleware_mods << mod
|
18
16
|
end
|
19
17
|
|
20
|
-
def start(*args, &
|
18
|
+
def start(*args, &base_implementation)
|
21
19
|
env = @env_klass.new(*args)
|
22
|
-
|
20
|
+
Executor.new(@middleware_mods).execute(env, base_implementation)
|
23
21
|
env
|
24
22
|
end
|
25
23
|
|
26
24
|
private
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
26
|
+
class Executor
|
27
|
+
def initialize(middleware_mods)
|
28
|
+
prev = nil
|
29
|
+
@middlewares = middleware_mods.map { |mod|
|
30
|
+
Middleware.new(self, mod).tap { |middleware|
|
31
|
+
prev._modware_next = middleware if prev
|
32
|
+
prev = middleware
|
33
|
+
}
|
34
|
+
}
|
33
35
|
end
|
34
36
|
|
35
|
-
|
37
|
+
def execute(env, base_implementation)
|
38
|
+
return call_implementation(env, base_implementation) if @middlewares.empty?
|
39
|
+
|
40
|
+
@middlewares.each do |middleware|
|
41
|
+
middleware.before env if middleware.respond_to? :before
|
42
|
+
end
|
43
|
+
|
44
|
+
@middlewares.first._modware_call(env, base_implementation)
|
36
45
|
|
37
|
-
|
38
|
-
|
46
|
+
@middlewares.each do |middleware|
|
47
|
+
middleware.after env if middleware.respond_to? :after
|
48
|
+
end
|
39
49
|
end
|
40
|
-
end
|
41
50
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
51
|
+
def call_implementation(env, base_implementation)
|
52
|
+
if middleware = @middlewares.select(&it.respond_to?(:implement)).last
|
53
|
+
middleware.implement(env)
|
54
|
+
elsif base_implementation
|
55
|
+
base_implementation.call env
|
56
|
+
else
|
57
|
+
raise StackError, "No base implementation nor middleware implementation in stack"
|
58
|
+
end
|
49
59
|
end
|
50
60
|
end
|
51
61
|
|
62
|
+
|
52
63
|
class Middleware
|
53
|
-
attr_accessor :
|
64
|
+
attr_accessor :_modware_next
|
54
65
|
|
55
|
-
def initialize(
|
56
|
-
@
|
66
|
+
def initialize(executor, mod)
|
67
|
+
@executor = executor
|
57
68
|
singleton_class.send :include, mod
|
58
69
|
end
|
59
70
|
|
60
|
-
def
|
71
|
+
def _modware_call(env, base_implementation)
|
61
72
|
if respond_to? :around
|
62
73
|
around(env) { |env|
|
63
|
-
|
74
|
+
_modware_continue env, base_implementation
|
64
75
|
}
|
65
76
|
else
|
66
|
-
|
77
|
+
_modware_continue env, base_implementation
|
67
78
|
end
|
68
79
|
end
|
69
80
|
|
70
|
-
def
|
71
|
-
if self.
|
72
|
-
self.
|
81
|
+
def _modware_continue(env, base_implementation)
|
82
|
+
if self._modware_next
|
83
|
+
self._modware_next._modware_call(env, base_implementation)
|
73
84
|
else
|
74
|
-
@
|
85
|
+
@executor.call_implementation env, base_implementation
|
75
86
|
end
|
76
87
|
end
|
77
88
|
end
|
data/lib/modware/version.rb
CHANGED
data/spec/execution_spec.rb
CHANGED
@@ -28,6 +28,13 @@ describe "Execution" do
|
|
28
28
|
Given { stack.add Factory.middleware(2, implement: nil) }
|
29
29
|
|
30
30
|
Then { expect(env.result).to eq [:before1, :before2, :around_pre2, :implement1, :around_post2, :after1, :after2 ] }
|
31
|
+
|
32
|
+
context "if add middleware3" do
|
33
|
+
|
34
|
+
Given { stack.add Factory.middleware(3, around: nil, implement: nil) }
|
35
|
+
|
36
|
+
Then { expect(env.result).to eq [:before1, :before2, :before3, :around_pre2, :implement1, :around_post2, :after1, :after2, :after3 ] }
|
37
|
+
end
|
31
38
|
end
|
32
39
|
end
|
33
40
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modware
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ronen barzel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: key_struct
|
@@ -179,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
179
179
|
version: '0'
|
180
180
|
requirements: []
|
181
181
|
rubyforge_project:
|
182
|
-
rubygems_version: 2.4.
|
182
|
+
rubygems_version: 2.4.5
|
183
183
|
signing_key:
|
184
184
|
specification_version: 4
|
185
185
|
summary: A middleware library, featuring a simple interface and "callback" style semantics
|