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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
#--
# Author:: Adam Jacob (<adam@opscode.com>)
# Author:: AJ Christensen (<aj@opscode.com>)
# Copyright:: Copyright (c) 2008 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/node/immutable_collections'
require 'chef/node/attribute_collections'
require 'chef/mixin/deep_merge'
require 'chef/log'
class Chef
class Node
# == Attribute
# Attribute implements a nested key-value (Hash) and flat collection
# (Array) data structure supporting multiple levels of precedence, such
# that a given key may have multiple values internally, but will only
# return the highest precedence value when reading.
class Attribute < Mash
include Immutablize
include Enumerable
COMPONENTS = [:@default, :@normal, :@override, :@automatic].freeze
COMPONENT_ACCESSORS = {:default => :@default,
:normal => :@normal,
:override => :@override,
:automatic => :@automatic
}
attr_accessor :properties
attr_reader :serial_number
[:all?,
:any?,
:assoc,
:chunk,
:collect,
:collect_concat,
:compare_by_identity,
:compare_by_identity?,
:count,
:cycle,
:detect,
:drop,
:drop_while,
:each,
:each_cons,
:each_entry,
:each_key,
:each_pair,
:each_slice,
:each_value,
:each_with_index,
:each_with_object,
:empty?,
:entries,
:except,
:fetch,
:find,
:find_all,
:find_index,
:first,
:flat_map,
:flatten,
:grep,
:group_by,
:has_value?,
:include?,
:index,
:inject,
:invert,
:key,
:keys,
:length,
:map,
:max,
:max_by,
:merge,
:min,
:min_by,
:minmax,
:minmax_by,
:none?,
:one?,
:partition,
:rassoc,
:reduce,
:reject,
:reverse_each,
:select,
:size,
:slice_before,
:sort,
:sort_by,
:store,
:symbolize_keys,
:take,
:take_while,
:to_a,
:to_hash,
:to_set,
:value?,
:values,
:values_at,
:zip].each do |delegated_method|
class_eval(<<-METHOD_DEFN)
def #{delegated_method}(*args, &block)
merged_attributes.send(:#{delegated_method}, *args, &block)
end
METHOD_DEFN
end
def initialize(normal, default, override, automatic)
@serial_number = 0
@set_unless_present = false
@normal = VividMash.new(self, normal)
@default = VividMash.new(self, default)
@override = VividMash.new(self, override)
@automatic = VividMash.new(self, automatic)
@merged_attributes = nil
end
def set_unless_value_present=(setting)
@set_unless_present = setting
end
def reset_cache
@serial_number += 1
@merged_attributes = nil
end
def reset
@serial_number += 1
@merged_attributes = nil
end
def default
@default
end
def default=(new_data)
reset
@default = VividMash.new(self, new_data)
end
def normal
@normal
end
def normal=(new_data)
reset
@normal = VividMash.new(self, new_data)
end
def override
@override
end
def override=(new_data)
reset
@override = VividMash.new(self, new_data)
end
def automatic
@automatic
end
def automatic=(new_data)
reset
@automatic = VividMash.new(self, new_data)
end
def merged_attributes
@merged_attributes ||= begin
resolved_attrs = COMPONENTS.inject(Mash.new) do |merged, component_ivar|
component_value = instance_variable_get(component_ivar)
Chef::Mixin::DeepMerge.merge(merged, component_value)
end
immutablize(self, resolved_attrs)
end
end
def [](key)
merged_attributes[key]
end
def []=(key, value)
merged_attributes[key] = value
end
def has_key?(key)
COMPONENTS.any? do |component_ivar|
instance_variable_get(component_ivar).has_key?(key)
end
end
alias :attribute? :has_key?
alias :member? :has_key?
alias :include? :has_key?
alias :key? :has_key?
alias :each_attribute :each
def method_missing(symbol, *args)
if args.empty?
if key?(symbol)
self[symbol]
else
raise NoMethodError, "Undefined method or attribute `#{symbol}' on `node'"
end
elsif symbol.to_s =~ /=$/
key_to_set = symbol.to_s[/^(.+)=$/, 1]
self[key_to_set] = (args.length == 1 ? args[0] : args)
else
raise NoMethodError, "Undefined node attribute or method `#{symbol}' on `node'"
end
end
def inspect
"#<#{self.class} " << (COMPONENTS + [:@merged_attributes, :@properties]).map{|iv|
"#{iv}=#{instance_variable_get(iv).inspect}"
}.join(', ') << ">"
end
def set_unless?
@set_unless_present
end
def stale_subtree?(serial_number)
serial_number != @serial_number
end
end
end
end
|