diff options
author | Seth Vargo <sethvargo@gmail.com> | 2014-06-25 12:42:32 -0700 |
---|---|---|
committer | Seth Vargo <sethvargo@gmail.com> | 2014-07-11 14:50:35 -0400 |
commit | 8ebbcb0f41c731fa60170ad48727d4941fd39545 (patch) | |
tree | 02306b6af8a0b562a216fa7d73a8c02dfc503821 /lib/chef/resource/lwrp_base.rb | |
parent | fbbc45178baf6c933380a35dd44278dbc3e4b02f (diff) | |
download | chef-8ebbcb0f41c731fa60170ad48727d4941fd39545.tar.gz |
Delegate DSL method values to their superclass
When inheriting from +Resource::LWRPBase+, the +default_action+ and +actions+
attributes are set as class instance variables. In Ruby's object model, class
instance variables are **not** shared with children. So subclassing a resource
that extended from +Resource::LWRPBase+ would result in some unexplainable
side-effects:
class Chef::Resource::Parent < Chef::Resource::LWRPBase
actions :execute
default_action :execute
end
class Child::Resource::Child < Chef::Resource::Parent
# In here, +actions+ and +default_action+ are **not** inherited so they
# are +nil+!
end
This commit tells resources that inherit from LWRPBase to check their
superclasses for values if they do not have one on their own.
This commit also deprecate +valid_actions+ in favor of just using +actions+ in
the LWRP DSL.
Diffstat (limited to 'lib/chef/resource/lwrp_base.rb')
-rw-r--r-- | lib/chef/resource/lwrp_base.rb | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/lib/chef/resource/lwrp_base.rb b/lib/chef/resource/lwrp_base.rb index b7bb7d08fd..5e5b05e3ce 100644 --- a/lib/chef/resource/lwrp_base.rb +++ b/lib/chef/resource/lwrp_base.rb @@ -92,19 +92,32 @@ class Chef # Sets the default action def self.default_action(action_name=NULL_ARG) unless action_name.equal?(NULL_ARG) - valid_actions.push(action_name) - @default_action = action_name + action = action_name.to_sym + actions.push(action) unless actions.include?(action) + @default_action = action end - @default_action + + @default_action ||= from_superclass(:default_action) end # Adds +action_names+ to the list of valid actions for this resource. def self.actions(*action_names) - valid_actions.push(*action_names) + if action_names.empty? + @actions || from_superclass(:actions, []).dup + else + # BC-compat way for checking if actions have already been defined + if @actions + @actions.push(*action_names) + else + @actions = action_names + end + end end - def self.valid_actions - @valid_actions ||= [] + # @deprecated + def self.valid_actions(*args) + Chef::Log.warn("`valid_actions' is deprecated, please use actions `instead'!") + actions(*args) end # Set the run context on the class. Used to provide access to the node @@ -125,6 +138,17 @@ class Chef DelayedEvaluator.new(&block) end + private + + # Get the value from the superclass, if it responds, otherwise return + # +nil+. Since class instance variables are **not** inherited upon + # subclassing, this is a required check to ensure Chef pulls the + # +default_action+ and other DSL-y methods when extending LWRP::Base. + def self.from_superclass(m, default = nil) + return default if superclass == Chef::Resource::LWRPBase + superclass.respond_to?(m) ? superclass.send(m) : default + end + # Default initializer. Sets the default action and allowed actions. def initialize(name, run_context=nil) super(name, run_context) @@ -137,7 +161,7 @@ class Chef @resource_name = self.class.resource_name.to_sym @action = self.class.default_action - allowed_actions.push(self.class.valid_actions).flatten! + allowed_actions.push(self.class.actions).flatten! end end |