summaryrefslogtreecommitdiff
path: root/lib/chef/resource/lwrp_base.rb
diff options
context:
space:
mode:
authorSeth Vargo <sethvargo@gmail.com>2014-06-26 13:44:05 -0700
committerSeth Vargo <sethvargo@gmail.com>2014-06-30 12:08:36 -0400
commit330b407750403da21cd49b3845c384f37b872dbc (patch)
tree6d0ab837403b7922695bbe73e0419898123b2acc /lib/chef/resource/lwrp_base.rb
parent724345741c573a51c4bb4b68df41d2a070eb1455 (diff)
downloadchef-330b407750403da21cd49b3845c384f37b872dbc.tar.gz
Allow users to set the +resource_name+ in an LWRP programatically
Previously this was marked as an internal API, but I am making it public. We are reliably using this API in our LHWRPs in Release Engineering and it is truly a delightful experience. This deprecates using +resource_name = :foo+ in favor of the more "Chef-like" +resource_name(:foo)+.
Diffstat (limited to 'lib/chef/resource/lwrp_base.rb')
-rw-r--r--lib/chef/resource/lwrp_base.rb23
1 files changed, 16 insertions, 7 deletions
diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb
index e91516e6f6..33e76f6f66 100644
--- a/lib/chef/resource/lwrp_base.rb
+++ b/lib/chef/resource/lwrp_base.rb
@@ -59,15 +59,17 @@ class Chef
resource_class
end
- # Set the resource snake_case name. Should only be called via
- # build_from_file.
- def self.resource_name=(resource_name)
- @resource_name = resource_name
+ # Set the resource name for this LWRP
+ def self.resource_name(arg = NULL_ARG)
+ if arg.equal?(NULL_ARG)
+ @resource_name
+ else
+ @resource_name = arg
+ end
end
- # Returns the resource snake_case name
- def self.resource_name
- @resource_name
+ class << self
+ alias_method :resource_name=, :resource_name
end
# Define an attribute on this resource, including optional validation
@@ -122,6 +124,13 @@ class Chef
# Default initializer. Sets the default action and allowed actions.
def initialize(name, run_context=nil)
super(name, run_context)
+
+ # Raise an exception if the resource_name was not defined
+ if self.class.resource_name.nil?
+ raise Chef::Exceptions::InvalidResourceSpecification,
+ "You must specify `resource_name'!"
+ end
+
@resource_name = self.class.resource_name.to_sym
@action = self.class.default_action
allowed_actions.push(self.class.valid_actions).flatten!