hobbit-contrib 0.6.0 → 0.7.0

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: 802654dd916d3a7a81d580faf85e0d5c1bf9eeb1
4
- data.tar.gz: 769fec8b8f0a05d54ddb9e94481485642daa3186
3
+ metadata.gz: 185258fc6ea56da609487b2888247abdd6cde2cb
4
+ data.tar.gz: 23dffe0b7043f1ebd7fe4683100cd50a119e30dd
5
5
  SHA512:
6
- metadata.gz: da0ff48b1b5d7f9682e149c6e056426f01c43ee03a2dbbb39417a7278778628e5a82c7b6b98a46050234a25c051fb2fefa2e2031526dbb3865cbfec8985cfdd9
7
- data.tar.gz: f75f472b13d2b8bd52bcb1ccaf1650311a6fea541b97aff553ce001e8668c71e611e42016ecb926abf04ec0c169536a3ead2a4db2c4bd6b58e1d7dccf43e7b4b
6
+ metadata.gz: ebc8f6211e9c33a16d806060ed02cb03d03ea68403087d8db43701abaef761583db7404150bbe4d0251ae8c9a8a2684268672fb1c20ef5d33981502215ab50e7
7
+ data.tar.gz: b75286914679c427040fe57f20b51db83ab76c998764ebfd9641af6e5f04897ca18bd39d6066932e71c46620aaa0d2452c67bdf0046225ec2519f7d2f2fed694
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.7.0
2
+
3
+ * `Hobbit::Filter`: call all matching filters instead of calling the first
4
+ matching filter.
5
+
1
6
  # 0.6.0
2
7
 
3
8
  * Make it work with `hobbit` 0.6.0
@@ -1,5 +1,5 @@
1
1
  module Hobbit
2
2
  module Contrib
3
- VERSION = '0.6.0'
3
+ VERSION = '0.7.0'
4
4
  end
5
5
  end
data/lib/hobbit/filter.rb CHANGED
@@ -54,26 +54,20 @@ module Hobbit
54
54
  private
55
55
 
56
56
  def filter(kind)
57
- filter = find_filter(kind)
58
- if filter
59
- instance_eval(&filter[:block])
60
- response.finish if kind == :after
61
- end
62
- end
63
-
64
- def find_filter(kind)
65
- filter = self.class.filters[kind].detect do |f|
66
- f[:compiled_path] =~ request.path_info || f[:compiled_path] =~ ''
67
- end
68
-
69
- if filter
70
- $~.captures.each_with_index do |value, index|
71
- param = filter[:extra_params][index]
72
- request.params[param] = value
57
+ find_filters(kind).each do |filter|
58
+ if filter[:compiled_path] =~ request.path_info
59
+ $~.captures.each_with_index do |value, index|
60
+ param = filter[:extra_params][index]
61
+ request.params[param] = value
62
+ end
73
63
  end
64
+ instance_eval &filter[:block]
74
65
  end
66
+ response.finish if kind == :after
67
+ end
75
68
 
76
- filter
69
+ def find_filters(kind)
70
+ self.class.filters[kind].select { |f| f[:compiled_path] =~ request.path_info || f[:compiled_path] =~ '' }
77
71
  end
78
72
  end
79
73
  end
data/test/filter_test.rb CHANGED
@@ -21,31 +21,28 @@ scope Hobbit::Filter do
21
21
  end
22
22
 
23
23
  %w(after before).each do |kind|
24
- str = <<EOS
25
- scope '::#{kind}' do
26
- test do
27
- p = Proc.new { 'do something' }
28
- app = mock_app do
29
- include Hobbit::Filter
30
- #{kind}('', &p)
24
+ scope "::#{kind}" do
25
+ test do
26
+ p = Proc.new { 'do something' }
27
+ app = mock_app do
28
+ include Hobbit::Filter
29
+ send kind, &p
30
+ end
31
+
32
+ assert app.to_app.class.filters[:"#{kind}"].size == 1
33
+ assert app.to_app.class.filters[:"#{kind}"].first[:block].call == p.call
31
34
  end
32
-
33
- assert app.to_app.class.filters[:#{kind}].size == 1
34
- assert app.to_app.class.filters[:#{kind}].first[:block].call == p.call
35
35
  end
36
- end
37
36
 
38
- scope 'when a filter matches' do
39
- test "calls the filters' block" do
40
- get '/'
41
- assert last_response.ok?
42
- assert last_request.env.include? 'hobbit.#{kind}'
43
- assert last_request.env['hobbit.#{kind}'] == 'this is #{kind}'
37
+ scope 'when a filter matches' do
38
+ test "calls the filters' block" do
39
+ get '/'
40
+ assert last_response.ok?
41
+ assert last_request.env.include? "hobbit.#{kind}"
42
+ assert last_request.env["hobbit.#{kind}"] == "this is #{kind}"
43
+ end
44
44
  end
45
45
  end
46
- EOS
47
- instance_eval str
48
- end
49
46
 
50
47
  scope '::filters' do
51
48
  test 'returns a Hash' do
@@ -142,32 +139,32 @@ EOS
142
139
  include Hobbit::Filter
143
140
 
144
141
  before do
145
- env['hobbit.before'] = 'this will match'
142
+ env['hobbit.before'] = ['this will match']
146
143
  end
147
144
 
148
145
  before '/' do
149
- env['hobbit.before'] = 'this wont match'
146
+ env['hobbit.before'] << 'this will match too'
150
147
  end
151
148
 
152
149
  after do
153
- env['hobbit.after'] = 'this will match'
150
+ env['hobbit.after'] = ['this will match']
154
151
  end
155
152
 
156
153
  after '/' do
157
- env['hobbit.after'] = 'this wont match'
154
+ env['hobbit.after'] << 'this will match too'
158
155
  end
159
156
 
160
157
  get('/') { 'GET /' }
161
158
  end
162
159
  end
163
160
 
164
- test 'calls the first that matches' do
161
+ test 'calls all matching filters' do
165
162
  get '/'
166
163
  assert last_response.ok?
167
164
  assert last_request.env.include? 'hobbit.before'
168
- assert last_request.env['hobbit.before'] == 'this will match'
165
+ assert_equal ['this will match', 'this will match too'], last_request.env['hobbit.before']
169
166
  assert last_request.env.include? 'hobbit.after'
170
- assert last_request.env['hobbit.after'] == 'this will match'
167
+ assert_equal ['this will match', 'this will match too'], last_request.env['hobbit.after']
171
168
  end
172
169
  end
173
170
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hobbit-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patricio Mac Adden
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-12 00:00:00.000000000 Z
11
+ date: 2014-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler