Sha256: 7660df2b47c008f4087525c081352d4ee6f345fad8cd6e1e9291b57d2b9b062a

Contents?: true

Size: 1.29 KB

Versions: 16

Compression:

Stored size: 1.29 KB

Contents

# Calls a [lambda](https://docs.puppet.com/puppet/latest/reference/lang_lambdas.html)
# without arguments if the value given to `lest` is `undef`.
# Returns the result of calling the lambda if the argument is `undef`, otherwise 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 the `lest` function
#
# ```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`
# (there is no `b` key in `a`).
#
# 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

16 entries across 16 versions & 2 rubygems

Version Path
bolt-0.20.3 vendored/puppet/lib/puppet/functions/lest.rb
bolt-0.20.2 vendored/puppet/lib/puppet/functions/lest.rb
bolt-0.19.1 vendored/puppet/lib/puppet/functions/lest.rb
puppet-5.5.1 lib/puppet/functions/lest.rb
puppet-5.5.1-x86-mingw32 lib/puppet/functions/lest.rb
puppet-5.5.1-x64-mingw32 lib/puppet/functions/lest.rb
puppet-5.5.1-universal-darwin lib/puppet/functions/lest.rb
bolt-0.19.0 vendored/puppet/lib/puppet/functions/lest.rb
bolt-0.18.2 vendored/puppet/lib/puppet/functions/lest.rb
bolt-0.18.1 vendored/puppet/lib/puppet/functions/lest.rb
bolt-0.18.0 vendored/puppet/lib/puppet/functions/lest.rb
puppet-5.5.0 lib/puppet/functions/lest.rb
puppet-5.5.0-x86-mingw32 lib/puppet/functions/lest.rb
puppet-5.5.0-x64-mingw32 lib/puppet/functions/lest.rb
puppet-5.5.0-universal-darwin lib/puppet/functions/lest.rb
bolt-0.17.2 vendored/puppet/lib/puppet/functions/lest.rb