Class: Cogy::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/cogy/command.rb

Overview

Command represents a user-defined, registered command that can be used in the chat. It contains information about Cog-related stuff (ie. everything that needs to be in the bundle config like args & opts) and the block that will return the result (#handler).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, handler, args: [], opts: {}, desc:, long_desc: nil, examples: nil, rules: nil, template: nil) ⇒ Command

This is typically used via Cogy.on which also registers the newly created Cogy::Command.

Parameters:

  • name (String, Symbol)

    the name of the command. This is how the command will be invoked in the chat.

  • handler (Proc)

    the code that will run when the command is invoked

  • args (Array<Symbol, String>, Symbol, String) (defaults to: [])

    the arguments accepted by the command

  • opts (Hash{Symbol=>Hash}) (defaults to: {})

    the options accepted by the command

  • desc (String)

    the description

  • long_desc (String) (defaults to: nil)

    the long description

  • examples (String) (defaults to: nil)

    usage examples of the command

  • rules (Array) (defaults to: nil)

    the command rules

  • template (String) (defaults to: nil)

    the name of the template to use

Raises:

  • (ArgumentError)

    if #opts are invalid

See Also:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cogy/command.rb', line 55

def initialize(name, handler, args: [], opts: {}, desc:, long_desc: nil,
               examples: nil, rules: nil, template: 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"]
  @template = template

  validate_opts
end

Instance Attribute Details

#argsArray (readonly)

Returns:

  • (Array)


14
15
16
# File 'lib/cogy/command.rb', line 14

def args
  @args
end

#descString (readonly)

Returns:

  • (String)


20
21
22
# File 'lib/cogy/command.rb', line 20

def desc
  @desc
end

#examplesString (readonly)

Returns:

  • (String)


26
27
28
# File 'lib/cogy/command.rb', line 26

def examples
  @examples
end

#handlerProc (readonly)

Returns:

  • (Proc)


11
12
13
# File 'lib/cogy/command.rb', line 11

def handler
  @handler
end

#long_descString (readonly)

Returns:

  • (String)


23
24
25
# File 'lib/cogy/command.rb', line 23

def long_desc
  @long_desc
end

#nameString (readonly)

Returns:

  • (String)


8
9
10
# File 'lib/cogy/command.rb', line 8

def name
  @name
end

#optsHash{Symbol=>Hash} (readonly)

Returns:

  • (Hash{Symbol=>Hash})


17
18
19
# File 'lib/cogy/command.rb', line 17

def opts
  @opts
end

#rulesArray (readonly)

Returns:

  • (Array)


29
30
31
# File 'lib/cogy/command.rb', line 29

def rules
  @rules
end

#templateString (readonly)

Returns:

  • (String)


32
33
34
# File 'lib/cogy/command.rb', line 32

def template
  @template
end

Instance Method Details

#formatted_argsString

Returns the command arguments suitable for conversion to YAML for displaying in a bundle config.

Returns:

  • (String)

    the command arguments suitable for conversion to YAML for displaying in a bundle config.



86
87
88
# File 'lib/cogy/command.rb', line 86

def formatted_args
  args.map { |a| "<#{a}>" }.join(" ")
end

#formatted_optsHash

Returns the command options suitable for conversion to YAML for displaying in a bundle config.

Returns:

  • (Hash)

    the command options suitable for conversion to YAML for displaying in a bundle config



92
93
94
95
96
# File 'lib/cogy/command.rb', line 92

def formatted_opts
  # Convert to Hash in order to get rid of HashWithIndifferentAccess,
  # otherwise the resulting YAML will contain garbage.
  opts.to_hash
end

#register!self

Registers a command.

Returns:

  • (self)

Raises:

  • (StandardError)

    if a command with the same name is already registered



76
77
78
79
80
81
82
# File 'lib/cogy/command.rb', line 76

def register!
  if Cogy.commands[name]
    raise "A command with the name #{name} is already registered"
  end

  Cogy.commands[name] = self
end