Skip to content

Instantly share code, notes, and snippets.

@kitwalker12
Created October 22, 2014 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitwalker12/7ca077b56dd5fd8915d6 to your computer and use it in GitHub Desktop.
Save kitwalker12/7ca077b56dd5fd8915d6 to your computer and use it in GitHub Desktop.
Method and Message (from Rubytapas)
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