Module: Chef::Node::Mixin::DeepMergeCache

Included in:
Attribute
Defined in:
lib/chef/node/mixin/deep_merge_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#deep_merge_cacheObject

Cache of deep merged values by top-level key. This is a simple hash which has keys that are the top-level keys of the node object, and we save the computed deep-merge for that key here. There is no cache of subtrees.


25
26
27
# File 'lib/chef/node/mixin/deep_merge_cache.rb', line 25

def deep_merge_cache
  @deep_merge_cache
end

Instance Method Details

#[](key) ⇒ Object


48
49
50
51
52
53
54
55
56
# File 'lib/chef/node/mixin/deep_merge_cache.rb', line 48

def [](key)
  if deep_merge_cache.has_key?(key.to_s)
    # return the cache of the deep merged values by top-level key
    deep_merge_cache[key.to_s]
  else
    # save all the work of computing node[key]
    deep_merge_cache[key.to_s] = merged_attributes(key)
  end
end

#initializeObject


27
28
29
30
31
32
# File 'lib/chef/node/mixin/deep_merge_cache.rb', line 27

def initialize
  @merged_attributes = nil
  @combined_override = nil
  @combined_default = nil
  @deep_merge_cache = {}
end

#reset_cache(path = nil) ⇒ Object Also known as: reset

Invalidate a key in the deep_merge_cache. If called with nil, or no arg, this will invalidate the entire deep_merge cache. In the case of the user doing node.default['foo']['bar']['baz']= that eventually results in a call to reset_cache('foo') here. A node.default=hash_thing call must invalidate the entire cache and re-deep-merge the entire node object.


38
39
40
41
42
43
44
# File 'lib/chef/node/mixin/deep_merge_cache.rb', line 38

def reset_cache(path = nil)
  if path.nil?
    deep_merge_cache.clear
  else
    deep_merge_cache.delete(path.to_s)
  end
end