Skip to content

Instantly share code, notes, and snippets.

@dbalatero
Created April 14, 2015 23:13
Show Gist options
  • Save dbalatero/7f35f59938d2fbd70e65 to your computer and use it in GitHub Desktop.
Save dbalatero/7f35f59938d2fbd70e65 to your computer and use it in GitHub Desktop.
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
@aaronjensen
Copy link

def initialize(*)

@krisleech
Copy link

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