Created
April 14, 2015 23:13
-
-
Save dbalatero/7f35f59938d2fbd70e65 to your computer and use it in GitHub Desktop.
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 Wisper::EnlightenedPublisher | |
extend ActiveSupport::Concern | |
included do | |
include Wisper::Publisher | |
prepend PrependedMethods | |
end | |
def subscribe_to_listeners | |
self.class.wisper_listeners.each do |listener| | |
subscribe(listener.constantize.new) | |
end | |
end | |
module PrependedMethods | |
def initialize(*args, &block) # ack what is the right signature here, I don't even know. | |
super | |
subscribe_to_listeners | |
end | |
end | |
module ClassMethods | |
def subscriber(klass) | |
wisper_subscriptions << klass | |
end | |
def wisper_subscriptions | |
@wisper_subscriptions ||= [] | |
end | |
end | |
end | |
class MyInteraction | |
include Wisper::EnlightenedPublisher | |
subscriber "AnalyticsListener" | |
def initialize(user) | |
@user = user | |
end | |
def run | |
broadcast :something | |
end | |
end |
You can do something similar with MyInteraction.subscribe(AnalyticsListener.new)
, see here. It will subscribe the analytics listener to every instance of MyInteraction
, it is also thread safe 😄
https://gist.github.com/dbalatero/7f35f59938d2fbd70e65#file-another_option-rb-L16 is correct for me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
def initialize(*)