aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@plataformatec.com.br>2013-09-20 08:18:01 -0700
committerJosé Valim <jose.valim@plataformatec.com.br>2013-09-20 08:18:01 -0700
commit34088572270a1dd5a2164b6aa5fc3642cb0479cb (patch)
treec3544acc1a380c8b09cd2bf422344640beef9b68 /activesupport/lib/active_support
parent218c089ae7fd997fda23cc604f18df95c58d31b0 (diff)
parentd2824a347fd827bb0af3c16ffed5a3608b358ffc (diff)
downloadrails-34088572270a1dd5a2164b6aa5fc3642cb0479cb.tar.gz
rails-34088572270a1dd5a2164b6aa5fc3642cb0479cb.tar.bz2
rails-34088572270a1dd5a2164b6aa5fc3642cb0479cb.zip
Merge pull request #12285 from dasch/dasch/allow-attaching-up-front
Allow attaching to AS::Notifications namespace up front
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r--activesupport/lib/active_support/subscriber.rb27
1 files changed, 25 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/subscriber.rb b/activesupport/lib/active_support/subscriber.rb
index 34c6f900c1..f2966f23fc 100644
--- a/activesupport/lib/active_support/subscriber.rb
+++ b/activesupport/lib/active_support/subscriber.rb
@@ -31,18 +31,41 @@ module ActiveSupport
# Attach the subscriber to a namespace.
def attach_to(namespace, subscriber=new, notifier=ActiveSupport::Notifications)
+ @namespace = namespace
+ @subscriber = subscriber
+ @notifier = notifier
+
subscribers << subscriber
+ # Add event subscribers for all existing methods on the class.
subscriber.public_methods(false).each do |event|
- next if %w{ start finish }.include?(event.to_s)
+ add_event_subscriber(event)
+ end
+ end
- notifier.subscribe("#{event}.#{namespace}", subscriber)
+ # Adds event subscribers for all new methods added to the class.
+ def method_added(event)
+ # Only public methods are added as subscribers, and only if a notifier
+ # has been set up. This means that subscribers will only be set up for
+ # classes that call #attach_to.
+ if public_method_defined?(event) && notifier
+ add_event_subscriber(event)
end
end
def subscribers
@@subscribers ||= []
end
+
+ protected
+
+ attr_reader :subscriber, :notifier, :namespace
+
+ def add_event_subscriber(event)
+ return if %w{ start finish }.include?(event.to_s)
+
+ notifier.subscribe("#{event}.#{namespace}", subscriber)
+ end
end
def initialize