From bf45150b190afa90a69c4317b0a89c4f8ebd20fa Mon Sep 17 00:00:00 2001 From: John Keiser Date: Sun, 19 Apr 2015 09:45:45 -0700 Subject: Make explicit "provides" override automatic --- lib/chef/dsl/resources.rb | 3 +++ lib/chef/mixin/provides.rb | 4 ++++ lib/chef/node_map.rb | 4 ++++ lib/chef/resource.rb | 24 ++++++++++++++++++++++++ 4 files changed, 35 insertions(+) diff --git a/lib/chef/dsl/resources.rb b/lib/chef/dsl/resources.rb index dfa0788879..482b14e3aa 100644 --- a/lib/chef/dsl/resources.rb +++ b/lib/chef/dsl/resources.rb @@ -14,6 +14,9 @@ class Chef end EOM end + def self.remove_resource_dsl(dsl_name) + remove_method dsl_name if method_defined?(dsl_name) + end end end end diff --git a/lib/chef/mixin/provides.rb b/lib/chef/mixin/provides.rb index d71097ca4b..cb643b9cfc 100644 --- a/lib/chef/mixin/provides.rb +++ b/lib/chef/mixin/provides.rb @@ -23,6 +23,10 @@ class Chef node_map.set(short_name, true, opts, &block) end + def does_not_provide(short_name) + node_map.delete(short_name) + end + # Check whether this resource provides the resource_name DSL for the given # node def provides?(node, resource_name) diff --git a/lib/chef/node_map.rb b/lib/chef/node_map.rb index 2ca6d9ba17..5befdf25ec 100644 --- a/lib/chef/node_map.rb +++ b/lib/chef/node_map.rb @@ -78,6 +78,10 @@ class Chef nil end + def delete(key) + @map.delete(key) + end + private # only allow valid filter options diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index ef0b59e4dc..280da8734c 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -978,14 +978,38 @@ class Chef super if subclass.dsl_name subclass.provides subclass.dsl_name.to_sym + subclass.instance_eval { @auto_provides = subclass.dsl_name.to_sym } end end def self.provides(name, *args, &block) + # If the user specifies provides, then we get rid of the auto-provided DSL + # and let them specify what they want + if @auto_provides + @auto_provides = auto_provides = nil + does_not_provide(auto_provides) + end + super + Chef::DSL::Resources.add_resource_dsl(name) end + def self.does_not_provide(name=nil) + name ||= dsl_name + if @auto_provides + @auto_provides = auto_provides = nil + does_not_provide(auto_provides) if name != auto_provides + end + + super + + # Get rid of the DSL if this was the only resource that used it + if !Chef::Resource.resource_matching_short_name(name) + Chef::DSL::Resources.remove_resource_dsl(name) + end + end + # Helper for #notifies def validate_resource_spec!(resource_spec) run_context.resource_collection.validate_lookup_spec!(resource_spec) -- cgit v1.2.1