Sha256: 8fa0124d67eabb1b0d26f10821caaa500c325c430c7abc7702838e9e06c26530

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

require 'puppet/parser/ast/lambda'

Puppet::Parser::Functions::newfunction(
:collect,
:type => :rvalue,
:arity => 2,
:doc => <<-'ENDHEREDOC') do |args|
  Applies a parameterized block to each element in a sequence of entries from the first
  argument and returns an array with the result of each invocation of the parameterized block.

  This function takes two mandatory arguments: the first should be an Array or a Hash, and the second
  a parameterized block as produced by the puppet syntax:

    $a.collect |$x| { ... }

  When the first argument is an Array, the block is called with each entry in turn. When the first argument
  is a hash the entry is an array with `[key, value]`.

  *Examples*

    # Turns hash into array of values
    $a.collect |$x|{ $x[1] }

    # Turns hash into array of keys
    $a.collect |$x| { $x[0] }

  Since 3.2
  ENDHEREDOC

  receiver = args[0]
  pblock = args[1]

  raise ArgumentError, ("collect(): wrong argument type (#{pblock.class}; must be a parameterized block.") unless pblock.is_a? Puppet::Parser::AST::Lambda

  case receiver
  when Array
  when Hash
  else
    raise ArgumentError, ("collect(): wrong argument type (#{receiver.class}; must be an Array or a Hash.")
  end

  receiver.to_a.collect {|x| pblock.call(self, x) }
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
puppet-3.2.2 lib/puppet/parser/functions/collect.rb
puppet-3.2.1 lib/puppet/parser/functions/collect.rb
puppet-3.2.1.rc1 lib/puppet/parser/functions/collect.rb
puppet-3.2.0.rc2 lib/puppet/parser/functions/collect.rb
puppet-3.2.0.rc1 lib/puppet/parser/functions/collect.rb