Class: Drawy::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/drawy/application.rb

Constant Summary collapse

[
  ['Register someone for the drawing', :register],
  ['Proceed to drawing', :draw],
  ['Check someone\'s result', :check],
  ['Exit', :exit]
]

Class Method Summary collapse

Class Method Details

.checkObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/drawy/application.rb', line 47

def check
  if Drawy::Engine.result.nil?
    raise Drawy::InputError, 'You must proceed to drawing before checking someone'
  end

  name = Drawy::IO.accept :string, 'Enter the name of the person you want to check:'

  if name.empty?
    raise Drawy::InputError, 'Entered name is empty... ignoring.'
  end

  result = Drawy::Engine.result[name.downcase]
  Drawy::IO.show "#{name.capitalize} should buy #{result.capitalize} a gift."
end

.drawObject



40
41
42
43
44
45
# File 'lib/drawy/application.rb', line 40

def draw
  Drawy::Validator.validate_people_format
  Drawy::IO.show 'Drawing...'
  Drawy::Engine.draw
  Drawy::IO.show 'Done! You can now check everyone\'s result.'
end

.exitObject



62
63
64
65
# File 'lib/drawy/application.rb', line 62

def exit
  Drawy::IO.show 'See you soon'
  Kernel.exit
end


22
23
24
25
26
# File 'lib/drawy/application.rb', line 22

def menu
  Drawy::IO.show 'What would you like to do?'
  MENU.each_with_index { |a, i| Drawy::IO.show "#{i+1}. #{a.first}" }
  Drawy::IO.accept :integer, "Enter the number corresponding to the action you want to perform: #{@menu_numbers}"
end

.registerObject



33
34
35
36
37
38
# File 'lib/drawy/application.rb', line 33

def register
  name         = Drawy::IO.accept :string, 'Enter the name of the person you want to register:'
  partner_name = Drawy::IO.accept :string, 'Enter the name of his/her partner (leave blank if the person is single):'
  Drawy::Validator.validate_uniqueness_of(name, partner_name)
  Drawy::Engine.add_people [name, partner_name]
end

.route_for(number = nil) ⇒ Object

Raises:



28
29
30
31
# File 'lib/drawy/application.rb', line 28

def route_for(number = nil)
  return send(MENU[number-1][1]) if @menu_numbers.include?(number)
  raise Drawy::InputError, "Please choose a number in #{@menu_numbers}"
end

.startObject



16
17
18
19
20
# File 'lib/drawy/application.rb', line 16

def start
  @menu_numbers = *(1..MENU.size)
  route_for(menu)
  start
end