Sha256: b0bccac6c77701b4624caed2eb1f1f4de5cbb2181eceb810f78dffc30062ec8d

Contents?: true

Size: 1000 Bytes

Versions: 16

Compression:

Stored size: 1000 Bytes

Contents

# The `convert_to(value, type)` is a convenience function does the same as `new(type, value)`.
# The difference in the argument ordering allows it to be used in chained style for
# improved readability "left to right".
#
# When the function is given a lambda, it is called with the converted value, and the function
# returns what the lambda returns, otherwise the converted value.
#
# @example 'convert_to' instead of 'new'
#
# ~~~ puppet
#   # using new operator - that is "calling the type" with operator ()
#   Hash(Array("abc").map |$i,$v| { [$i, $v] })
#
#   # using 'convert_to'
#   "abc".convert_to(Array).map |$i,$v| { [$i, $v] }.convert_to(Hash)
#
# ~~~
#
# @since 5.4.0
#
Puppet::Functions.create_function(:convert_to) do
  dispatch :convert_to do
    param 'Any', :value
    param 'Type', :type
    optional_block_param 'Callable[1,1]', :block
  end

  def convert_to(value, type, &block)
    result = call_function('new', type, value)
    block_given? ? yield(result) : result
  end
end

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
bolt-0.17.1 vendored/puppet/lib/puppet/functions/convert_to.rb
bolt-0.17.0 vendored/puppet/lib/puppet/functions/convert_to.rb
bolt-0.16.4 vendored/puppet/lib/puppet/functions/convert_to.rb
bolt-0.16.3 vendored/puppet/lib/puppet/functions/convert_to.rb
bolt-0.16.2 vendored/puppet/lib/puppet/functions/convert_to.rb
puppet-5.4.0 lib/puppet/functions/convert_to.rb
puppet-5.4.0-x86-mingw32 lib/puppet/functions/convert_to.rb
puppet-5.4.0-x64-mingw32 lib/puppet/functions/convert_to.rb
puppet-5.4.0-universal-darwin lib/puppet/functions/convert_to.rb
bolt-0.16.1 vendored/puppet/lib/puppet/functions/convert_to.rb
bolt-0.16.0 vendored/puppet/lib/puppet/functions/convert_to.rb
bolt-0.15.0 vendored/puppet/lib/puppet/functions/convert_to.rb
bolt-0.14.0 vendored/puppet/lib/puppet/functions/convert_to.rb
bolt-0.13.0 vendored/puppet/lib/puppet/functions/convert_to.rb
bolt-0.12.0 vendored/puppet/lib/puppet/functions/convert_to.rb
bolt-0.11.0 vendored/puppet/lib/puppet/functions/convert_to.rb