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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3954be9354da6c601d61ef054899259b7fc749c9
4
- data.tar.gz: 6840f5fa4aad0fbd2f4a97200133e12a5362bb25
3
+ metadata.gz: 69f274970332a9e8b996853befecaee2a5d386c4
4
+ data.tar.gz: 6f4274479fb7fbe02d085e9e468960bb078f65b7
5
5
  SHA512:
6
- metadata.gz: 997889d2b9aed15eabab7ebc41f85f52ba1be4213d2cdcd44ab562ce5245c75c6a3b0d9e87515b02b4387eea34e2305c8c841ee93324c8b76319953f4ab2f803
7
- data.tar.gz: 0cfe6a6036f066df4794e96944ac42750f4249d1d4a13da5482412fd63cc9bd051de7e7a7f419cc9c67805cec360ee8eb53bfa104f29c18935c2828599e9887f
6
+ metadata.gz: 29a50dfea8185c607d62cc677516eb4109b762dc190cb41c070a0a30a8919691148e93b4b3c68e508b94623754a9b26a127553f7a0cca88547340dc9146f9636
7
+ data.tar.gz: 2296664bee1116d4b0a201a93f0b88d4c37b811da374c540588cfb309a297838bef4a91ca2c7c9a9b9740ba5c29fdf35469bffa530a5e311c2a36759e4eb3e6d
data/README.md CHANGED
@@ -132,6 +132,7 @@ The [middleware](https://rubygems.org/gems/middleware) gem works well, following
132
132
 
133
133
  ## Change Log
134
134
 
135
+ * 0.1.2 - More thread safety in Stack#start
135
136
  * 0.1.1 - Thread safety in Stack#start
136
137
  * 0.1.0 - Initial release
137
138
 
@@ -8,70 +8,81 @@ module Modware
8
8
  when Class then env
9
9
  else KeyStruct[*env]
10
10
  end
11
- @middlewares = []
11
+ @middleware_mods = []
12
12
  end
13
13
 
14
14
  def add(mod)
15
- middleware = Middleware.new(self, mod)
16
- @middlewares.last._next = middleware if @middlewares.any?
17
- @middlewares << middleware
15
+ @middleware_mods << mod
18
16
  end
19
17
 
20
- def start(*args, &implementation)
18
+ def start(*args, &base_implementation)
21
19
  env = @env_klass.new(*args)
22
- execute_stack(env, implementation)
20
+ Executor.new(@middleware_mods).execute(env, base_implementation)
23
21
  env
24
22
  end
25
23
 
26
24
  private
27
25
 
28
- def execute_stack(env, base_implementation)
29
- return call_implementation(env, base_implementation) unless @middlewares.any?
30
-
31
- @middlewares.each do |middleware|
32
- middleware.before env if middleware.respond_to? :before
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
- @middlewares.first._call(env, base_implementation)
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
- @middlewares.each do |middleware|
38
- middleware.after env if middleware.respond_to? :after
46
+ @middlewares.each do |middleware|
47
+ middleware.after env if middleware.respond_to? :after
48
+ end
39
49
  end
40
- end
41
50
 
42
- def call_implementation(env, base_implementation)
43
- if middleware = @middlewares.select(&it.respond_to?(:implement)).last
44
- middleware.implement(env)
45
- elsif base_implementation
46
- base_implementation.call env
47
- else
48
- raise StackError, "No base implementation nor middleware implementation in stack"
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 :_next
64
+ attr_accessor :_modware_next
54
65
 
55
- def initialize(stack, mod)
56
- @stack = stack
66
+ def initialize(executor, mod)
67
+ @executor = executor
57
68
  singleton_class.send :include, mod
58
69
  end
59
70
 
60
- def _call(env, base_implementation)
71
+ def _modware_call(env, base_implementation)
61
72
  if respond_to? :around
62
73
  around(env) { |env|
63
- _continue env, base_implementation
74
+ _modware_continue env, base_implementation
64
75
  }
65
76
  else
66
- _continue env, base_implementation
77
+ _modware_continue env, base_implementation
67
78
  end
68
79
  end
69
80
 
70
- def _continue(env, base_implementation)
71
- if self._next
72
- self._next._call(env, base_implementation)
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
- @stack.send :call_implementation, env, base_implementation
85
+ @executor.call_implementation env, base_implementation
75
86
  end
76
87
  end
77
88
  end
@@ -1,3 +1,3 @@
1
1
  module Modware
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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.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-25 00:00:00.000000000 Z
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.6
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