3333class Chef
3434 class Node
3535
36- attr_accessor :attribute , : recipe_list, :couchdb , :couchdb_rev , :run_state , :run_list , :override_attrs , :default_attrs , :cookbook_loader
36+ attr_accessor :recipe_list , :couchdb , :couchdb_rev , :run_state , :run_list , :override_attrs , :default_attrs , :normal_attrs , :cookbook_loader
3737 attr_reader :node
3838 attr_reader :couchdb_id
3939
@@ -131,7 +131,7 @@ def initialize(couchdb=nil)
131131 @name = nil
132132 @node = self
133133
134- @attribute = Mash . new
134+ @normal_attrs = Mash . new
135135 @override_attrs = Mash . new
136136 @default_attrs = Mash . new
137137 @run_list = Chef ::RunList . new
@@ -191,64 +191,109 @@ def name(arg=nil)
191191 @name
192192 end
193193 end
194+
195+ def attribute
196+ Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs )
197+ end
194198
195199 # Return an attribute of this node. Returns nil if the attribute is not found.
196200 def []( attrib )
197- Chef ::Node ::Attribute . new ( @attribute , @default_attrs , @override_attrs ) [ attrib ]
201+ Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs ) [ attrib ]
198202 end
199203
200204 # Set an attribute of this node
201205 def []=( attrib , value )
202- Chef ::Node ::Attribute . new ( @attribute , @default_attrs , @override_attrs ) [ attrib ] = value
206+ Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs ) [ attrib ] = value
203207 end
204208
205209 def store ( attrib , value )
206210 self [ attrib ] = value
207211 end
208212
209- # Set an attribute of this node, but auto-vivifiy any Mashes that might
210- # be missing
213+ # Set a normal attribute of this node, but auto-vivifiy any Mashes that
214+ # might be missing
211215 def set
212- attrs = Chef ::Node ::Attribute . new ( @attribute , @default_attrs , @override_attrs )
216+ attrs = Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs )
217+ attrs . set_type = :attribute
213218 attrs . auto_vivifiy_on_read = true
214219 attrs
215220 end
216221
217- # Set an attribute of this node, auto-vivifiying any mashes that are
222+ alias_method :normal , :set
223+
224+ # Set a normal attribute of this node, auto-vivifiying any mashes that are
218225 # missing, but if the final value already exists, don't set it
219226 def set_unless
220- attrs = Chef ::Node ::Attribute . new ( @attribute , @default_attrs , @override_attrs )
227+ attrs = Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs )
228+ attrs . set_type = :attribute
221229 attrs . auto_vivifiy_on_read = true
222230 attrs . set_unless_value_present = true
223231 attrs
224232 end
233+ alias_method :normal_unless , :set_unless
234+
235+ # Set a default of this node, but auto-vivifiy any Mashes that might
236+ # be missing
237+ def default
238+ attrs = Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs )
239+ attrs . set_type = :default
240+ attrs . auto_vivifiy_on_read = true
241+ attrs
242+ end
225243
226- alias_method :default , :set_unless
244+ # Set a default attribute of this node, auto-vivifiying any mashes that are
245+ # missing, but if the final value already exists, don't set it
246+ def default_unless
247+ attrs = Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs )
248+ attrs . set_type = :default
249+ attrs . auto_vivifiy_on_read = true
250+ attrs . set_unless_value_present = true
251+ attrs
252+ end
253+
254+ # Set an override attribute of this node, but auto-vivifiy any Mashes that
255+ # might be missing
256+ def override
257+ attrs = Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs )
258+ attrs . set_type = :override
259+ attrs . auto_vivifiy_on_read = true
260+ attrs
261+ end
262+
263+ # Set an override attribute of this node, auto-vivifiying any mashes that
264+ # are missing, but if the final value already exists, don't set it
265+ def override_unless
266+ attrs = Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs )
267+ attrs . set_type = :override
268+ attrs . auto_vivifiy_on_read = true
269+ attrs . set_unless_value_present = true
270+ attrs
271+ end
227272
228273 # Return true if this Node has a given attribute, false if not. Takes either a symbol or
229274 # a string.
230275 #
231276 # Only works on the top level. Preferred way is to use the normal [] style
232277 # lookup and call attribute?()
233278 def attribute? ( attrib )
234- Chef ::Node ::Attribute . new ( @attribute , @default_attrs , @override_attrs ) . attribute? ( attrib )
279+ Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs ) . attribute? ( attrib )
235280 end
236281
237282 # Yield each key of the top level to the block.
238283 def each ( &block )
239- Chef ::Node ::Attribute . new ( @attribute , @default_attrs , @override_attrs ) . each ( &block )
284+ Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs ) . each ( &block )
240285 end
241286
242287 # Iterates over each attribute, passing the attribute and value to the block.
243288 def each_attribute ( &block )
244- Chef ::Node ::Attribute . new ( @attribute , @default_attrs , @override_attrs ) . each_attribute ( &block )
289+ Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs ) . each_attribute ( &block )
245290 end
246291
247292 # Set an attribute based on the missing method. If you pass an argument, we'll use that
248293 # to set the attribute values. Otherwise, we'll wind up just returning the attributes
249294 # value.
250295 def method_missing ( symbol , *args )
251- Chef ::Node ::Attribute . new ( @attribute , @default_attrs , @override_attrs ) . send ( symbol , *args )
296+ Chef ::Node ::Attribute . new ( @normal_attrs , @default_attrs , @override_attrs ) . send ( symbol , *args )
252297 end
253298
254299 # Returns true if this Node expects a given recipe, false if not.
@@ -295,11 +340,11 @@ def consume_attributes(attrs)
295340 # Transform the node to a Hash
296341 def to_hash
297342 index_hash = Hash . new
298- self . each do |k , v |
299- index_hash [ k ] = v
300- end
301343 index_hash [ "chef_type" ] = "node"
302344 index_hash [ "name" ] = @name
345+ index_hash [ "attributes" ] = @normal_attrs
346+ index_hash [ "defaults" ] = @default_attrs
347+ index_hash [ "overrides" ] = @override_attrs
303348 index_hash [ "recipe" ] = @run_list . recipes if @run_list . recipes . length > 0
304349 index_hash [ "role" ] = @run_list . roles if @run_list . roles . length > 0
305350 index_hash [ "run_list" ] = @run_list . run_list if @run_list . run_list . length > 0
@@ -311,7 +356,7 @@ def to_json(*a)
311356 result = {
312357 "name" => @name ,
313358 'json_class' => self . class . name ,
314- "attributes" => @attribute ,
359+ "attributes" => @normal_attrs ,
315360 "chef_type" => "node" ,
316361 "defaults" => @default_attrs ,
317362 "overrides" => @override_attrs ,
@@ -325,8 +370,7 @@ def to_json(*a)
325370 def self . json_create ( o )
326371 node = new
327372 node . name ( o [ "name" ] )
328- o [ "attributes" ] . each { |k , v | node [ k ] = v }
329-
373+ node . normal_attrs = Mash . new ( o [ "attributes" ] ) if o . has_key? ( "attributes" )
330374 node . default_attrs = Mash . new ( o [ "defaults" ] ) if o . has_key? ( "defaults" )
331375 node . override_attrs = Mash . new ( o [ "overrides" ] ) if o . has_key? ( "overrides" )
332376
0 commit comments