I have a service that broadcast an event when some criteria is satisfied. I also have several customer specific services. I want the customer services to be notified of the event. However, I would rather not add a subscription to the broadcasting service or to an initializer each time a customer and its associated service is added. I want the broadcasting service to be unaware of the specific listeners so that we don't have to add code to that service each time a customer is added. Is there some way, I can have one subscription (either in the broadcasting service or an initializer) that can notify all customer services? This is a Ruby/Rails application and the customer services are not instantiated -- they contain class methods.
Asked
Active
Viewed 143 times
1 Answers
0
I solved this by iterating over all classes that are a member of my Customer module and subscribing each. This was added to application.rb.
config.after_initialize do
Customer.constants.select { |c| Customer.const_get(c).is_a? Class }
.map { |klass| 'Customer::' + klass.to_s }
.each { |klass| ScanEventService.subscribe(klass.constantize) }
end
Each time I add a new customer service, I make it a member of this module.

David
- 1
- 1