Module: Toast::ConsoleUtils

Defined in:
lib/console_utils.rb

Class Method Summary collapse

Class Method Details

.class_for_file_name(file) ⇒ Object



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

def self.class_for_file_name file
  File.basename(file,'.rb').classify.constantize
end

.get_bot_class_for_name(bot_name = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/console_utils.rb', line 3

def self.get_bot_class_for_name bot_name=nil

  # If it's nil, show a menu for .
  if bot_name.nil?
    file=show_menu_for_directory '.'
    load file
    return class_for_file_name(file)
  end

  # If it's a file, then we just use it
  if File.file? bot_name
    load bot_name
    return class_for_file_name(bot_name)
  end

  # if it's not a .rb file, but is present, it's a directory
  if File.directory? bot_name
    file=show_menu_for_directory(bot_name)
    load file
    return class_for_file_name(file)
  end

  raise "Can't find #{bot_name}"
end

.show_menu_for_directory(path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/console_utils.rb', line 28

def self.show_menu_for_directory path
  n=0
  Dir.glob("#{path}/*.rb").each do |file|
    n+=1
    name=File.basename(file,'.rb')
    puts "- #{name}"
    Readline::HISTORY.push(name)
  end

  if n==0
    path="current directory" if path=='.'
    raise "No .rb files found in #{path}"
  end

  bot_name=Readline::readline('Which bot: ')
  n.times {Readline::HISTORY.shift}

  return "#{path}/#{bot_name}.rb"
end