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, readonly: nil) ⇒ Command

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

Parameters:

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

  • the code that will run when the command is invoked

  • (defaults to: [])

    the arguments accepted by the command

  • (defaults to: {})

    the options accepted by the command

  • the description

  • (defaults to: nil)

    the long description

  • (defaults to: nil)

    usage examples of the command

  • (defaults to: nil)

    the command rules

  • (defaults to: nil)

    the name of the template to use

  • (defaults to: nil)

    whether the command should use the readonly active record connection or not

Raises:

See Also:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cogy/command.rb', line 60

def initialize(name, handler, args: [], opts: {}, desc:, long_desc: nil,
               examples: nil, rules: nil, template: nil, readonly: 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.is_a?(Array) ? examples.join("\n\n") : examples
  @rules = rules || ["allow"]
  @template = template
  @readonly = readonly.nil? ? Cogy.readonly_by_default : readonly

  validate_opts
end

Instance Attribute Details

#argsArray (readonly)

Returns:



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

def args
  @args
end

#descString (readonly)

Returns:



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

def desc
  @desc
end

#examplesString (readonly)

Returns:



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

def examples
  @examples
end

#handlerProc (readonly)

Returns:



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

def handler
  @handler
end

#long_descString (readonly)

Returns:



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

def long_desc
  @long_desc
end

#nameString (readonly)

Returns:



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

def name
  @name
end

#optsHash{Symbol=>Hash} (readonly)

Returns:



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

def opts
  @opts
end

#readonlyBoolean (readonly)

Returns:



35
36
37
# File 'lib/cogy/command.rb', line 35

def readonly
  @readonly
end

#rulesArray (readonly)

Returns:



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

def rules
  @rules
end

#templateString (readonly)

Returns:



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:

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



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

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:

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



98
99
100
101
102
# File 'lib/cogy/command.rb', line 98

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:

Raises:

  • if a command with the same name is already registered



82
83
84
85
86
87
88
# File 'lib/cogy/command.rb', line 82

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

  Cogy.commands[name] = self
end