Module: Barrymore::InstanceMethods

Defined in:
lib/barrymore/instance_methods.rb

Overview

Instance-level helpers

Instance Method Summary collapse

Instance Method Details

#chat_data(arg) ⇒ Object

arg [Object] - arg.chat or arg itself will be used



29
30
31
32
# File 'lib/barrymore/instance_methods.rb', line 29

def chat_data(arg)
  chat = arg.respond_to?(:chat) ? arg.chat : arg
  barrymore_chats_data[chat] ||= {}.freeze
end

#command_defined?(*args) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/barrymore/instance_methods.rb', line 40

def command_defined?(*args)
  self.class.command_defined?(*args)
end

#command_in_progress?(msg) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/barrymore/instance_methods.rb', line 44

def command_in_progress?(msg)
  commands_in_progress.key?(msg.chat)
end

#commands_in_progressObject



48
49
50
# File 'lib/barrymore/instance_methods.rb', line 48

def commands_in_progress
  @commands_in_progress ||= {}
end

#continue_command(msg) ⇒ Object



34
35
36
37
38
# File 'lib/barrymore/instance_methods.rb', line 34

def continue_command(msg)
  raise(NoCommandInProgressError, msg.chat.to_s) unless command_in_progress?(msg)
  proc = get_barrymore_command(commands_in_progress[msg.chat]).continuation
  instance_exec(msg, &proc)
end

#execute_command(msg) ⇒ Object

command should be specified in msg.text. chat should be specified in msg.chat.



8
9
10
11
# File 'lib/barrymore/instance_methods.rb', line 8

def execute_command(msg)
  raise(UndefinedCommandError, msg.text) unless command_defined?(msg)
  instance_exec(msg, &get_barrymore_command(msg).execution)
end

#set_chat_data(arg, data) ⇒ Object

arg [Object] - arg.chat or arg itself will be used



23
24
25
26
# File 'lib/barrymore/instance_methods.rb', line 23

def set_chat_data(arg, data)
  chat = arg.respond_to?(:chat) ? arg.chat : arg
  barrymore_chats_data[chat] = chat_data(arg).merge(data).freeze
end

#start_command_processing(msg) ⇒ Object



13
14
15
16
# File 'lib/barrymore/instance_methods.rb', line 13

def start_command_processing(msg)
  # TODO: use chat_data ?
  commands_in_progress[msg.chat] = msg.text
end

#stop_command_processing(msg) ⇒ Object



18
19
20
# File 'lib/barrymore/instance_methods.rb', line 18

def stop_command_processing(msg)
  commands_in_progress.delete(msg.chat)
end