summaryrefslogtreecommitdiff
path: root/lib/puppet/parser/functions/intersection.rb
blob: 48f02e9d3794d61f1139b6bd165c12ca43f7702d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#
# intersection.rb
#

module Puppet::Parser::Functions
  newfunction(:intersection, :type => :rvalue, :doc => <<-EOS
This function returns an array an intersection of two.

*Examples:*

    intersection(["a","b","c"],["b","c","d"])

Would return: ["b","c"]
    EOS
  ) do |arguments|

    # Two arguments are required
    raise(Puppet::ParseError, "intersection(): Wrong number of arguments " +
      "given (#{arguments.size} for 2)") if arguments.size != 2

    first = arguments[0]
    second = arguments[1]

    unless first.is_a?(Array) && second.is_a?(Array)
      raise(Puppet::ParseError, 'intersection(): Requires 2 arrays')
    end

    result = first & second

    return result
  end
end

# vim: set ts=2 sw=2 et :