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:


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

# good
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
55
56
57
# 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|
      # replace only the receiver and the .load selector, so that the arguments are left
      # exactly as written. rewriting node.source would also hit any argument that
      # happened to contain the class name, and would leave the leading :: of a scope
      # resolved ::Chef::DataBagItem.load behind
      corrector.replace(node.receiver.source_range.join(node.loc.selector), 'data_bag_item')
    end
  end
end