module Poise::Helpers::LazyDefault
Resource
mixin to allow lazyily-evaluated defaults in resource attributes. This is designed to be used with {LWRPPolyfill} or a similar attributes method.
@since 1.0.0 @example
class MyResource < Chef::Resource include Poise::Helpers::LWRPPolyfill include Poise::Helpers::LazyDefault attribute(:path, default: lazy { name + '_temp' }) end
Public Class Methods
needs_polyfill?()
click to toggle source
Check if this version of Chef already supports lazy defaults. This is true for Chef 12.5+.
@since 2.0.3 @api private @return [Boolean]
# File lib/poise/helpers/lazy_default.rb, line 40 def self.needs_polyfill? @needs_polyfill ||= Gem::Requirement.new('< 12.5.pre').satisfied_by?(Gem::Version.new(Chef::VERSION)) end
Public Instance Methods
set_or_return(symbol, arg, validation)
click to toggle source
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.
Calls superclass method
# File lib/poise/helpers/lazy_default.rb, line 47 def set_or_return(symbol, arg, validation) if LazyDefault.needs_polyfill? && validation && validation[:default].is_a?(Chef::DelayedEvaluator) validation = validation.dup if (arg.nil? || arg == Poise::NOT_PASSED) && (!instance_variable_defined?(:"@#{symbol}") || instance_variable_get(:"@#{symbol}").nil?) validation[:default] = instance_eval(&validation[:default]) else # Clear the default. validation.delete(:default) end end super(symbol, arg, validation) end