diff options
author | John Keiser <john@johnkeiser.com> | 2015-05-12 20:37:52 -0700 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-06-23 14:42:27 -0700 |
commit | d505a314fee15e5b5de411e2c27a7cdbd2a0e48b (patch) | |
tree | 6206976c6f366297ac1b7179fe0362ad29d7bf3e | |
parent | b927d174e192fb67264f432be2220fc67a50aacf (diff) | |
download | chef-d505a314fee15e5b5de411e2c27a7cdbd2a0e48b.tar.gz |
Define the action() method
-rw-r--r-- | lib/chef/resource.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index ed0dbb50a7..4ec0a03f4f 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -61,6 +61,42 @@ class Chef NULL_ARG = Object.new # + # Define an action on this resource. + # + # The action is defined as a *recipe* block that will be compiled and then + # converged when the action is taken (when Resource is converged). The recipe + # has access to the resource's attributes and methods, as well as the Chef + # recipe DSL. + # + # Resources in the action recipe may notify and subscribe to other resources + # within the action recipe, but cannot notify or subscribe to resources + # in the main Chef run. + # + # Resource actions are *inheritable*: if resource A defines `action :create` + # and B is a subclass of A, B gets all of A's actions. Additionally, + # resource B can define `action :create` and call `super()` to invoke A's + # action code. + # + # @param name [Symbol] The action name to define. + # @param recipe_block The recipe to run when the action is taken. This block + # takes no parameters, and will be evaluated in a new context containing: + # + # - The resource's public and protected methods (including attributes) + # - The Chef Recipe DSL (file, etc.) + # - super() referring to the parent version of the action (if any) + # + # @return The Action class implementing the action + # + def self.action(action, &recipe_block) + action = action.to_sym + self.action_classes[action] ||= Class.new(ActionRecipe) do + resource_class self + action action + recipe_block recipe_block + end + end + + # # The node the current Chef run is using. # # Corresponds to `run_context.node`. |