Class: CommandBot::CommandCall

Inherits:
Object
  • Object
show all
Defined in:
lib/command_bot/command_call.rb

Overview

Stores command call information.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, text, data) ⇒ CommandCall

New command call.

Parameters:

  • bot (Bot)
  • text (String)
  • data (Hash)


10
11
12
13
14
15
16
17
18
# File 'lib/command_bot/command_call.rb', line 10

def initialize(bot, text, data)
  @bot = bot
  @text = text
  @data = data

  @command = nil
  @arguments = []
  @options = {}
end

Instance Attribute Details

#argumentsArray<String>

Returns:

  • (Array<String>)


33
34
35
# File 'lib/command_bot/command_call.rb', line 33

def arguments
  @arguments
end

#botBot (readonly)

Returns:



21
22
23
# File 'lib/command_bot/command_call.rb', line 21

def bot
  @bot
end

#commandCommand?

Returns nil if command can’t be found.

Returns:

  • (Command, nil)

    nil if command can’t be found.



30
31
32
# File 'lib/command_bot/command_call.rb', line 30

def command
  @command
end

#dataHash (readonly)

Returns:

  • (Hash)


27
28
29
# File 'lib/command_bot/command_call.rb', line 27

def data
  @data
end

#optionsHash<String, String>

Returns:

  • (Hash<String, String>)


36
37
38
# File 'lib/command_bot/command_call.rb', line 36

def options
  @options
end

#textString (readonly)

Returns:

  • (String)


24
25
26
# File 'lib/command_bot/command_call.rb', line 24

def text
  @text
end

Instance Method Details

#executevoid

This method returns an undefined value.

Executes command.



40
41
42
43
44
# File 'lib/command_bot/command_call.rb', line 40

def execute
  raise 'Command not identified!' if command.nil?

  command.handler.call(bot, arguments, options, data)
end

#to_sString

Returns:

  • (String)


47
48
49
50
51
52
53
54
55
# File 'lib/command_bot/command_call.rb', line 47

def to_s
  if command.nil?
    "COMMAND_NOT_FOUND(`#{text}`)"
  else
    arg_str = arguments.join(', ')
    opt_str = options.map { |k, v| "#{k}: #{v}" }.join(', ')
    "#{command.name}[#{arg_str}]{#{opt_str}}"
  end
end