summaryrefslogtreecommitdiffstats
path: root/lib/puppet/property/list.rb
blob: 0c933f16471cd1f5b9a7a304218c92224d56a9cc (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require 'puppet/property'

module Puppet
    class Property
        class List < Property

            def should_to_s(should_value)
                #just return the should value
                should_value
            end

            def is_to_s(currentvalue)
                currentvalue.join(delimiter)
            end

            def membership
                :membership
            end

            def add_should_with_current(should, current)
                if current.is_a?(Array)
                    should += current
                end
                should.uniq
            end

            def inclusive?
                @resource[membership] == :inclusive
            end

            #dearrayify was motivated because to simplify the implementation of the OrderedList property
            def dearrayify(array)
                array.sort.join(delimiter)
            end

            def should
                unless defined? @should and @should
                    return nil
                end

                members = @should
                #inclusive means we are managing everything so if it isn't in should, its gone
                if ! inclusive?
                    members = add_should_with_current(members, retrieve)
                end

                dearrayify(members)
            end

            def delimiter
                ","
            end

            def retrieve
                #ok, some 'convention' if the list property is named groups, provider should implement a groups method
                if tmp = provider.send(name) and tmp != :absent
                    return tmp.split(delimiter)
                else
                    return :absent
                end
            end

            def prepare_is_for_comparison(is)
                if is.is_a? Array
                    is = dearrayify(is)
                end
                is
            end

            def insync?(is)
                unless defined? @should and @should
                    return true
                end

                unless is
                    return true
                end

                return (prepare_is_for_comparison(is) == self.should)
            end
        end
    end
end