Created
April 11, 2012 19:33
-
-
Save postmodern/2361750 to your computer and use it in GitHub Desktop.
Attempt at defining commands within Cinch Plugins.
This file contains hidden or 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
ARGUMENT_FORMATS = { | |
string: /\S+/, | |
integer: /\d+/, | |
float: /\d*\.\d+/, | |
text: /.+/ | |
} | |
# | |
# All defined commands. | |
# | |
# @return [Hash{String => Hash}] | |
# The command names and additional information. | |
# | |
def self.commands | |
@@commands ||= {} | |
end | |
# | |
# Defines a command in the plugin. | |
# | |
# @param [String, Symbol] name | |
# The primary name of the command. | |
# | |
# @param [Array<Symbol, String, Array<String>>] arguments | |
# Arguments for the command. | |
# | |
# @param [Hash] options | |
# Additional options for the command. | |
# | |
# @option options [Symbol, Array<Symbol>] :aliases | |
# Additional aliases for the command. | |
# | |
def self.command(name,arguments=[],options={}) | |
name = name.to_s | |
aliases = options.fetch(:aliases,[]).map(&:to_s) | |
pattern = Regexp.union([name] + aliases).source | |
arguments.each do |argument| | |
pattern << ' ' << case argument | |
when Symbol | |
'(' + ARG_FORMATS.fetch(argument).source + ')' | |
else | |
'(?:' + Regexp.union(argument).source + ')' | |
end | |
end | |
match(Regexp.new(pattern), method: name) | |
commands[name] = { | |
arguments: arguments, | |
aliases: aliases, | |
summary: options[:summary] | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment