-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use help scope for providing help attrs
- Loading branch information
Showing
10 changed files
with
118 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,102 @@ | ||
require 'singleton' | ||
|
||
module SlackRubyBot | ||
class CommandHelpAttrs | ||
attr_accessor :command_name, :command_desc, :command_long_desc | ||
attr_reader :class_name, :commands | ||
|
||
def initialize(class_name) | ||
@class_name = class_name | ||
@commands = [] | ||
end | ||
|
||
def title(title) | ||
self.command_name = title | ||
end | ||
|
||
def desc(desc) | ||
self.command_desc = desc | ||
end | ||
|
||
def long_desc(long_desc) | ||
self.command_long_desc = long_desc | ||
end | ||
|
||
def command(title, &block) | ||
@commands << self.class.new(class_name).tap { |k| k.title(title); k.instance_eval &block } | ||
end | ||
end | ||
|
||
class CommandsHelper | ||
BUILTIN_COMMAND_CLASSES = [SlackRubyBot::Commands::Help, SlackRubyBot::Commands::Hi].freeze | ||
HELP_ATTR_METHODS_MAP = { | ||
command_name: :title, | ||
command_desc: :desc, | ||
command_long_desc: :long_desc | ||
}.freeze | ||
|
||
class << self | ||
def validate_attrs | ||
all_command_classes.each do |k| | ||
HELP_ATTR_METHODS_MAP.each do |attr, setter_method| | ||
raise "#{k}: #{setter_method} is not present" if k.public_send(attr).nil? | ||
end | ||
end | ||
end | ||
include Singleton | ||
attr_reader :commands_help_attrs | ||
|
||
def all_bots_descs | ||
commands_info(bot_classes).map do |command_info| | ||
"#{command_name_and_desc(command_info)}\n#{command_info[:long_desc]}" | ||
end | ||
end | ||
def initialize | ||
@commands_help_attrs = [] | ||
end | ||
|
||
def all_commands_descs | ||
commands_info(command_classes_only).map do |command_info| | ||
command_name_and_desc(command_info) | ||
end | ||
end | ||
def capture_help(class_name, &block) | ||
k = CommandHelpAttrs.new(class_name) | ||
k.instance_eval(&block) | ||
@commands_help_attrs << k | ||
end | ||
|
||
def command_full_desc(name) | ||
info = commands_info(command_classes_only).find { |command_info| command_info[:title] == name } | ||
return "There's no command #{name}" unless info | ||
return "There's no description for command #{name}" if info[:long_desc].blank? | ||
"#{command_name_and_desc(info)}\n\n#{info[:long_desc]}" | ||
def bot_desc_and_commands | ||
collect_help_attrs(bot_help_attrs) do |help_attrs| | ||
bot_commands_descs = collect_name_and_desc(help_attrs.commands) | ||
"#{command_name_and_desc(help_attrs)}\n\n*Commands:*\n#{bot_commands_descs.join("\n")}" | ||
end | ||
end | ||
|
||
private | ||
def other_commands_descs | ||
collect_name_and_desc(other_commands_help_attrs) | ||
end | ||
|
||
def command_name_and_desc(command_info) | ||
desc = command_info[:desc].present? ? "- #{command_info[:desc]}" : '' | ||
"*#{command_info[:title]}* #{desc}" | ||
end | ||
def command_full_desc(name) | ||
unescaped_name = CGI.unescapeHTML(name) | ||
help_attrs = find_command_help_attrs(unescaped_name) | ||
return "There's no command *#{unescaped_name}*" unless help_attrs | ||
return "There's no description for command *#{unescaped_name}*" if help_attrs.command_long_desc.blank? | ||
"#{command_name_and_desc(help_attrs)}\n\n#{help_attrs.command_long_desc}" | ||
end | ||
|
||
def commands_info(command_classes) | ||
commands_with_present_names = command_classes.select { |k| k.command_name.present? } | ||
commands_with_present_names.inject([]) do |data, klass| | ||
info = {} | ||
HELP_ATTR_METHODS_MAP.each do |attr, setter_method| | ||
info[setter_method] = klass.public_send(attr) | ||
end | ||
data << info | ||
end | ||
end | ||
private | ||
|
||
def bot_classes | ||
all_command_classes.select { |k| k.superclass == SlackRubyBot::Bot } | ||
end | ||
def find_command_help_attrs(name) | ||
help_attrs = commands_help_attrs.find { |k| k.command_name == name } | ||
return help_attrs if help_attrs | ||
commands_help_attrs.each { |k| k.commands.each { |c| return c if c.command_name == name } } | ||
return nil | ||
end | ||
|
||
def command_classes_only | ||
all_command_classes.reject { |k| k.superclass == SlackRubyBot::Bot } | ||
def collect_help_attrs(help_attrs) | ||
help_attrs_with_present_names(help_attrs).map do |help_attrs| | ||
yield(help_attrs) | ||
end | ||
end | ||
|
||
def all_command_classes | ||
BUILTIN_COMMAND_CLASSES + external_command_classes | ||
def collect_name_and_desc(help_attrs) | ||
collect_help_attrs(help_attrs) do |help_attrs| | ||
command_name_and_desc(help_attrs) | ||
end | ||
end | ||
|
||
def command_classes | ||
SlackRubyBot::Commands::Base.descendants | ||
end | ||
def command_name_and_desc(help_attrs) | ||
desc = help_attrs.command_desc.present? ? "- #{help_attrs.command_desc}" : '' | ||
"*#{help_attrs.command_name}* #{desc}" | ||
end | ||
|
||
def external_command_classes | ||
command_classes.reject do |k| | ||
k.name && k.name.start_with?('SlackRubyBot::Commands') || k == SlackRubyBot::Bot | ||
end | ||
end | ||
def help_attrs_with_present_names(help_attrs) | ||
help_attrs.select { |k| k.command_name.present? } | ||
end | ||
|
||
def bot_help_attrs | ||
commands_help_attrs.select { |k| k.class_name.constantize.superclass == SlackRubyBot::Bot } | ||
end | ||
|
||
def other_commands_help_attrs | ||
commands_help_attrs.select { |k| k.class_name.constantize.superclass == SlackRubyBot::Commands::Base } | ||
end | ||
|
||
end | ||
end |