Class: RubyWarrior::UI

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_warrior/ui.rb

Class Method Summary collapse

Class Method Details

.ask(msg) ⇒ Object



27
28
29
# File 'lib/ruby_warrior/ui.rb', line 27

def ask(msg)
  request("#{msg} [yn] ") == 'y'
end

.choose(item, options) ⇒ Object

REFACTORME



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ruby_warrior/ui.rb', line 32

def choose(item, options)
  if options.length == 1
    response = options.first
  else
    options.each_with_index do |option, i|
      if option.kind_of? Array
        puts "[#{i+1}] #{option.last}"
      else
        puts "[#{i+1}] #{option}"
      end
    end
    choice = request("Choose #{item} by typing the number: ")
    response = options[choice.to_i-1]
  end
  if response.kind_of? Array
    response.first
  else
    response
  end
end

.getsObject



18
19
20
# File 'lib/ruby_warrior/ui.rb', line 18

def gets
  Config.in_stream ? Config.in_stream.gets : ''
end


14
15
16
# File 'lib/ruby_warrior/ui.rb', line 14

def print(msg)
  Config.out_stream.print(msg) if Config.out_stream
end

.puts(msg) ⇒ Object



4
5
6
# File 'lib/ruby_warrior/ui.rb', line 4

def puts(msg)
  Config.out_stream.puts(msg) if Config.out_stream
end

.puts_with_delay(msg) ⇒ Object



8
9
10
11
12
# File 'lib/ruby_warrior/ui.rb', line 8

def puts_with_delay(msg)
  result = puts(msg)
  sleep(Config.delay) if Config.delay
  result
end

.request(msg) ⇒ Object



22
23
24
25
# File 'lib/ruby_warrior/ui.rb', line 22

def request(msg)
  print(msg)
  gets.chomp
end