-
Notifications
You must be signed in to change notification settings - Fork 0
/
08_east_injection_alarm.rb
265 lines (222 loc) · 6.15 KB
/
08_east_injection_alarm.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Plugins & East Oriented
# https://avdi.codes/topic/methods-vs-messages/
# https://avdi.codes/topic/pluggable-selector/
# exploring different IO instantiations
# ui = Object.const_get('IO').new(1) # file object 1 is standard out
# ui.puts 'Cat'
#
# ui=$stdout
# ui.puts 'Cat'
#
# ui=STDOUT
# ui.puts 'Cat'
# Exploration of just the timer
SleepTimer = Struct.new(:notifier, :notify_method) do
def start(minutes, notify_message)
sleep minutes * 60
notifier.send(notify_method, notify_message)
# notifier.public_send(notify_method, notify_message)
end
end
SleepTimer.new($stdout, :puts).start(0.1, "Woof")
# a self configuring stdout clock - with vairable time & message
class TeaClock
attr_accessor :ui
attr_accessor :timer
# pass the whole ui object to the timer, instead of a method object.
def initialize(ui_klass='IO', run_method=:puts, init_info=1)
self.ui = Object.const_get(ui_klass).new(init_info) # base method (super call this)
self.timer = SleepTimer.new(ui, run_method)
initialize_plugins
end
# allows runtime dynamic plugin classes to be used
def runtime_plugin(klass_name)
@plugins << Object.const_get(klass_name).new(self)
self # enable Fluent API (East Orientation) - chaining
end
def start(minutes=0.1, message='Tea Time')
timer.start(minutes, message)
self # enable Fluent API (East Orientation) - chaining
end
private
# looks for any constants (class names) defined in the Plugin module namespace,
def initialize_plugins
@plugins = []
# early return if ::Plugins const can't be found (avoids a crash)
return self unless Object.const_defined?('::Plugins')
# collect class names (constants) in the Plugins Namespace
::Plugins.constants.each do |name|
# initialize the plugin class(es) found
@plugins << ::Plugins.const_get(name).new(self)
end
self # enable Fluent API (East Orientation) - chaining
end
end
# default configs work
t = TeaClock.new
t.start
# demo changeable times and message
TeaClock.new.start(0.1, "IO-Bark")
# demo self config works
TeaClock.new('IO', :puts).start(0.1, "Bark-Bark")
# IO-Bark
# add a really basic UI implementation, that plays nicely with $stdout
class StdioUi
def initialize(_=nil)
end
def run(text)
puts text # calls the common interface
self # enable Fluent API (East Orientation) - chaining
end
def call(text)
puts "PUTS - #{text}" # calls the common interface
self
end
end
# home built Stdout class - works with no params
StdioUi.new.run 'hi'
# hi
# demo home built Stdout class - works / ignores given params
StdioUi.new(1).run 'hi'
# hi
# demo original behavior
TeaClock.new('StdioUi', :run).start(0.1, "Bark")
# Bark
# demo alterative call / config
TeaClock.new('StdioUi', :call).start(0.1, "Bark-Bark")
# PUTS - Bark-Bark
# Here’s are simple plugins to demo behavior and execution order
# It dynamically extends the ui object with a module that adds extra behavior to the notify method.
# The idea behind this plugin is that it will augment the StdioUi
module Plugins
class Beep
def initialize(tea_clock)
tea_clock.ui.extend(UiWithBeep)
end
module UiWithBeep
def run(*)
puts "BEEP!"
super
self # allow chaining
end
end
end
class Alarm
def initialize(tea_clock)
tea_clock.ui.extend(UiWithAlarm)
end
module UiWithAlarm
def run(*)
puts "BEEP - Beeeeeep!"
super
self
end
end
end
end
# demo original timer still flexible & can use stdout directly
SleepTimer.new($stdout, :puts).start(0.1, "Woof")
# Tea Time
# demo fancy teaclock with call configured doesn't use plugins
TeaClock.new('StdioUi', :call).start(0.1, "Bark-Bark")
# PUTS - Bark-Bark
# demo fancy teaclock with standard 'run' configured uses all the plugins
clock = TeaClock.new('StdioUi', :run).start(0.1, "Bark")
# BEEP!
# BEEP - Beeeeeep!
# Bark
# demo extend allows adding new module / methods on the fly
module UiWithBop
def run(message, *)
puts "<BOP> #{message}"
super
self
end
end
# config clock with run (plugin api)
clock = TeaClock.new('StdioUi', :run).start(0.1, "Bark")
# dynamically add Bop (following plugin api)
clock.ui.extend(UiWithBop)
clock.start(0.2, "Beer Time")
# <BOP> Beer Time
# BEEP!
# BEEP - Beeeep!
# Beer Time
# demo usage of a method - not following plugin api (just a new behavior)
module UiExecute
def execute(message)
puts message
self # allows chaining
end
end
# first extend, then 'execute' extension, then use the timer as planned with api plugins
TeaClock.new('StdioUi', :run).extend(UiExecute).execute("hi").start(0.05, 'Wow')
# hi
# <BOP> Wow
# BEEP!
# BEEP - Beeeeeep!
# Wow
class Run
def initialize(clock)
clock.ui.extend(UiRun)
end
module UiRun
def run(message, *)
puts "<run> #{message}"
super
self
end
end
end
# configure using standards for api (included in config remind)
clock = TeaClock.new('StdioUi', :run)
# demo adding the api compatible class on the fly
clock.runtime_plugin('Run')
# demo that the dynamically loaded plugin executes (in addition to the others)
clock.start(0.02, "Bed Time")
# <run> Bed Time
# <BOP> Bed Time
# BEEP!
# BEEP - Beeeeeep!
# Bed Time
# demo a new 'main class - that takes params
class StdioPuts
def call(message)
puts "<Call>"
puts message
self
end
end
tea = TeaClock.new('StdioPuts', :call)
tea.start(0.05, 'Wow')
# <Call>
# Wow
# demo chaining in action
module UiNewLine
def new_line
puts ''
self
end
end
# extend allows adding new methods on the fly (without plugin capabilities)
clock = TeaClock.new.extend(UiNewLine)
clock.start(0.2, "Beer Time")
# Beer Time
# returning self allows chaining
clock.start(0.2, "Beer Time").new_line.start(0.2, "Bed Time")
# Beer Time
#
# Bed Time
# extend allows adding new methods on the fly (WITH plugin capabilities)
TeaClock.new('StdioUi', :run).start(0.1, "Bark")
# BEEP!
# BEEP - Beep!
# Bark
# DEMO returning self allows chaining
TeaClock.new('StdioUi', :run).extend(UiNewLine).start(0.2, "Beer Time").new_line.start(0.2, "Bed Time")
# BEEP!
# BEEP - Beep!
# Beer Time
# BEEP!
# BEEP - Beep!
# Bed Time