summaryrefslogtreecommitdiff
path: root/lib/chef/resource.rb
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-05-27 16:04:47 -0700
committerJohn Keiser <john@johnkeiser.com>2015-06-01 08:02:03 -0700
commita3aba411ccdf1a0a5f236ed3ce2a678a1892e29b (patch)
treec6c5a4da0143b4928e076f57941d72a9c2baa1b0 /lib/chef/resource.rb
parent9f8d3fbb943206c27364593b49b875f7254b77be (diff)
downloadchef-a3aba411ccdf1a0a5f236ed3ce2a678a1892e29b.tar.gz
Move resource_name up to Resource, and figure out its value automatically
Diffstat (limited to 'lib/chef/resource.rb')
-rw-r--r--lib/chef/resource.rb93
1 files changed, 59 insertions, 34 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 18759b29f7..6c82b5668c 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -57,6 +57,8 @@ class Chef
include Chef::Mixin::ShellOut
include Chef::Mixin::PowershellOut
+ NULL_ARG = Object.new
+
#
# The node the current Chef run is using.
#
@@ -88,7 +90,6 @@ class Chef
run_context.resource_collection.find(*args)
end
-
#
# Resource User Interface (for users)
#
@@ -606,7 +607,7 @@ class Chef
#
def to_s
- "#{@resource_name}[#{@name}]"
+ "#{resource_name}[#{name}]"
end
def to_text
@@ -825,23 +826,15 @@ class Chef
end
#
- # The DSL name of this resource (e.g. `package` or `yum_package`)
- #
- # @return [String] The DSL name of this resource.
- def self.dsl_name
- Chef::Log.deprecation "Resource.dsl_name is deprecated and will be removed in Chef 11. Use resource.resource_name instead."
- if name
- name = self.name.split('::')[-1]
- convert_to_snake_case(name)
- end
- end
-
+ # The display name of this resource type, for printing purposes.
#
- # The name of this resource (e.g. `file`)
+ # Will be used to print out the resource in messages, e.g. resource_name[name]
#
- # @return [String] The name of this resource.
+ # @return [Symbol] The name of this resource type (e.g. `:execute`).
#
- attr_reader :resource_name
+ def resource_name
+ @resource_name || self.class.resource_name
+ end
#
# Sets a list of capabilities of the real resource. For example, `:remount`
@@ -873,23 +866,53 @@ class Chef
nil
end
- #
- # The module where Chef should look for providers for this resource.
- # The provider for `MyResource` will be looked up using
- # `provider_base::MyResource`. Defaults to `Chef::Provider`.
- #
- # @param arg [Module] The module containing providers for this resource
- # @return [Module] The module containing providers for this resource
- #
- # @example
- # class MyResource < Chef::Resource
- # provider_base Chef::Provider::Deploy
- # # ...other stuff
- # end
- #
- def self.provider_base(arg=nil)
- @provider_base ||= arg
- @provider_base ||= Chef::Provider
+ # Provider lookup and naming
+ class<<self
+ #
+ # The DSL name of this resource (e.g. `package` or `yum_package`)
+ #
+ # @return [String] The DSL name of this resource.
+ #
+ # @deprecated Use resource_name instead.
+ #
+ def dsl_name
+ Chef::Log.deprecation "Resource.dsl_name is deprecated and will be removed in Chef 11. Use resource_name instead."
+ if name
+ name = self.name.split('::')[-1]
+ convert_to_snake_case(name)
+ end
+ end
+
+ #
+ # The display name of this resource type, for printing purposes.
+ #
+ # @return [Symbol] The name of this resource type (e.g. `:execute`).
+ #
+ attr_accessor :resource_name
+ def resource_name(value=NULL_ARG)
+ if value != NULL_ARG
+ self.resource_name = value.to_sym
+ end
+ @resource_name
+ end
+
+ #
+ # The module where Chef should look for providers for this resource.
+ # The provider for `MyResource` will be looked up using
+ # `provider_base::MyResource`. Defaults to `Chef::Provider`.
+ #
+ # @param arg [Module] The module containing providers for this resource
+ # @return [Module] The module containing providers for this resource
+ #
+ # @example
+ # class MyResource < Chef::Resource
+ # provider_base Chef::Provider::Deploy
+ # # ...other stuff
+ # end
+ #
+ def provider_base(arg=nil)
+ @provider_base ||= arg || Chef::Provider
+ end
end
@@ -984,6 +1007,8 @@ class Chef
def self.provides(name, *args, &block)
result = super
+ # The first time `provides` is called on the class, it is used for resource_name
+ self.resource_name ||= name
Chef::DSL::Resources.add_resource_dsl(name)
result
end
@@ -1162,7 +1187,7 @@ class Chef
def const_missing(class_name)
if deprecated_constants[class_name.to_sym]
- Chef::Log.deprecation("Using an LWRP by its name (#{class_name}) directly is no longer supported in Chef 12 and will be removed. Use Chef::Resource.resource_for_node(node, name) instead.")
+ 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}"