Class: Commandant::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description = nil, &command) ⇒ Command

Create a new command by name and add it to the commands list.

Parameters:

  • name (Symbol)

    the name of the command

  • description (String, nil) (defaults to: nil)

    the command’s description

Raises:

  • (ArgumentError)

    if a command block is not provided

  • (ArgumentError)

    if a command already exists by that name



11
12
13
14
15
16
17
18
19
20
# File 'lib/commandant/command.rb', line 11

def initialize(name, description=nil, &command)
  raise ArgumentError, "Must provide a command block" unless command
  raise ArgumentError, "Command already exists: #{name}" if COMMANDS[name]

  @name        = name
  @description = description
  @command     = command

  COMMANDS[@name] = self
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/commandant/command.rb', line 3

def description
  @description
end

Instance Method Details

#call(args = nil) ⇒ Object

Execute the command

Parameters:

  • args (Array) (defaults to: nil)

    the command’s arguments



25
26
27
# File 'lib/commandant/command.rb', line 25

def call(args=nil)
  @command.call(args)
end