Class: CommandBot::CommandIdentifier
- Inherits:
-
Object
- Object
- CommandBot::CommandIdentifier
- Defined in:
- lib/command_bot/command_identifier.rb
Overview
Stores command call information.
Class Method Summary collapse
-
._get_options(words, prefix, separator) ⇒ Object
Utility method to get options hash from words array (it will be modified).
- .default(prefix: '', disallow_whitespace: false, o_prefix: '--', o_separator: '=') ⇒ CommandIdentifier
-
.primitive ⇒ CommandIdentifier
Primitive optionless identifier.
Instance Method Summary collapse
-
#call(bot, text, data) ⇒ CommandCall?
Call identifier process.
-
#initialize {|bot, text, data| ... } ⇒ CommandIdentifier
constructor
Create new identifier.
Constructor Details
#initialize {|bot, text, data| ... } ⇒ CommandIdentifier
Create new identifier.
11 12 13 |
# File 'lib/command_bot/command_identifier.rb', line 11 def initialize(&identifier) @identifier = identifier end |
Class Method Details
._get_options(words, prefix, separator) ⇒ Object
Utility method to get options hash from words array (it will be modified).
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/command_bot/command_identifier.rb', line 70 def self.(words, prefix, separator) = {} while words.first&.start_with?(prefix) str = words.shift str.downcase! str.delete_prefix! prefix # Need to check for splitter in key key, *, value = str.partition(separator) # Following line allows writing '!command --key = val' with words=['--key', '=', 'val'] *, value = words.shift(2) if value.empty? && words.first == separator [key] = value || '' end end |
.default(prefix: '', disallow_whitespace: false, o_prefix: '--', o_separator: '=') ⇒ CommandIdentifier
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/command_bot/command_identifier.rb', line 29 def self.default(prefix: '', disallow_whitespace: false, o_prefix: '--', o_separator: '=') # rubocop:disable Metrics/AbcSize, Metrics/MethodLength new do |bot, text_original, data| command_call = CommandCall.new(bot, text_original, data) text = text_original.dup # Not a command if got prefix and text do not start with it next nil unless prefix.empty? || text.delete_prefix!(prefix) # Check for whitespace next nil if disallow_whitespace && text.lstrip! words = text.split next nil if words.empty? # Just a prefix command_name = words.shift command_name&.downcase! # Usually commands should not depend on case, so downcasing = (words, o_prefix, o_separator) command_call.command = bot.find_command(command_name) command_call.arguments = words # Everything left is considered to be arguments command_call. = command_call end end |
.primitive ⇒ CommandIdentifier
Returns primitive optionless identifier.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/command_bot/command_identifier.rb', line 55 def self.primitive new do |bot, text, data| command_call = CommandCall.new(bot, text, data) words = text.split command_call.command = bot.find_command(words.shift || '') command_call.arguments = words command_call. = {} command_call end end |
Instance Method Details
#call(bot, text, data) ⇒ CommandCall?
Call identifier process.
19 20 21 |
# File 'lib/command_bot/command_identifier.rb', line 19 def call(bot, text, data) @identifier.call(bot, text, data) end |