Class: RuboCop::Cop::Chef::Deprecations::RubyBlockCreateAction

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RuboCop::Chef::CookbookHelpers
Defined in:
lib/rubocop/cop/chef/deprecation/ruby_block_create_action.rb

Overview

Use the :run action in the ruby_block resource instead of the deprecated :create action

Examples:


# bad
ruby_block 'my special ruby block' do
  block do
    puts 'running'
  end
  action :create
end

# bad
template '/etc/foo.conf' do
  notifies :create, 'ruby_block[my special ruby block]', :immediately
end

# good
ruby_block 'my special ruby block' do
  block do
    puts 'running'
  end
  action :run
end

# good
template '/etc/foo.conf' do
  notifies :run, 'ruby_block[my special ruby block]', :immediately
end

Constant Summary collapse

MSG =
'Use the :run action in the ruby_block resource instead of the deprecated :create action'
RESTRICT_ON_SEND =
[:notifies, :subscribes].freeze

Instance Method Summary collapse

Methods included from RuboCop::Chef::CookbookHelpers

#each_action_symbol, #match_property_in_resource?, #match_resource_type?, #method_arg_ast_to_string, #resource_block_name_if_string

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_block(node) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/rubocop/cop/chef/deprecation/ruby_block_create_action.rb', line 63

def on_block(node)
  match_property_in_resource?(:ruby_block, 'action', node) do |ruby_action|
    each_action_symbol(ruby_action) do |action, prefix|
      next unless action.value == :create
      add_offense(action, severity: :warning) do |corrector|
        corrector.replace(action, "#{prefix}run")
      end
    end
  end
end

#on_send(node) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/rubocop/cop/chef/deprecation/ruby_block_create_action.rb', line 74

def on_send(node)
  notification?(node) do |action, notified_resource|
    next unless ruby_block?(notified_resource)

    add_offense(action, severity: :warning) do |corrector|
      corrector.replace(action, ':run')
    end
  end
end