From fc1f8cdbee606da0d2a1a162942295d28cdcbf64 Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Thu, 4 Jun 2009 00:32:12 -0500 Subject: Adding a special class to handle loaded classes/defines/nodes This class is extracted from the Parser class, and the main driver for it is to enable us to put mutexes around some of the hashes to see if they're the source of a race condition. Signed-off-by: Luke Kanies --- lib/puppet/parser/loaded_code.rb | 85 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 lib/puppet/parser/loaded_code.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/loaded_code.rb b/lib/puppet/parser/loaded_code.rb new file mode 100644 index 000000000..7c918d4c3 --- /dev/null +++ b/lib/puppet/parser/loaded_code.rb @@ -0,0 +1,85 @@ +class Puppet::Parser::LoadedCode + def initialize + @hostclasses = {} + @definitions = {} + @nodes = {} + end + + def add_hostclass(name, code) + @hostclasses[munge_name(name)] = code + end + + def hostclass(name) + @hostclasses[munge_name(name)] + end + + def add_node(name, code) + @nodes[munge_name(name)] = code + end + + def node(name) + @nodes[munge_name(name)] + end + + def nodes? + @nodes.length > 0 + end + + def add_definition(name, code) + @definitions[munge_name(name)] = code + end + + def definition(name) + @definitions[munge_name(name)] + end + + def find(namespace, name, type) + if r = find_fully_qualified(name, type) + return r + end + + ary = namespace.split("::") + + while ary.length > 0 + tmp_namespace = ary.join("::") + if r = find_partially_qualified(tmp_namespace, name, type) + return r + end + + # Delete the second to last object, which reduces our namespace by one. + ary.pop + end + + send(type, name) + end + + def find_node(name) + find("", name, :node) + end + + def find_hostclass(namespace, name) + find(namespace, name, :hostclass) + end + + def find_definition(namespace, name) + find(namespace, name, :definition) + end + + private + + def find_fully_qualified(name, type) + return nil unless name =~ /^::/ + + name = name.sub(/^::/, '') + + send(type, name) + end + + def find_partially_qualified(namespace, name, type) + send(type, [namespace, name].join("::")) + end + + def munge_name(name) + name.to_s.downcase + end +end -- cgit