Sha256: b0ddbbc12f7b781d96d9706a1e4db817520bf8959b9238bcdfd820123e949981

Contents?: true

Size: 1.26 KB

Versions: 26

Compression:

Stored size: 1.26 KB

Contents

# Call a [lambda](https://docs.puppet.com/puppet/latest/reference/lang_lambdas.html)
# (which should accept no arguments) if the argument given to the function is `undef`.
# Returns the result of calling the lambda if the argument is `undef`, otherwwise the
# given argument.
#
# The `lest` function is useful in a chain of `then` calls, or in general
# as a guard against `undef` values. The function can be used to call `fail`, or to
# return a default value.
#
# These two expressions are equivalent:
# ~~~puppet
# if $x == undef { do_things() }
# lest($x) || { do_things() }
# ~~
#
# @example Using `lest`
#
# ~~~ puppet
# $data = {a => [ b, c ] }
# notice $data.dig(a, b, c)
#  .then |$x| { $x * 2 }
#  .lest || { fail("no value for $data[a][b][c]" }
# ~~~
#
# Would fail the operation because $data[a][b][c] results in `undef`
#
# In contrast - this example:
#
# ~~~ puppet
# $data = {a => { b => { c => 10 } } }
# notice $data.dig(a, b, c)
#  .then |$x| { $x * 2 }
#  .lest || { fail("no value for $data[a][b][c]" }
# ~~~
#
# Would notice the value 20
#
# @since 4.5.0
#
Puppet::Functions.create_function(:lest) do
  dispatch :lest do
    param 'Any', :arg
    block_param 'Callable[0,0]', :block
  end

  def lest(arg)
    if arg.nil?
      yield()
    else
      arg
    end
  end
end

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
puppet-retrospec-1.5.0 vendor/gems/puppet-4.5.2/lib/puppet/functions/lest.rb
puppet-retrospec-1.4.1 vendor/gems/puppet-4.5.2/lib/puppet/functions/lest.rb
puppet-retrospec-1.4.0 vendor/gems/puppet-4.5.2/lib/puppet/functions/lest.rb
puppet-retrospec-1.3.2 vendor/gems/puppet-4.5.2/lib/puppet/functions/lest.rb
puppet-retrospec-1.3.1 vendor/gems/puppet-4.5.2/lib/puppet/functions/lest.rb
puppet-retrospec-1.3.0 vendor/gems/puppet-4.5.2/lib/puppet/functions/lest.rb
puppet-retrospec-1.2.1 vendor/gems/puppet-4.5.2/lib/puppet/functions/lest.rb
puppet-retrospec-1.2.0 vendor/gems/puppet-4.5.2/lib/puppet/functions/lest.rb
puppet-retrospec-1.1.0 vendor/gems/puppet-4.5.2/lib/puppet/functions/lest.rb
puppet-4.5.3 lib/puppet/functions/lest.rb
puppet-4.5.3-x86-mingw32 lib/puppet/functions/lest.rb
puppet-4.5.3-x64-mingw32 lib/puppet/functions/lest.rb
puppet-4.5.3-universal-darwin lib/puppet/functions/lest.rb
puppet-retrospec-1.0.0 vendor/gems/puppet-4.5.2/lib/puppet/functions/lest.rb
puppet-4.5.2 lib/puppet/functions/lest.rb
puppet-4.5.2-x86-mingw32 lib/puppet/functions/lest.rb
puppet-4.5.2-x64-mingw32 lib/puppet/functions/lest.rb
puppet-4.5.2-universal-darwin lib/puppet/functions/lest.rb
puppet-4.5.1 lib/puppet/functions/lest.rb
puppet-4.5.1-x86-mingw32 lib/puppet/functions/lest.rb