Skip to content

Instantly share code, notes, and snippets.

@eyston
Created June 9, 2011 14:45
Show Gist options
  • Save eyston/1016863 to your computer and use it in GitHub Desktop.
Save eyston/1016863 to your computer and use it in GitHub Desktop.
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