Skip to content

Commit afff784

Browse files
authored
Merge pull request #112 from dblock/multiple-events
Added support for multiple .on events as an argument.
2 parents e3c5451 + 4f4326e commit afff784

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Your contribution here.
66
* [#111](https://github.com/slack-ruby/slack-ruby-bot-server/pull/111): Removed dependency on Virtus - [@dblock](https://github.com/dblock).
77
* [#110](https://github.com/slack-ruby/slack-ruby-bot-server/pull/110): Fix ActiveRecord sample app - [@CeeBeeUK](https://github.com/CeeBeeUK).
8+
* [#112](https://github.com/slack-ruby/slack-ruby-bot-server/pull/112): Added support for multiple `.on` events as an argument - [@dblock](https://github.com/dblock).
89

910
#### 0.11.1 (2019/5/17)
1011

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ You can introduce custom behavior into the service lifecycle via callbacks. This
138138
```ruby
139139
instance = SlackRubyBotServer::Service.instance
140140

141+
instance.on :started, :stopped do |team|
142+
# team has been started or stopped
143+
end
144+
141145
instance.on :created do |team, error, options|
142146
# a new team has been registered
143147
end

lib/slack-ruby-bot-server/service.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ def initialize
1717
@callbacks = Hash.new { |h, k| h[k] = [] }
1818
end
1919

20-
def on(type, &block)
21-
@callbacks[type.to_s] << block
20+
def on(*types, &block)
21+
Array(types).each do |type|
22+
@callbacks[type.to_s] << block
23+
end
2224
end
2325

2426
def create!(team, options = {})

spec/slack-ruby-bot-server/service_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,42 @@ def initialize(options = {})
8787
expect(@events).to eq %w[starting started stopping stopped]
8888
end
8989
end
90+
context 'multiple callbacks' do
91+
before do
92+
@events = []
93+
SlackRubyBotServer::Service.instance.tap do |instance|
94+
instance.on :starting, :stopping do |team|
95+
expect(team).to_not be_nil
96+
@events << 'call'
97+
end
98+
end
99+
end
100+
it 'invokes starting and stopping callbacks' do
101+
allow_any_instance_of(SlackRubyBotServer::Server).to receive(:start_async)
102+
SlackRubyBotServer::Service.instance.start!(team)
103+
allow_any_instance_of(SlackRubyBotServer::Server).to receive(:stop!)
104+
SlackRubyBotServer::Service.instance.stop!(team)
105+
expect(@events).to eq %w[call call]
106+
end
107+
end
108+
context 'multiple callback blocks' do
109+
before do
110+
@events = []
111+
SlackRubyBotServer::Service.instance.tap do |instance|
112+
instance.on :starting, :starting do |team|
113+
expect(team).to_not be_nil
114+
@events << 'starting'
115+
end
116+
end
117+
end
118+
it 'invokes starting and stopping callbacks' do
119+
allow_any_instance_of(SlackRubyBotServer::Server).to receive(:start_async)
120+
SlackRubyBotServer::Service.instance.start!(team)
121+
allow_any_instance_of(SlackRubyBotServer::Server).to receive(:stop!)
122+
SlackRubyBotServer::Service.instance.stop!(team)
123+
expect(@events).to eq %w[starting starting]
124+
end
125+
end
90126
context 'overriding service_class' do
91127
let(:service_class) do
92128
Class.new(SlackRubyBotServer::Service) do

0 commit comments

Comments
 (0)