-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.rb
75 lines (61 loc) · 1.09 KB
/
remote.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class RemoteControl
def initialize
@on_commands = []
@off_commands = []
no_command = NoCommand.new
(0..7).each do |index|
on_commands[index] = no_command
off_commands[index] = no_command
end
@undo_command = no_command
end
def set_command(off_command, on_command, slot)
@on_commands[slot] = on_command
@off_commands[slot] = off_command
end
def on_button_was_pressed(slot)
@on_commands[slot].execute
@undo_command = on_commands[slot]
end
def off_button_was_pressed(slot)
@off_commands[slot].execute
@undo_command = off_commands[slot]
end
def undo_button_was_pressed
@undo_command.undo
end
end
class NoCommand
def execute
end
end
class Light
def on
puts "light turned on"
end
def off
puts "Light turned off"
end
end
class LightOffCommand
def initialize(light)
@light = light
end
def execute
@light.off
end
def undo
@light.on
end
end
class LightOnCommand
def initialize(light)
@light = light
end
def execute
@light.on
end
def undo
@light.off
end
end