Skip to content

Commit

Permalink
Deprecate hooks method on HookSupport module
Browse files Browse the repository at this point in the history
  • Loading branch information
Laertis Pappas committed Jun 23, 2017
1 parent 90ee566 commit 5eca0e3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
14 changes: 11 additions & 3 deletions lib/slack-ruby-bot/hooks/hook_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ def on(event_name, &block)

# Instance stuff
def hooks
@hooks ||= SlackRubyBot::Hooks::Set.new
warn Kernel.caller.first + ' [DEPRECATION] `hooks` method is deprecated. Please use `server.on` instead to register a hook.'
_hooks
end

def on(event_name, handler)
hooks.add(event_name, handler)
_hooks.add(event_name, handler)
end

def flush_hook_blocks
Expand All @@ -31,11 +32,18 @@ def flush_hook_blocks
add_hook_handlers(self.class.hook_blocks)
end

# TODO: This should be deprecated in favor of `on`
def add_hook_handlers(handler_hash)
handler_hash.each do |hook, handlers|
Array(handlers).each { |handler| hooks.add(hook, handler) }
Array(handlers).each { |handler| on(hook, handler) }
end
end

# Temp use this method in order to deprecate `hooks` and revisit
def _hooks
@hooks ||= SlackRubyBot::Hooks::Set.new
end
private :_hooks
end
end
end
2 changes: 1 addition & 1 deletion lib/slack-ruby-bot/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def client
@client = nil
restart! unless @stopping
end
hooks.client = client
_hooks.client = client

client
end
Expand Down
18 changes: 15 additions & 3 deletions spec/slack-ruby-bot/hooks/hook_support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
it 'registers class hook blocks as hook handlers in set' do
object = subject.new

expect(object.hooks).to receive(:add).exactly(3).times.and_call_original
expect(object.send(:_hooks)).to receive(:add).exactly(3).times.and_call_original

expect do
object.flush_hook_blocks
end.to change { object.hooks.handlers.size }.by(2)
end.to change { object.send(:_hooks).handlers.size }.by(2)
end
end

Expand All @@ -49,8 +49,20 @@
event_name = :message_received
handler = ->(_, _) {}

expect(subject.hooks).to receive(:add).with(event_name, handler).and_call_original
expect(subject.send(:_hooks)).to receive(:add).with(event_name, handler).and_call_original
subject.on(event_name, handler)
end
end

describe '#hooks' do
subject { super().new }
it { expect(subject.hooks).to eq subject.send(:_hooks) }
end

describe '#_hooks' do
it 'returns a SlackRubyBot::Hooks::Set instance' do
hooks_set = subject.new.send(:_hooks)
expect(hooks_set).to be_a SlackRubyBot::Hooks::Set
end
end
end

0 comments on commit 5eca0e3

Please sign in to comment.