diff --git a/lib/puppet/functions/with.rb b/lib/puppet/functions/with.rb new file mode 100644 index 000000000..1b2b1e0e4 --- /dev/null +++ b/lib/puppet/functions/with.rb @@ -0,0 +1,15 @@ +# Call a lambda with the given arguments. Since the parameters of the lambda +# are local to the lambda's scope, this can be used to create private sections +# of logic in a class so that the variables are not visible outside of the +# class. +Puppet::Functions.create_function(:with) do + dispatch :with do + param 'Object', 'arg' + arg_count(0, :default) + required_block_param + end + + def with(*args) + args[-1].call({}, *args[0..-2]) + end +end diff --git a/spec/unit/functions/with_spec.rb b/spec/unit/functions/with_spec.rb new file mode 100644 index 000000000..d5d6989e4 --- /dev/null +++ b/spec/unit/functions/with_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +require 'puppet_spec/compiler' +require 'matchers/resource' + +describe 'the with function' do + include PuppetSpec::Compiler + include Matchers::Resource + + before :each do + Puppet[:parser] = 'future' + end + + it 'calls a lambda passing no arguments' do + expect(compile_to_catalog("with() || { notify { testing: } }")).to have_resource('Notify[testing]') + end + + it 'calls a lambda passing a single argument' do + expect(compile_to_catalog('with(1) |$x| { notify { "testing$x": } }')).to have_resource('Notify[testing1]') + end + + it 'calls a lambda passing more than one argument' do + expect(compile_to_catalog('with(1, 2) |*$x| { notify { "testing${x[0]}, ${x[1]}": } }')).to have_resource('Notify[testing1, 2]') + end + + it 'errors when not given enough arguments for the lambda' do + expect do + compile_to_catalog('with(1) |$x, $y| { }') + end.to raise_error(/Too few arguments/) + end +end