summaryrefslogtreecommitdiff
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
parent25dac92eeb1ffa83ec549bfed0b19672c5847d80 (diff)
downloadchef-310f0104d0807f46b77e6055bb20bb20276f33ad.tar.gz
Make Dispatcher metaprogramming more straightforward
-rw-r--r--lib/chef/event_dispatch/dispatcher.rb36
-rw-r--r--lib/chef/log.rb2
2 files changed, 19 insertions, 19 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
diff --git a/lib/chef/log.rb b/lib/chef/log.rb
index 2cf08324c8..aff6252e12 100644
--- a/lib/chef/log.rb
+++ b/lib/chef/log.rb
@@ -38,7 +38,7 @@ class Chef
end
def self.deprecation(msg=nil, location=caller(2..2)[0], &block)
- msg = Array(msg) + [ location ] if location
+ msg << " at #{Array(location).join("\n")}"
if Chef::Config[:treat_deprecation_warnings_as_errors]
error(msg, &block)
raise Chef::Exceptions::DeprecatedFeatureError.new(msg)