summaryrefslogtreecommitdiff
path: root/lib/chef/event_dispatch
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-09-01 13:01:42 -0700
committerJohn Keiser <john@johnkeiser.com>2015-09-01 19:30:45 -0700
commit310f0104d0807f46b77e6055bb20bb20276f33ad (patch)
tree85c351fe9865176c75cfad0897298b89dad6b7f8 /lib/chef/event_dispatch
parent25dac92eeb1ffa83ec549bfed0b19672c5847d80 (diff)
downloadchef-310f0104d0807f46b77e6055bb20bb20276f33ad.tar.gz
Make Dispatcher metaprogramming more straightforward
Diffstat (limited to 'lib/chef/event_dispatch')
-rw-r--r--lib/chef/event_dispatch/dispatcher.rb36
1 files changed, 18 insertions, 18 deletions
diff --git a/lib/chef/event_dispatch/dispatcher.rb b/lib/chef/event_dispatch/dispatcher.rb
index 0c5b27514c..0f05365ab1 100644
--- a/lib/chef/event_dispatch/dispatcher.rb
+++ b/lib/chef/event_dispatch/dispatcher.rb
@@ -25,30 +25,30 @@ class Chef
# define the forwarding in one go:
#
- # Define a method that will be forwarded to all
- def self.def_forwarding_method(method_name)
- define_method(method_name) do |*args|
- if method_name == :deprecation && args.size == 1
- args << caller(2..2)[0]
- end
- @subscribers.each do |s|
- # Skip new/unsupported event names.
- if s.respond_to?(method_name)
- mth = s.method(method_name)
- # Anything with a *args is arity -1, so use all arguments.
- arity = mth.arity < 0 ? args.length : mth.arity
- # Trim arguments to match what the subscriber expects to allow
- # adding new arguments without breaking compat.
- mth.call(*args.take(arity))
- end
- end
+ def call_subscribers(method_name, *args)
+ @subscribers.each do |s|
+ # Skip new/unsupported event names.
+ next if !s.respond_to?(method_name)
+ mth = s.method(method_name)
+ # Trim arguments to match what the subscriber expects to allow
+ # adding new arguments without breaking compat.
+ args = args.take(arity) if mth.arity < args.size && mth.arity >= 0
+ mth.call(*args)
end
end
(Base.instance_methods - Object.instance_methods).each do |method_name|
- def_forwarding_method(method_name)
+ class_eval <<-EOM
+ def #{method_name}(*args)
+ call_subscribers(#{method_name.inspect}, *args)
+ end
+ EOM
end
+ # Special case deprecation, since it needs to know its caller
+ def deprecation(message, location=caller(2..2)[0])
+ call_subscribers(:deprecation, message, location)
+ end
end
end
end