Created
October 22, 2014 18:53
-
-
Save kitwalker12/7ca077b56dd5fd8915d6 to your computer and use it in GitHub Desktop.
Method and Message (from Rubytapas)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TeaClock | |
attr_accessor :timer | |
attr_accessor :ui | |
def initialize(minutes) | |
#Tie ui object | |
self.ui = StdioUi.new | |
#Tie timer object. Pass ui object so that the final implementation of notify (after plugins are loaded) is called | |
self.timer = SleepTimer.new(minutes, ui) | |
#Extend ui or timer objects | |
init_plugins | |
end | |
def init_plugins | |
@plugins = [] | |
::Plugins.constants.each do |name| | |
#Call Beep class to extend ui object with it's implementation of notify | |
@plugins << ::Plugins.const_get(name).new(self) | |
end | |
end | |
def start | |
#call start on timer object which would be the SleepTimer in this case | |
timer.start | |
end | |
end | |
SleepTimer = Struct.new(:minutes, :notifier) do | |
def start | |
sleep minutes * 60 | |
notifier.notify('Tea is Ready!') | |
end | |
end | |
class StdioUi | |
def notify(text) | |
puts text | |
end | |
end | |
module Plugins | |
class Beep | |
def initialize(tea_clock) | |
tea_clock.ui.extend(UiWithBeep) | |
end | |
module UiWithBeep | |
def notify(*) | |
puts "Beep!" | |
super | |
end | |
end | |
end | |
end | |
t = TeaClock.new(0.01).start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment