Skip to content

Commit 6c104e5

Browse files
author
Jeff McCune
committed
Merge branch 'lifton-feature/2.x/join_keys_to_values_function' into 2.x
* lifton-feature/2.x/join_keys_to_values_function: Add join_keys_to_values function
2 parents 6c36b49 + ee0f2b3 commit 6c104e5

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
# join.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:join_keys_to_values, :type => :rvalue, :doc => <<-EOS
7+
This function joins each key of a hash to that key's corresponding value with a
8+
separator. Keys and values are cast to strings. The return value is an array in
9+
which each element is one joined key/value pair.
10+
11+
*Examples:*
12+
13+
join_keys_to_values({'a'=>1,'b'=>2}, " is ")
14+
15+
Would result in: ["a is 1","b is 2"]
16+
EOS
17+
) do |arguments|
18+
19+
# Validate the number of arguments.
20+
if arguments.size != 2
21+
raise(Puppet::ParseError, "join_keys_to_values(): Takes exactly two " +
22+
"arguments, but #{arguments.size} given.")
23+
end
24+
25+
# Validate the first argument.
26+
hash = arguments[0]
27+
if not hash.is_a?(Hash)
28+
raise(TypeError, "join_keys_to_values(): The first argument must be a " +
29+
"hash, but a #{hash.class} was given.")
30+
end
31+
32+
# Validate the second argument.
33+
separator = arguments[1]
34+
if not separator.is_a?(String)
35+
raise(TypeError, "join_keys_to_values(): The second argument must be a " +
36+
"string, but a #{separator.class} was given.")
37+
end
38+
39+
# Join the keys to their values.
40+
hash.map do |k,v|
41+
String(k) + separator + String(v)
42+
end
43+
44+
end
45+
end
46+
47+
# vim: set ts=2 sw=2 et :
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the join_keys_to_values function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
it "should exist" do
8+
Puppet::Parser::Functions.function("join_keys_to_values").should == "function_join_keys_to_values"
9+
end
10+
11+
it "should raise a ParseError if there are fewer than two arguments" do
12+
lambda { scope.function_join_keys_to_values([{}]) }.should raise_error Puppet::ParseError
13+
end
14+
15+
it "should raise a ParseError if there are greater than two arguments" do
16+
lambda { scope.function_join_keys_to_values([{}, 'foo', 'bar']) }.should raise_error Puppet::ParseError
17+
end
18+
19+
it "should raise a TypeError if the first argument is an array" do
20+
lambda { scope.function_join_keys_to_values([[1,2], ',']) }.should raise_error TypeError
21+
end
22+
23+
it "should raise a TypeError if the second argument is an array" do
24+
lambda { scope.function_join_keys_to_values([{}, [1,2]]) }.should raise_error TypeError
25+
end
26+
27+
it "should raise a TypeError if the second argument is a number" do
28+
lambda { scope.function_join_keys_to_values([{}, 1]) }.should raise_error TypeError
29+
end
30+
31+
it "should return an empty array given an empty hash" do
32+
result = scope.function_join_keys_to_values([{}, ":"])
33+
result.should == []
34+
end
35+
36+
it "should join hash's keys to its values" do
37+
result = scope.function_join_keys_to_values([{'a'=>1,2=>'foo',:b=>nil}, ":"])
38+
result.should =~ ['a:1','2:foo','b:']
39+
end
40+
end

0 commit comments

Comments
 (0)