diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 99ce7ed..ca745f8 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,5 +1,6 @@ -# 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 @@ -7,48 +8,48 @@ # 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 + Exclude: + - 'lib/slack-ruby-bot/config.rb' diff --git a/CHANGELOG.md b/CHANGELOG.md index 2120c2c..2a8e506 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ * [#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. ### 0.9.0 (8/29/2016) diff --git a/lib/slack-ruby-bot/commands/base.rb b/lib/slack-ruby-bot/commands/base.rb index 1f0288d..af4560c 100644 --- a/lib/slack-ruby-bot/commands/base.rb +++ b/lib/slack-ruby-bot/commands/base.rb @@ -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 diff --git a/lib/slack-ruby-bot/commands/help/attrs.rb b/lib/slack-ruby-bot/commands/help/attrs.rb index d279a73..477b7ec 100644 --- a/lib/slack-ruby-bot/commands/help/attrs.rb +++ b/lib/slack-ruby-bot/commands/help/attrs.rb @@ -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 @@ -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 diff --git a/lib/slack-ruby-bot/support/commands_helper.rb b/lib/slack-ruby-bot/support/commands_helper.rb index 6a99804..1db3b7c 100644 --- a/lib/slack-ruby-bot/support/commands_helper.rb +++ b/lib/slack-ruby-bot/support/commands_helper.rb @@ -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 @@ -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.superclass == 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.superclass == SlackRubyBot::Commands::Base } + commands_help_attrs.select { |k| k.klass.ancestors.include?(SlackRubyBot::Commands::Base) && !k.klass.ancestors.include?(SlackRubyBot::Bot) } end end end diff --git a/spec/slack-ruby-bot/support/commands_helper_spec.rb b/spec/slack-ruby-bot/support/commands_helper_spec.rb index 6120427..10ddf2f 100644 --- a/spec/slack-ruby-bot/support/commands_helper_spec.rb +++ b/spec/slack-ruby-bot/support/commands_helper_spec.rb @@ -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 @@ -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.') @@ -81,15 +82,43 @@ it 'do not show description for command without description' do expect(bot_desc).to include("*command_without_description*\n") end + + 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 + + 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 - let(:commands_desc) { commands_helper.other_commands_descs } - let(:hello_command_desc) { commands_desc.find { |desc| desc =~ /\*hello\*/ } } + 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(: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(other_commands_descs).to include('*Creating your own base class works too!*') + end end describe '#command_full_desc' do