Class: RuboCop::Cop::Chef::Correctness::LazyEvalNodeAttributeDefaults

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

Overview

When setting a node attribute as the default value for a custom resource property, wrap the node attribute in ‘lazy {}` so that its value is available when the resource executes.

Examples:


### incorrect
property :Something, String, default: node['hostname']

### correct
property :Something, String, default: lazy { node['hostname'] }

Constant Summary collapse

MSG =
'When setting a node attribute as the default value for a custom resource property, wrap the node attribute in `lazy {}` so that its value is available when the resource executes.'

Instance Method Summary collapse

Methods included from RuboCop::Chef::CookbookHelpers

#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_send(node) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/rubocop/cop/chef/correctness/lazy_eval_node_attribute_defaults.rb', line 42

def on_send(node)
  non_lazy_node_attribute_default?(node) do |default|
    add_offense(default, severity: :refactor) do |corrector|
      corrector.replace(default, "lazy { #{default.source} }")
    end
  end
end