summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-02 13:38:14 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-03 12:04:12 -0700
commit7292f123eee89301ac732edce1ccca2bdfdf2747 (patch)
treeedca8fdcc1659b65d86adbe91e3523bfe18eda6d /lib
parent27deff0d81499c9df32cfc0235792ea3a23834e4 (diff)
downloadchef-7292f123eee89301ac732edce1ccca2bdfdf2747.tar.gz
Fix issue #3463: use real constants instead of const_missing
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/resource.rb53
-rw-r--r--lib/chef/resource/lwrp_base.rb9
2 files changed, 41 insertions, 21 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index b8bf53d6ec..003683d66b 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -1274,31 +1274,48 @@ class Chef
end
end
- # Implement deprecated LWRP class
- module DeprecatedLWRPClass
- # @api private
- def register_deprecated_lwrp_class(resource_class, class_name)
- if Chef::Resource.const_defined?(class_name, false)
- Chef::Log.warn "#{class_name} already exists! Cannot create deprecation class for #{resource_class}"
- else
- deprecated_constants[class_name.to_sym] = resource_class
+ # @api private
+ def self.register_deprecated_lwrp_class(resource_class, class_name)
+ if Chef::Resource.const_defined?(class_name, false)
+ Chef::Log.warn "#{class_name} already exists! Deprecation class overwrites #{resource_class}"
+ Chef::Resource.send(:remove_const, class_name)
+ end
+ resource_subclass = class_eval <<-EOM, __FILE__, __LINE__+1
+ class Chef::Resource::#{class_name} < resource_class
+ def initialize(*args, &block)
+ Chef::Log.deprecation("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 13 and will be removed. Use Chef::Resource.resource_for_node(node, name) instead.")
+ super
+ end
+ self
+ end
+ EOM
+ # Make case, is_a and kind_of work with the new subclass, for backcompat
+ # Any subclass of Chef::Resource::ResourceClass is already a subclass of resource_class
+ # Any subclass of resource_class is considered a subclass of Chef::Resource::ResourceClass
+ resource_class.class_eval do
+ define_method(:is_a?) do |other|
+ other.is_a?(Module) && other === self
+ end
+ define_method(:kind_of?) do |other|
+ other.is_a?(Module) && other === self
end
end
-
- def const_missing(class_name)
- if deprecated_constants[class_name.to_sym]
+ resource_subclass.class_eval do
+ define_singleton_method(:===) do |other|
Chef::Log.deprecation("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 13 and will be removed. Use Chef::Resource.resource_for_node(node, name) instead.")
- deprecated_constants[class_name.to_sym]
- else
- raise NameError, "uninitialized constant Chef::Resource::#{class_name}"
+ # resource_subclass is a superclass of all resource_class descendants.
+ if self == resource_subclass && other.class <= resource_class
+ return true
+ end
+ super(other)
end
end
+ deprecated_constants[class_name.to_sym] = resource_subclass
+ end
- def deprecated_constants
- @deprecated_constants ||= {}
- end
+ def self.deprecated_constants
+ @deprecated_constants ||= {}
end
- extend DeprecatedLWRPClass
# @api private
def lookup_provider_constant(name, action=:nothing)
diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb
index 20d236a161..ea47ad4b06 100644
--- a/lib/chef/resource/lwrp_base.rb
+++ b/lib/chef/resource/lwrp_base.rb
@@ -58,7 +58,10 @@ class Chef
resource_class.class_from_file(filename)
# Make a useful string for the class (rather than <Class:312894723894>)
- resource_class.instance_eval do
+ resource_class.class_eval do
+ define_method(:kind_of?) { |other| other.class <= resource_class }
+ define_method(:is_a?) { |other| other.class <= resource_class }
+
define_singleton_method(:to_s) do
"LWRP resource #{resource_name} from cookbook #{cookbook_name}"
end
@@ -69,8 +72,8 @@ class Chef
LWRPBase.loaded_lwrps[filename] = true
- Chef::Resource.register_deprecated_lwrp_class(resource_class, convert_to_class_name(resource_name))
-
+ # Create the deprecated Chef::Resource::LwrpFoo class
+ resource_subclass = Chef::Resource.register_deprecated_lwrp_class(resource_class, convert_to_class_name(resource_name))
resource_class
end