diff options
author | Jay Mundrawala <jdmundrawala@gmail.com> | 2015-06-30 09:40:17 -0700 |
---|---|---|
committer | Jay Mundrawala <jdmundrawala@gmail.com> | 2015-06-30 11:50:38 -0700 |
commit | 1d25d780a69ebc118494939a8d315f6ec6d9ad3f (patch) | |
tree | af68f02e6d49d19d2d5c75774e72947fd1b288b1 /lib/chef/resource.rb | |
parent | 480d18f26f17c289889ed65d3f8a50e5b336053f (diff) | |
download | chef-1d25d780a69ebc118494939a8d315f6ec6d9ad3f.tar.gz |
Revert "Merge pull request #3603 from chef/jdm/lwrp-base"
This reverts commit 27d8675ec1c80f1eb7ec57f7b6c854441bb395ee, reversing
changes made to 3501ba4020dc21377bc999e57e25eadb315ec783.
Diffstat (limited to 'lib/chef/resource.rb')
-rw-r--r-- | lib/chef/resource.rb | 53 |
1 files changed, 50 insertions, 3 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 7965068037..0abf874044 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -1499,9 +1499,56 @@ class Chef Chef::Resource.send(:remove_const, class_name) end - Chef::Resource.const_set(class_name, resource_class) - - deprecated_constants[class_name.to_sym] = resource_class + # In order to generate deprecation warnings when you use Chef::Resource::MyLwrp, + # we make a special subclass (identical in nearly all respects) of the + # actual LWRP. When you say any of these, a deprecation warning will be + # generated: + # + # - Chef::Resource::MyLwrp.new(...) + # - resource.is_a?(Chef::Resource::MyLwrp) + # - resource.kind_of?(Chef::Resource::MyLwrp) + # - case resource + # when Chef::Resource::MyLwrp + # end + # + resource_subclass = Class.new(resource_class) do + resource_name nil # we do not actually provide anything + def initialize(*args, &block) + Chef::Log.deprecation("Using an LWRP by its name (#{self.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 + def self.resource_name(*args) + if args.empty? + @resource_name ||= superclass.resource_name + else + super + end + end + self + end + eval("Chef::Resource::#{class_name} = resource_subclass") + # 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 + 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.") + # 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 self.deprecated_constants |