diff options
author | John Keiser <john@johnkeiser.com> | 2015-07-10 12:15:07 -0600 |
---|---|---|
committer | John Keiser <john@johnkeiser.com> | 2015-09-22 11:48:17 -0700 |
commit | 064e0a6b90d1e9a1e00e0c9ef4d18eb1ecc33a3e (patch) | |
tree | b1c654fce7526bc352dc494e7ffa5943fde017c5 | |
parent | 871c858dbcec3dabec64474b68feb16372556377 (diff) | |
download | chef-jk/declare_action_class.tar.gz |
Rename action_provider_class to less-wordy action_classjk/declare_action_class
-rw-r--r-- | lib/chef/exceptions.rb | 2 | ||||
-rw-r--r-- | lib/chef/resource.rb | 45 | ||||
-rw-r--r-- | lib/chef/resource/action_class.rb (renamed from lib/chef/resource/action_provider.rb) | 16 | ||||
-rw-r--r-- | spec/integration/recipes/resource_action_spec.rb | 26 | ||||
-rw-r--r-- | spec/integration/recipes/resource_converge_if_changed_spec.rb | 2 |
5 files changed, 53 insertions, 38 deletions
diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb index 7862d9c160..6e7ff2e24a 100644 --- a/lib/chef/exceptions.rb +++ b/lib/chef/exceptions.rb @@ -105,7 +105,7 @@ class Chef class VerificationNotFound < RuntimeError; end class InvalidEventType < ArgumentError; end class MultipleIdentityError < RuntimeError; end - # Used in Resource::ActionProvider#load_current_resource to denote that + # Used in Resource::ActionClass#load_current_resource to denote that # the resource doesn't actually exist (for example, the file does not exist) class CurrentValueDoesNotExist < RuntimeError; end diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb index 5c230ad2f4..6c2f91ed8b 100644 --- a/lib/chef/resource.rb +++ b/lib/chef/resource.rb @@ -29,7 +29,7 @@ require 'chef/mixin/convert_to_class_name' require 'chef/guard_interpreter/resource_guard_interpreter' require 'chef/resource/conditional' require 'chef/resource/conditional_action_not_nothing' -require 'chef/resource/action_provider' +require 'chef/resource/action_class' require 'chef/resource_collection' require 'chef/node_map' require 'chef/node' @@ -688,7 +688,7 @@ class Chef # The provider class for this resource. # # If `action :x do ... end` has been declared on this resource or its - # superclasses, this will return the `action_provider_class`. + # superclasses, this will return the `action_class`. # # If this is not set, `provider_for_action` will dynamically determine the # provider. @@ -699,7 +699,7 @@ class Chef # # @return The provider class for this resource. # - # @see Chef::Resource.action_provider_class + # @see Chef::Resource.action_class # def provider(arg=nil) klass = if arg.kind_of?(String) || arg.kind_of?(Symbol) @@ -708,7 +708,7 @@ class Chef arg end set_or_return(:provider, klass, kind_of: [ Class ]) || - self.class.action_provider_class + self.class.action_class end def provider=(arg) provider(arg) @@ -1372,7 +1372,8 @@ class Chef # def self.action(action, &recipe_block) action = action.to_sym - new_action_provider_class.action(action, &recipe_block) + declare_action_class + action_class.action(action, &recipe_block) self.allowed_actions += [ action ] default_action action if Array(default_action) == [:nothing] end @@ -1416,7 +1417,7 @@ class Chef end # - # The action provider class is an automatic `Provider` created to handle + # The action class is an automatic `Provider` created to handle # actions declared by `action :x do ... end`. # # This class will be returned by `resource.provider` if `resource.provider` @@ -1425,40 +1426,38 @@ class Chef # # If the user has not declared actions on this class or its superclasses # using `action :x do ... end`, then there is no need for this class and - # `action_provider_class` will be `nil`. + # `action_class` will be `nil`. # # @api private # - def self.action_provider_class - @action_provider_class || + def self.action_class + @action_class || # If the superclass needed one, then we need one as well. - if superclass.respond_to?(:action_provider_class) && superclass.action_provider_class - new_action_provider_class + if superclass.respond_to?(:action_class) && superclass.action_class + declare_action_class end end # - # Ensure the action provider class actually gets created. This is called + # Ensure the action class actually gets created. This is called # when the user does `action :x do ... end`. # + # If a block is passed, it is run inside the action_class. + # # @api private - def self.new_action_provider_class - return @action_provider_class if @action_provider_class + def self.declare_action_class + return @action_class if @action_class - if superclass.respond_to?(:action_provider_class) - base_provider = superclass.action_provider_class + if superclass.respond_to?(:action_class) + base_provider = superclass.action_class end base_provider ||= Chef::Provider resource_class = self - @action_provider_class = Class.new(base_provider) do - include ActionProvider - define_singleton_method(:to_s) { "#{resource_class} action provider" } - def self.inspect - to_s - end + @action_class = Class.new(base_provider) do + include ActionClass + self.resource_class = resource_class end - @action_provider_class end # diff --git a/lib/chef/resource/action_provider.rb b/lib/chef/resource/action_class.rb index d71b54ef4d..12211418e9 100644 --- a/lib/chef/resource/action_provider.rb +++ b/lib/chef/resource/action_class.rb @@ -20,7 +20,7 @@ require 'chef/exceptions' class Chef class Resource - module ActionProvider + module ActionClass # # If load_current_value! is defined on the resource, use that. # @@ -63,6 +63,20 @@ class Chef end module ClassMethods + # + # The Chef::Resource class this ActionClass was declared against. + # + # @return [Class] The Chef::Resource class this ActionClass was declared against. + # + attr_accessor :resource_class + + def to_s + "#{resource_class} action provider" + end + + def inspect + to_s + end end end end diff --git a/spec/integration/recipes/resource_action_spec.rb b/spec/integration/recipes/resource_action_spec.rb index 53611c144f..1c84b986cc 100644 --- a/spec/integration/recipes/resource_action_spec.rb +++ b/spec/integration/recipes/resource_action_spec.rb @@ -4,7 +4,7 @@ describe "Resource.action" do include IntegrationSupport shared_context "ActionJackson" do - it "The default action is the first declared action" do + it "the default action is the first declared action" do converge <<-EOM, __FILE__, __LINE__+1 #{resource_dsl} 'hi' do foo 'foo!' @@ -14,7 +14,7 @@ describe "Resource.action" do expect(ActionJackson.succeeded).to eq true end - it "The action can access recipe DSL" do + it "the action can access recipe DSL" do converge <<-EOM, __FILE__, __LINE__+1 #{resource_dsl} 'hi' do foo 'foo!' @@ -25,7 +25,7 @@ describe "Resource.action" do expect(ActionJackson.succeeded).to eq true end - it "The action can access attributes" do + it "the action can access attributes" do converge <<-EOM, __FILE__, __LINE__+1 #{resource_dsl} 'hi' do foo 'foo!' @@ -36,7 +36,7 @@ describe "Resource.action" do expect(ActionJackson.succeeded).to eq 'foo!' end - it "The action can access public methods" do + it "the action can access public methods" do converge <<-EOM, __FILE__, __LINE__+1 #{resource_dsl} 'hi' do foo 'foo!' @@ -47,7 +47,7 @@ describe "Resource.action" do expect(ActionJackson.succeeded).to eq 'foo_public!' end - it "The action can access protected methods" do + it "the action can access protected methods" do converge <<-EOM, __FILE__, __LINE__+1 #{resource_dsl} 'hi' do foo 'foo!' @@ -58,7 +58,7 @@ describe "Resource.action" do expect(ActionJackson.succeeded).to eq 'foo_protected!' end - it "The action cannot access private methods" do + it "the action cannot access private methods" do expect { converge(<<-EOM, __FILE__, __LINE__+1) #{resource_dsl} 'hi' do @@ -70,7 +70,7 @@ describe "Resource.action" do expect(ActionJackson.ran_action).to eq :access_private_method end - it "The action cannot access resource instance variables" do + it "the action cannot access resource instance variables" do converge <<-EOM, __FILE__, __LINE__+1 #{resource_dsl} 'hi' do foo 'foo!' @@ -81,7 +81,7 @@ describe "Resource.action" do expect(ActionJackson.succeeded).to be_nil end - it "The action does not compile until the prior resource has converged" do + it "the action does not compile until the prior resource has converged" do converge <<-EOM, __FILE__, __LINE__+1 ruby_block 'wow' do block do @@ -98,7 +98,7 @@ describe "Resource.action" do expect(ActionJackson.succeeded).to eq 'ruby_block_converged!' end - it "The action's resources converge before the next resource converges" do + it "the action's resources converge before the next resource converges" do converge <<-EOM, __FILE__, __LINE__+1 #{resource_dsl} 'hi' do foo 'foo!' @@ -214,7 +214,7 @@ describe "Resource.action" do end end - context "And 'action_jackalope' inheriting from ActionJackson with an extra attribute and action" do + context "And 'action_jackalope' inheriting from ActionJackson with an extra attribute, action and custom method" do before(:context) { class ActionJackalope < ActionJackson use_automatic_resource_name @@ -228,6 +228,7 @@ describe "Resource.action" do @bar end class <<self + attr_accessor :load_current_resource_ran attr_accessor :jackalope_ran end action :access_jackalope do @@ -243,6 +244,7 @@ describe "Resource.action" do } before do ActionJackalope.jackalope_ran = nil + ActionJackalope.load_current_resource_ran = nil end context "action_jackson still behaves the same" do @@ -251,7 +253,7 @@ describe "Resource.action" do end end - it "The default action remains the same even though new actions were specified first" do + it "the default action remains the same even though new actions were specified first" do converge { action_jackalope 'hi' do foo 'foo!' @@ -320,7 +322,7 @@ describe "Resource.action" do end end } - it "The default action is :nothing" do + it "the default action is :nothing" do converge { no_action_jackson 'hi' do foo 'foo!' diff --git a/spec/integration/recipes/resource_converge_if_changed_spec.rb b/spec/integration/recipes/resource_converge_if_changed_spec.rb index d00252a717..82d38a8faf 100644 --- a/spec/integration/recipes/resource_converge_if_changed_spec.rb +++ b/spec/integration/recipes/resource_converge_if_changed_spec.rb @@ -1,6 +1,6 @@ require 'support/shared/integration/integration_helper' -describe "Resource::ActionProvider#converge_if_changed" do +describe "Resource::ActionClass#converge_if_changed" do include IntegrationSupport module Namer |