Class: Drawy::Application
- Inherits:
-
Object
- Object
- Drawy::Application
- Defined in:
- lib/drawy/application.rb
Constant Summary collapse
- MENU =
To expand the application, just add an entry in the menu and write the corresponding method The entry is of form [‘Text to display’, :method_to_route_to]
[ ['Register someone for the drawing', :register], ['Proceed to drawing', :draw], ['Check someone\'s result', :check], ['Exit', :exit] ]
Class Method Summary collapse
- .check ⇒ Object
- .draw ⇒ Object
- .exit ⇒ Object
- .menu ⇒ Object
- .register ⇒ Object
- .route_for(number = nil) ⇒ Object
- .start ⇒ Object
Class Method Details
.check ⇒ Object
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 |
.draw ⇒ Object
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 |
.exit ⇒ Object
62 63 64 65 |
# File 'lib/drawy/application.rb', line 62 def exit Drawy::IO.show 'See you soon' Kernel.exit end |
.menu ⇒ Object
22 23 24 25 26 |
# File 'lib/drawy/application.rb', line 22 def 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 |
.register ⇒ Object
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
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 |
.start ⇒ Object
16 17 18 19 20 |
# File 'lib/drawy/application.rb', line 16 def start @menu_numbers = *(1..MENU.size) route_for() start end |