Class: RuboCop::Cop::Chef::Modernize::DatabagHelpers

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/chef/modernize/databag_helpers.rb

Overview

Use the ‘data_bag_item` helper instead of `Chef::DataBagItem.load` or `Chef::EncryptedDataBagItem.load`.

Examples:


### incorrect
plain_text_data = Chef::DataBagItem.load('foo', 'bar')
encrypted_data = Chef::EncryptedDataBagItem.load('foo2', 'bar2')

### correct
plain_text_data = data_bag_item('foo', 'bar')
encrypted_data = data_bag_item('foo2', 'bar2')

Constant Summary collapse

MSG =
'Use the `data_bag_item` helper instead of `Chef::DataBagItem.load` or `Chef::EncryptedDataBagItem.load`.'
RESTRICT_ON_SEND =
[:load].freeze

Instance Method Summary collapse

Methods inherited from Base

#target_chef_version

Instance Method Details

#on_send(node) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/chef/modernize/databag_helpers.rb', line 47

def on_send(node)
  data_bag_class_load?(node) do
    add_offense(node, severity: :refactor) do |corrector|
      corrector.replace(node,
         node.source.gsub(/Chef::(EncryptedDataBagItem|DataBagItem).load/, 'data_bag_item'))
    end
  end
end