-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.rb
49 lines (41 loc) · 841 Bytes
/
router.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Router
def initialize(controller, view)
@controller = controller
@view = view
@running = true
end
def run
@view.welcome
@controller.list
while @running
display_tasks
action = @view.ask_for("action")
@view.clear
perform(action)
end
end
def perform(action)
index = action.to_i - 1
if index >= 0 && index < tasks.size
task = tasks[index][1]
task[0].send(task[1])
else
@view.invalid_action
end
end
def display_tasks
@view.header
@view.list_with_index(tasks.map(&:first))
end
def tasks
[
["List users", [@controller, :list]],
["Add order", [@controller, :order]],
["Create user", [@controller, :create]],
["Exit program", [self, :stop]],
]
end
def stop
@running = false
end
end