Skip to content

Instantly share code, notes, and snippets.

@eyston
Created Jun 9, 2011
Embed
What would you like to do?
Ruby Events? Maybe?
class EventBus
def self.publish event args
@subscribers[event].each do |sub|
# whatever meta stuff sends event to method on object (handler) :p
end
end
def self.subscribe event, handler, method
@subscribers[event] = { :handler => handler, :method => method }
end
module EventHandler
def subscribe event, method=:handle
EventBus.subscribe event, self, method # self might be in the wrong context here, I'm ruby meta dumb
end
end
class CustomerPreferredHandler
include EventHandler
subscribe :customer_preferred_event
def handle customer_preferred_event_args
# wutevs
end
end
EventBus.send :customer_preferred_event, { :some => args }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment