lib/cogy/command.rb in cogy-0.0.3 vs lib/cogy/command.rb in cogy-0.1.0
- old
+ new
@@ -1,43 +1,60 @@
module Cogy
+ # {Command} represents a user-defined registered command that can be used
+ # in the chat. It contains the Cog-related stuff (ie. everything that
+ # needs to be in the bundle config) and a block that will run and return
+ # the result (ie. handler).
class Command
- attr :name, :args, :opts, :desc, :long_desc, :examples, :rules, :handler
+ # The name of the command. Also used in {Cogy.bundle_config}.
+ #
+ # @return [String]
+ attr :name
- def initialize(name, args: [], opts: {}, desc:, long_desc: nil, examples: nil, rules: nil)
- @name = name
+ # The code that will run when the command is invoked
+ #
+ # @return [Proc]
+ attr :handler
+
+ # Attributes related to the bundle config in Cog
+ attr :args, :opts, :desc, :long_desc, :examples, :rules
+
+ # See {Cogy.on}
+ def initialize(name, handler, args: [], opts: {}, desc:, long_desc: nil, examples: nil, rules: nil)
+ @name = name.to_s
+ @handler = handler
@args = [args].flatten.map!(&:to_s)
@opts = opts.with_indifferent_access
@desc = desc
@long_desc = long_desc
@examples = examples
@rules = rules || ["allow"]
end
- def register!(handler)
+ # Registers a command.
+ #
+ # @raise [StandardError] if a command with the same name is already
+ # registered
+ #
+ # @return [self]
+ def register!
if Cogy.commands[name]
raise "A command with the name #{name} is already registered"
end
- @handler = handler
- @handler.command = self
-
Cogy.commands[name] = self
end
- def run!(*args)
- handler.run(*args)
- end
-
- # Suitable for bundle config display
+ # @return [String] the command arguments suitable for conversion to YAML
+ # for displaying in a bundle config.
def formatted_args
args.map { |a| "<#{a}>" }.join(" ")
end
- # Suitable for bundle config display.
- #
- # Get rid of HashWithIndifferentAccess, otherwise the resulting YAML
- # will contain garbage.
+ # @return [Hash] the command options suitable for conversion to YAML
+ # for displaying in a bundle config
def formatted_opts
+ # Convert to Hash in order to get rid of HashWithIndifferentAccess,
+ # otherwise the resulting YAML will contain garbage.
opts.to_hash
end
end
end