Created
October 2, 2012 20:01
-
-
Save mwean/3822941 to your computer and use it in GitHub Desktop.
Message-Based Callbacks
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 FmsEngine | |
module Core | |
class Callbacks | |
class << self | |
# Redis or something: | |
CALLBACK_REGISTRY = Hash.new { |h, k| h[k] = [] } | |
def bind(event, klass, callback_method, facade_method) | |
CALLBACK_REGISTRY[event] << { klass: klass, callback_method: callback_method, facade_method: facade_method } | |
end | |
def fire(event, object) | |
relevant_callbacks = CALLBACK_REGISTRY[event] | |
relevant_callbacks.each do |callback| | |
callback[:klass].send(callback[:callback_method], object.send(callback[:facade_method])) | |
end | |
end | |
end | |
end | |
end | |
end | |
module FmsEngine | |
module Billing | |
class SomeClass | |
FmsEngine::Core::Callbacks.bind('enrollment.consumer.created', self, :new_consumer, :demographic_info) | |
# FmsEngine::Core::Callbacks.bind('consumer.update', self, :update_my_consumer) | |
def self.new_consumer(demographic_info) | |
p demographic_info | |
end | |
def self.update_my_consumer(consumer_id) | |
p "Consumer #{consumer_id.to_s} was Updated" | |
end | |
end | |
end | |
end | |
module FmsEngine | |
module Enrollment | |
class Consumer | |
def physical_address | |
'Some Address' | |
end | |
def first_name | |
'Jim' | |
end | |
def last_name | |
'Halpert' | |
end | |
end | |
class ConsumerFacade | |
def initialize(consumer) | |
@consumer = consumer | |
end | |
def address | |
@consumer.physical_address | |
end | |
def demographic_info | |
{ first_name: @consumer.first_name, last_name: @consumer.last_name } | |
end | |
end | |
class ConsumerPublisher | |
def self.updated_consumer(id) | |
FmsEngine::Core::CallbackRegistry.fire_callback('consumer.did_update', id) | |
end | |
def self.created_consumer(id) | |
consumer_facade = ConsumerFacade.new(Consumer.new) | |
FmsEngine::Core::Callbacks.fire('enrollment.consumer.created', consumer_facade) | |
end | |
end | |
end | |
end | |
FmsEngine::Enrollment::ConsumerPublisher.created_consumer(12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment