Created
August 28, 2012 14:23
-
-
Save dgtm/3498456 to your computer and use it in GitHub Desktop.
Cheap Callback Pass 1
This file contains 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
module Actions | |
module Base | |
def email(args) | |
extend(Mail) | |
send(*args) | |
end | |
def statistics(m,*args) | |
extend(Statistics) | |
send(m,*args) | |
end | |
end | |
module Mail | |
def complete(args) | |
puts "complete mail" | |
end | |
def auto_posted | |
puts "posted" | |
end | |
end | |
module Statistics | |
def complete(args) | |
puts "complete stats" | |
end | |
def posted(args) | |
puts "posted #{args}" | |
end | |
end | |
end | |
module Events | |
attr_accessor :wrapper | |
def self.extended(base) | |
base.wrapper = {} | |
base.send(:include,Triggers) | |
end | |
def completed_trip(&block) | |
wrapper[:completed_trip] = block | |
end | |
def auto_posted(&block) | |
wrapper[:auto_posted] = block | |
end | |
end | |
module Triggers | |
def auto_posted! | |
current_wrapper = self.class.wrapper[:auto_posted] | |
send(:extend, Actions::Base) | |
current_wrapper.call(self) | |
end | |
def completed_trip! | |
current_wrapper = self.class.wrapper[:completed_trip] | |
send(:extend, Actions::Base) | |
current_wrapper.call(self) | |
end | |
end | |
module CallbackChain | |
def notification_callbacks(&block) | |
send(:extend, Events) | |
instance_eval(&block) | |
end | |
end | |
class A | |
extend CallbackChain | |
notification_callbacks do | |
completed_trip do |d| | |
d.email :complete | |
d.statistics :complete, 2 | |
end | |
auto_posted do |d| | |
d.email :auto_posted | |
d.statistics :posted, 1 | |
end | |
end | |
end | |
A.new.completed_trip! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment