summaryrefslogtreecommitdiffstats
path: root/lib/puppet/property/keyvalue.rb
blob: df6560643068efce6ff99c8e2f93b4443ee7b6e2 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
#This subclass of property manages string key value pairs.

#In order to use this property:
# - the @should value must be an array of keyvalue pairs separated by the 'separator'
# - the retrieve method should return a hash with the keys as symbols
# IMPORTANT NOTE: In order for this property to work there must also be a 'membership' parameter
# The class that inherits from property should override that method with the symbol for the membership

require 'puppet/property'

module Puppet
    class Property
        class KeyValue < Property

            def hash_to_key_value_s(hash)
                hash.select { |k,v| true }.map { |pair| pair.join(separator) }.join(delimiter)
            end

            def should_to_s(should_value)
                hash_to_key_value_s(should_value)
            end

            def is_to_s(current_value)
                hash_to_key_value_s(current_value)
            end

            def membership
                :key_value_membership
            end

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

            def hashify(key_value_array)
                #turns string array into a hash
                key_value_array.inject({}) do |hash, key_value|
                    tmp = key_value.split(separator)
                    hash[tmp[0].intern] = tmp[1]
                    hash
                end
            end

            def process_current_hash(current)
                return {} if current == :absent

                #inclusive means we are managing everything so if it isn't in should, its gone
                if inclusive?
                    current.each_key { |key| current[key] = nil }
                end
                current
            end

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

                members = hashify(@should)
                current = process_current_hash(retrieve)

                #shared keys will get overwritten by members
                current.merge(members)
            end

            def separator
                "="
            end

            def delimiter
                ";"
            end

            def retrieve
                #ok, some 'convention' if the keyvalue property is named properties, provider should implement a properties method
                if key_hash = provider.send(name) and key_hash != :absent
                    return key_hash
                else
                    return :absent
                end
            end

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

                unless is
                    return true
                end

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