Class: RubyWarrior::UI

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.delayObject

Returns the value of attribute delay.



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

def delay
  @delay
end

Class Method Details

.ask(msg) ⇒ Object



37
38
39
# File 'lib/ruby_warrior/ui.rb', line 37

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

.choose(item, options) ⇒ Object

REFACTORME



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ruby_warrior/ui.rb', line 42

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



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

def gets
  @in ? @in.gets : ''
end

.in_stream=(in_stream) ⇒ Object



6
7
8
# File 'lib/ruby_warrior/ui.rb', line 6

def in_stream=(in_stream)
  @in = in_stream
end

.out_stream=(stream) ⇒ Object



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

def out_stream=(stream)
  @out = stream
end


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

def print(msg)
  @out.print(msg) if @out
end

.puts(msg) ⇒ Object



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

def puts(msg)
  @out.puts(msg) if @out
end

.puts_with_delay(msg) ⇒ Object



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

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

.request(msg) ⇒ Object



32
33
34
35
# File 'lib/ruby_warrior/ui.rb', line 32

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