Skip to content

Commit

Permalink
Fix: support anonymous command classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Sep 24, 2016
1 parent 2fb9790 commit b1f7d2f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 59 deletions.
49 changes: 23 additions & 26 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,58 +1,55 @@
# This configuration was generated by `rubocop --auto-gen-config`
# on 2016-02-09 15:33:27 -0500 using RuboCop version 0.32.1.
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-09-24 09:10:21 -0400 using RuboCop version 0.38.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
Lint/HandleExceptions:
Enabled: false
Exclude:
- 'lib/initializers/giphy.rb'

# Offense count: 6
# Offense count: 5
Metrics/AbcSize:
Max: 42

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 102
Max: 36

# Offense count: 2
Metrics/CyclomaticComplexity:
Max: 12
Max: 11

# Offense count: 130
# Configuration parameters: AllowURI, URISchemes.
# Offense count: 156
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
# URISchemes: http, https
Metrics/LineLength:
Max: 158
Max: 147

# Offense count: 7
# Offense count: 6
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 27

# Offense count: 2
Metrics/PerceivedComplexity:
Max: 12
Max: 11

# Offense count: 20
# Offense count: 26
Style/Documentation:
Enabled: false

# Offense count: 3
# Offense count: 1
Style/DoubleNegation:
Enabled: false
Exclude:
- 'lib/slack-ruby-bot/commands/base.rb'

# Offense count: 1
# Configuration parameters: Exclude.
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts.
Style/FileName:
Enabled: false
Exclude:
- 'lib/slack-ruby-bot.rb'

# Offense count: 1
Style/ModuleFunction:
Enabled: false

# Offense count: 1
Style/StringLiterals:
Enabled: false
Exclude:
- 'lib/slack-ruby-bot/config.rb'
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

* [#95](https://github.com/slack-ruby/slack-ruby-bot/pull/95): Log team name and ID on successful connection - [@dblock](https://github.com/dblock).
* [#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).
* Your contribution here.
* [#86](https://github.com/dblock/slack-ruby-bot/pull/86): Fix: allow help statements from classes that do not directly inherit from Base classes to appear in `bot help` - [@maclover7](https://github.com/maclover7).

### 0.9.0 (8/29/2016)

Expand Down
2 changes: 1 addition & 1 deletion lib/slack-ruby-bot/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def send_gif(client, channel, keywords, options = {})
end

def help(&block)
CommandsHelper.instance.capture_help(name, &block)
CommandsHelper.instance.capture_help(self, &block)
end

def default_command_name
Expand Down
8 changes: 4 additions & 4 deletions lib/slack-ruby-bot/commands/help/attrs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module Commands
module Help
class Attrs
attr_accessor :command_name, :command_desc, :command_long_desc
attr_reader :class_name, :commands
attr_reader :klass, :commands

def initialize(class_name)
@class_name = class_name
def initialize(klass)
@klass = klass
@commands = []
end

Expand All @@ -23,7 +23,7 @@ def long_desc(long_desc)
end

def command(title, &block)
@commands << self.class.new(class_name).tap do |k|
@commands << self.class.new(klass).tap do |k|
k.title(title)
k.instance_eval(&block)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/slack-ruby-bot/support/commands_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def initialize
@commands_help_attrs = []
end

def capture_help(class_name, &block)
k = Commands::Help::Attrs.new(class_name)
def capture_help(klass, &block)
k = Commands::Help::Attrs.new(klass)
k.instance_eval(&block)
@commands_help_attrs << k
end
Expand Down Expand Up @@ -66,11 +66,11 @@ def help_attrs_with_present_names(help_attrs)
end

def bot_help_attrs
commands_help_attrs.select { |k| k.class_name.constantize.ancestors.include?(SlackRubyBot::Bot) }
commands_help_attrs.select { |k| k.klass.ancestors.include?(SlackRubyBot::Bot) }
end

def other_commands_help_attrs
commands_help_attrs.select { |k| k.class_name.constantize.ancestors.include?(SlackRubyBot::Commands::Base) }
commands_help_attrs.select { |k| k.klass.ancestors.include?(SlackRubyBot::Commands::Base) && !k.klass.ancestors.include?(SlackRubyBot::Bot) }
end
end
end
49 changes: 26 additions & 23 deletions spec/slack-ruby-bot/support/commands_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'spec_helper'

describe SlackRubyBot::CommandsHelper do
let(:bot_class_name) { 'Testing::WeatherBot' }
let(:command_class_name) { 'Testing::HelloCommand' }
let(:bot_class) { Testing::WeatherBot }
let(:command_class) { Testing::HelloCommand }
let(:commands_helper) { described_class.instance }

describe '#capture_help' do
let(:help_attrs) { commands_helper.commands_help_attrs }
let(:bot_help_attrs) { help_attrs.find { |k| k.class_name == bot_class_name } }
let(:command_help_attrs) { help_attrs.find { |k| k.class_name == command_class_name } }
let(:bot_help_attrs) { help_attrs.find { |k| k.klass == bot_class } }
let(:command_help_attrs) { help_attrs.find { |k| k.klass == command_class } }

it 'should save bot class name' do
expect(bot_help_attrs).to be
Expand Down Expand Up @@ -62,7 +62,8 @@
end

describe '#bot_desc_and_commands' do
let(:bot_desc) { commands_helper.bot_desc_and_commands.first }
let(:bot_desc_and_commands) { commands_helper.bot_desc_and_commands }
let(:bot_desc) { bot_desc_and_commands.first }

it 'returns bot name and description' do
expect(bot_desc).to include('*Weather Bot* - This bot tells you the weather.')
Expand All @@ -82,39 +83,41 @@
expect(bot_desc).to include("*command_without_description*\n")
end

class AnotherBotBase < SlackRubyBot::Bot
end

class AnotherBot < AnotherBotBase
help do
title 'Creating your own base class works too!'
context 'subclasses' do
let(:another_bot_base) { Class.new(SlackRubyBot::Bot) }
let!(:another_bot) do
Class.new(another_bot_base) do
help do
title 'Creating your own base class works too!'
end
end
end
end

it 'includes commands who do not directly inherit from the provided base class' do
expect(commands_helper.bot_desc_and_commands).to include("*Creating your own base class works too!*\n\n*Commands:*\n")
it 'expose help commands' do
expect(bot_desc_and_commands).to include("*Creating your own base class works too!*\n\n*Commands:*\n")
end
end
end

describe '#other_commands_descs' do
class AnotherCommandBase < SlackRubyBot::Commands::Base
end

class AnotherCommand < AnotherCommandBase
help do
title 'Creating your own base class works too!'
let(:another_command_base) { Class.new(SlackRubyBot::Commands::Base) }
let!(:another_command) do
Class.new(another_command_base) do
help do
title 'Creating your own base class works too!'
end
end
end

let(:commands_desc) { commands_helper.other_commands_descs }
let(:hello_command_desc) { commands_desc.find { |desc| desc =~ /\*hello\*/ } }
let(:other_commands_descs) { commands_helper.other_commands_descs }
let(:hello_command_desc) { other_commands_descs.find { |desc| desc =~ /\*hello\*/ } }

it 'returns command name and description' do
expect(hello_command_desc).to eq('*hello* - Says hello.')
end

it 'includes commands who do not directly inherit from provided base class' do
expect(commands_desc).to include('*Creating your own base class works too!*')
expect(other_commands_descs).to include('*Creating your own base class works too!*')
end
end

Expand Down

0 comments on commit b1f7d2f

Please sign in to comment.