action_subscriber 5.1.4 → 5.1.5

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
  SHA256:
3
- metadata.gz: e8c37c6735aa71bb3a8f1085d401c2634869484a6269a5a40daaca3a40bf92a2
4
- data.tar.gz: 34e673c42225b7c1771d7ca42eaf629e8b693e300c38c62ff43c21697171648a
3
+ metadata.gz: a596e1dcbba93eacacb9d650f0853ffdb1837a3dc36daf5255409d85a9dd52a4
4
+ data.tar.gz: f85747ecbb7868d6d0ec0ff01afe9dda45f4fb8ea0a1d8a5f7781cd482e04714
5
5
  SHA512:
6
- metadata.gz: 4b4da055a37f1332a3eaaa509940907193cc3e6dbdd58d5cf321ff7e7328df3aacc50f2152f894b74f85291dc59d487c4bb04c1e397fc6652970be75e6b429ca
7
- data.tar.gz: b171388946ad395f6c65128c06911440accd09c941dd048183b89c61c9868ab05ef23d794433709575219c98e81de34389ee0722a078ec4f00494a8ab2bcab21
6
+ metadata.gz: 86a8e02b695d8b9a9e8a0cf561b66f2ddcd1a4e8d8471f83143f833db1109b845f0fe42baa20b620628109721d9f1ad059e9ea3e1ac4abfe34548010fd7e3b7c
7
+ data.tar.gz: 334ca4005d7f67ddf6579b97488fd7220f8b695f7668098f303ae62ea0a68b188f41da60ea093c1c97a5da39cf7962ab74165a61058ff3e3df308151976e742a
@@ -1,4 +1,8 @@
1
1
  language: ruby
2
+ os:
3
+ linux
4
+ dist:
5
+ trusty
2
6
  rvm:
3
7
  - 2.2.6
4
8
  - 2.3.3
@@ -1,5 +1,38 @@
1
1
  module ActionSubscriber
2
2
  module DSL
3
+ class Filter
4
+ attr_accessor :callback_method
5
+ attr_accessor :included_actions
6
+ attr_accessor :excluded_actions
7
+
8
+ def initialize(callback_method, options)
9
+ @callback_method = callback_method
10
+ @included_actions = @excluded_actions = []
11
+ parse_options(options)
12
+ end
13
+
14
+ def matches(action)
15
+ unless included_actions.empty?
16
+ return included_actions.include?(action)
17
+ end
18
+
19
+ unless excluded_actions.empty?
20
+ return false if excluded_actions.include?(action)
21
+ end
22
+
23
+ true
24
+ end
25
+
26
+ private
27
+
28
+ def parse_options(options)
29
+ return unless options
30
+
31
+ @included_actions = options.fetch(:if, [])
32
+ @excluded_actions = options.fetch(:unless, [])
33
+ end
34
+ end
35
+
3
36
  def at_least_once!
4
37
  @_acknowledge_messages = true
5
38
  @_at_least_once = true
@@ -22,11 +55,16 @@ module ActionSubscriber
22
55
  !!@_acknowledge_messages
23
56
  end
24
57
 
25
- def around_filter(filter_method)
26
- around_filters << filter_method unless around_filters.include?(filter_method)
58
+ def around_filter(callback_method, options = nil)
59
+ filter = Filter.new(callback_method, options)
60
+ conditionally_add_filter!(filter)
27
61
  around_filters
28
62
  end
29
63
 
64
+ def conditionally_add_filter!(filter)
65
+ around_filters << filter unless around_filters.any? { |f| f.callback_method == filter.callback_method }
66
+ end
67
+
30
68
  def around_filters
31
69
  @_around_filters ||= []
32
70
  end
@@ -95,7 +133,11 @@ module ActionSubscriber
95
133
  final_block = Proc.new { subscriber_instance.public_send(action) }
96
134
 
97
135
  first_proc = around_filters.reverse.reduce(final_block) do |block, filter|
98
- Proc.new { subscriber_instance.send(filter, &block) }
136
+ if filter.matches(action)
137
+ Proc.new { subscriber_instance.send(filter.callback_method, &block) }
138
+ else
139
+ block
140
+ end
99
141
  end
100
142
  first_proc.call
101
143
  end
@@ -1,3 +1,3 @@
1
1
  module ActionSubscriber
2
- VERSION = "5.1.4"
2
+ VERSION = "5.1.5"
3
3
  end
@@ -33,7 +33,7 @@ describe "subscriber filters", :integration => true do
33
33
  let(:subscriber) { InstaSubscriber }
34
34
 
35
35
  it "does not allow an around filter to be pushed on twice" do
36
- expect(InstaSubscriber.around_filters).to eq([:whisper, :yell])
36
+ expect(InstaSubscriber.around_filters.map(&:callback_method)).to eq([:whisper, :yell])
37
37
  end
38
38
 
39
39
  it "runs multiple around filters" do
@@ -46,3 +46,89 @@ describe "subscriber filters", :integration => true do
46
46
  end
47
47
  end
48
48
  end
49
+
50
+ class OptionsSubscriber < ActionSubscriber::Base
51
+ around_filter :whisper, :if => [:primero, :segundo]
52
+ around_filter :yell, :if => [:primero]
53
+ around_filter :gossip, :unless => [:private_action]
54
+ around_filter :everybody
55
+
56
+ def primero
57
+ $messages << payload
58
+ end
59
+
60
+ def private_action
61
+ $messages << payload
62
+ end
63
+
64
+ def segundo
65
+ $messages << payload
66
+ end
67
+
68
+ private
69
+
70
+ def everybody
71
+ $messages << :everybody_before
72
+ yield
73
+ $messages << :everybody_after
74
+ end
75
+
76
+ def gossip
77
+ $messages << :gossip_before
78
+ yield
79
+ $messages << :gossip_after
80
+ end
81
+
82
+ def whisper
83
+ $messages << :whisper_before
84
+ yield
85
+ $messages << :whisper_after
86
+ end
87
+
88
+ def yell
89
+ $messages << :yell_before
90
+ yield
91
+ $messages << :yell_after
92
+ end
93
+ end
94
+
95
+ describe "subscriber filters with conditions", :integration => true do
96
+ let(:draw_routes) do
97
+ ::ActionSubscriber.draw_routes do
98
+ default_routes_for OptionsSubscriber
99
+ end
100
+ end
101
+ let(:subscriber) { OptionsSubscriber }
102
+
103
+ context "honors conditions" do
104
+ it "runs yell" do
105
+ $messages = []
106
+ ::ActionSubscriber.start_subscribers!
107
+ ::ActivePublisher.publish("options.primero", "Howdy!", "events")
108
+
109
+ verify_expectation_within(1.0) do
110
+ expect($messages).to eq [:whisper_before, :yell_before, :gossip_before, :everybody_before, "Howdy!", :everybody_after, :gossip_after, :yell_after, :whisper_after]
111
+ end
112
+ end
113
+
114
+ it "doesn't yell" do
115
+ $messages = []
116
+ ::ActionSubscriber.start_subscribers!
117
+ ::ActivePublisher.publish("options.segundo", "Howdy!", "events")
118
+
119
+ verify_expectation_within(1.0) do
120
+ expect($messages).to eq [:whisper_before, :gossip_before, :everybody_before, "Howdy!", :everybody_after, :gossip_after, :whisper_after]
121
+ end
122
+ end
123
+
124
+ it "doesn't gossip" do
125
+ $messages = []
126
+ ::ActionSubscriber.start_subscribers!
127
+ ::ActivePublisher.publish("options.private_action", "Howdy!", "events")
128
+
129
+ verify_expectation_within(1.0) do
130
+ expect($messages).to eq [:everybody_before, "Howdy!", :everybody_after]
131
+ end
132
+ end
133
+ end
134
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_subscriber
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.1.4
4
+ version: 5.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Stien
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2019-06-07 00:00:00.000000000 Z
15
+ date: 2020-01-10 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activesupport
@@ -296,8 +296,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
296
296
  - !ruby/object:Gem::Version
297
297
  version: '0'
298
298
  requirements: []
299
- rubyforge_project:
300
- rubygems_version: 2.7.6
299
+ rubygems_version: 3.0.3
301
300
  signing_key:
302
301
  specification_version: 4
303
302
  summary: ActionSubscriber is a DSL that allows a rails app to consume messages from