Module: Poise::Helpers::LazyDefault

Included in:
TemplateContent, Resource
Defined in:
lib/poise/helpers/lazy_default.rb

Overview

Resource mixin to allow lazyily-evaluated defaults in resource attributes. This is designed to be used with LWRPPolyfill or a similar #attributes method.

Examples:

class MyResource < Chef::Resource
  include Poise::Helpers::LWRPPolyfill
  include Poise::Helpers::LazyDefault
  attribute(:path, default: lazy { name + '_temp' })
end

Since:

  • 1.0.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.lazy(&block) ⇒ Chef::DelayedEvaluator

Create a lazyily-evaluated block.

Since:

  • 1.0.0



49
50
51
# File 'lib/poise/helpers/lazy_default.rb', line 49

def lazy(&block)
  Chef::DelayedEvaluator.new(&block)
end

Instance Method Details

#set_or_return(symbol, arg, validation) ⇒ Object

Override the default set_or_return to support lazy evaluation of the default value. This only actually matters when it is called from a class level context via #attributes.

Since:

  • 1.0.0



35
36
37
38
39
40
41
# File 'lib/poise/helpers/lazy_default.rb', line 35

def set_or_return(symbol, arg, validation)
  if validation && validation[:default].is_a?(Chef::DelayedEvaluator)
    validation = validation.dup
    validation[:default] = instance_eval(&validation[:default])
  end
  super(symbol, arg, validation)
end