Class: MuchRailsPubSub::Config::Subscriptions
- Inherits:
-
Object
- Object
- MuchRailsPubSub::Config::Subscriptions
- Defined in:
- lib/much-rails-pub-sub.rb
Instance Method Summary collapse
- #<<(subscription) ⇒ Object
- #dispatch(publish_params) ⇒ Object
- #for_event(event_name) ⇒ Object
-
#initialize ⇒ Subscriptions
constructor
A new instance of Subscriptions.
Constructor Details
#initialize ⇒ Subscriptions
Returns a new instance of Subscriptions.
113 114 115 |
# File 'lib/much-rails-pub-sub.rb', line 113 def initialize @subscriptions = Hash.new{ |hash, key| hash[key] = ::Set.new } end |
Instance Method Details
#<<(subscription) ⇒ Object
121 122 123 124 |
# File 'lib/much-rails-pub-sub.rb', line 121 def <<(subscription) @subscriptions[normalize_event_name(subscription.event_name)] << subscription end |
#dispatch(publish_params) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/much-rails-pub-sub.rb', line 126 def dispatch(publish_params) event_id = publish_params["event_id"] event_name = publish_params["event_name"] event_params = publish_params["event_params"] subscriptions = for_event(event_name) subscription_log_details = subscriptions .map{ |subscription| " - #{subscription.job_class.inspect}" } .join("\n") subscriptions.each{ |subscription| subscription.call(event_params) } logger.info( "[MuchRailsPubSub] Dispatched #{subscriptions.size} subscription "\ "job(s) for #{event_name.inspect} (#{event_id}):\n"\ "#{subscription_log_details}", ) end |
#for_event(event_name) ⇒ Object
117 118 119 |
# File 'lib/much-rails-pub-sub.rb', line 117 def for_event(event_name) @subscriptions[normalize_event_name(event_name)].to_a end |