Skip to content

Commit

Permalink
Merge pull request #101 from gconklin/ordered-commands
Browse files Browse the repository at this point in the history
Ordered commands
  • Loading branch information
dblock committed Dec 9, 2016
2 parents 3c73542 + b1c7327 commit aefdc02
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [#94](https://github.com/slack-ruby/slack-ruby-bot/pull/94): Use the slack-ruby-danger gem - [@dblock](https://github.com/dblock).
* [#86](https://github.com/dblock/slack-ruby-bot/pull/86): Fix: help statements from classes that do not directly inherit from `Base` appear in `bot help` - [@maclover7](https://github.com/maclover7).
* [#96](https://github.com/slack-ruby/slack-ruby-bot/pull/96): Support help statements in anonymous command and bot classes - [@dblock](https://github.com/dblock).
* [#75](https://github.com/slack-ruby/slack-ruby-bot/pull/101): Fix: Guarantee order of commands based on load order - [@gconklin](https://github.com/gconklin).
* Your contribution here.

### 0.9.0 (8/29/2016)
Expand Down
7 changes: 7 additions & 0 deletions lib/slack-ruby-bot/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ class Base
class_attribute :routes

class << self
attr_reader :command_classes

def inherited(subclass)
@command_classes ||= []
@command_classes << subclass
end

def send_message(client, channel, text, options = {})
logger.warn '[DEPRECATION] `send_message` is deprecated. Please use `client.say` instead.'
if text && !text.length.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/slack-ruby-bot/hooks/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def message_to_self?(client, data)
# @return [Array] Descendants of SlackRubyBot::Commands::Base.
#
def command_classes
SlackRubyBot::Commands::Base.descendants
SlackRubyBot::Commands::Base.command_classes
end

#
Expand Down
39 changes: 39 additions & 0 deletions spec/slack-ruby-bot/commands/commands_command_classes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'spec_helper'

describe SlackRubyBot::Commands::Base do
let! :command1 do
Class.new(SlackRubyBot::Commands::Base) do
command 'command'

def self.call(client, data, match)
client.say(channel: data.channel, text: "#{match[:command]}: #{match[:expression]}")
end
end
end

let! :command2 do
Class.new(SlackRubyBot::Commands::Base) do
command 'command'

def self.call(client, data, match)
client.say(channel: data.channel, text: "#{match[:command]}: #{match[:expression]}")
end
end
end

def app
SlackRubyBot::App.new
end

describe '#command_classes' do
it 'returns all registered command classes' do
expect(SlackRubyBot::Commands::Base.command_classes).to include command1
expect(SlackRubyBot::Commands::Base.command_classes).to include command2
end

it 'returns command1 before command2' do
command_classes = SlackRubyBot::Commands::Base.command_classes
expect(command_classes.find_index(command1)).to be < command_classes.find_index(command2)
end
end
end

0 comments on commit aefdc02

Please sign in to comment.