summaryrefslogtreecommitdiff
path: root/lib/chef/node_map.rb
diff options
context:
space:
mode:
authorJohn Keiser <john@johnkeiser.com>2015-06-29 16:20:15 -0600
committerJohn Keiser <john@johnkeiser.com>2015-06-30 11:18:41 -0600
commit7f2efa0bdabee2745b9b61725d88944bedd44052 (patch)
tree95dc93c7059d18ac90b173fca227dd284f2131cc /lib/chef/node_map.rb
parent2bf7afdba5496f5f3e7ba065650c6db5522fe8c1 (diff)
downloadchef-7f2efa0bdabee2745b9b61725d88944bedd44052.tar.gz
Optimize logic on class initialize so it doesn't fall into the
"look at all classes to see if they provide the DSL" else clause when you're just setting resource_name (which happens on every class). Perf fix for tests.
Diffstat (limited to 'lib/chef/node_map.rb')
-rw-r--r--lib/chef/node_map.rb31
1 files changed, 14 insertions, 17 deletions
diff --git a/lib/chef/node_map.rb b/lib/chef/node_map.rb
index edf0fd689a..1ce52818b2 100644
--- a/lib/chef/node_map.rb
+++ b/lib/chef/node_map.rb
@@ -20,13 +20,6 @@ class Chef
class NodeMap
#
- # Create a new NodeMap
- #
- def initialize
- @map = {}
- end
-
- #
# Set a key/value pair on the map with a filter. The filter must be true
# when applied to the node in order to retrieve the value.
#
@@ -55,18 +48,18 @@ class Chef
# The map is sorted in order of preference already; we just need to find
# our place in it (just before the first value with the same preference level).
insert_at = nil
- @map[key] ||= []
- @map[key].each_with_index do |matcher,index|
+ map[key] ||= []
+ map[key].each_with_index do |matcher,index|
cmp = compare_matchers(key, new_matcher, matcher)
insert_at ||= index if cmp && cmp <= 0
end
if insert_at
- @map[key].insert(insert_at, new_matcher)
+ map[key].insert(insert_at, new_matcher)
else
- @map[key] << new_matcher
+ map[key] << new_matcher
end
- insert_at ||= @map[key].size - 1
- @map
+ insert_at ||= map[key].size - 1
+ map
end
#
@@ -100,8 +93,8 @@ class Chef
#
def list(node, key, canonical: nil)
raise ArgumentError, "first argument must be a Chef::Node" unless node.is_a?(Chef::Node) || node.nil?
- return [] unless @map.has_key?(key)
- @map[key].select do |matcher|
+ return [] unless map.has_key?(key)
+ map[key].select do |matcher|
node_matches?(node, matcher) && canonical_matches?(canonical, matcher)
end.map { |matcher| matcher[:value] }
end
@@ -110,11 +103,11 @@ class Chef
# @return remaining
# @api private
def delete_canonical(key, value)
- remaining = @map[key]
+ remaining = map[key]
if remaining
remaining.delete_if { |matcher| matcher[:canonical] && Array(matcher[:value]) == Array(value) }
if remaining.empty?
- @map.delete(key)
+ map.delete(key)
remaining = nil
end
end
@@ -222,5 +215,9 @@ class Chef
end
cmp
end
+
+ def map
+ @map ||= {}
+ end
end
end