class Puppet::Pops::Types::ImplementationRegistry
The {ImplementationRegistry} maps names types in the Puppet Type System to names of corresponding implementation modules/classes. Each mapping is unique and bidirectional so that for any given type name there is only one implementation and vice versa.
@api private
Constants
- TYPE_REGEXP_SUBST
Public Class Methods
Create a new instance. This method is normally only called once
@param parent [ImplementationRegistry, nil] the parent of this registry
# File lib/puppet/pops/types/implementation_registry.rb 15 def initialize(parent = nil) 16 @parent = parent 17 @type_names_per_implementation = {} 18 @implementations_per_type_name = {} 19 @type_name_substitutions = [] 20 @impl_name_substitutions = [] 21 end
Public Instance Methods
Find the module that corresponds to the given type or type name
@param type [PAnyType,String] the name of the type @return [Module,nil] the name of the implementation module, or `nil` if no mapping was found
# File lib/puppet/pops/types/implementation_registry.rb 90 def module_for_type(type) 91 name = module_name_for_type(type) 92 # TODO Shouldn't ClassLoader be module specific? 93 name.nil? ? nil : ClassLoader.provide(name) 94 end
Find the name for the module that corresponds to the given type or type name
@param type [PAnyType,String] the name of the type @return [String,nil] the name of the implementation module, or `nil` if no mapping was found
# File lib/puppet/pops/types/implementation_registry.rb 80 def module_name_for_type(type) 81 type = type.name if type.is_a?(PAnyType) 82 name = @parent.module_name_for_type(type) unless @parent.nil? 83 name.nil? ? find_mapping(type, @implementations_per_type_name, @type_name_substitutions) : name 84 end
Register a bidirectional mapping between a type and an implementation
@param type [PAnyType,String] the type or type name @param impl_module the module or module name
# File lib/puppet/pops/types/implementation_registry.rb 68 def register_implementation(type, impl_module, _ = nil) 69 type = type.name if type.is_a?(PAnyType) 70 impl_module = impl_module.name if impl_module.is_a?(Module) 71 @type_names_per_implementation[impl_module] = type 72 @implementations_per_type_name[type] = impl_module 73 nil 74 end
Register a bidirectional namespace mapping
@param type_namespace [String] the namespace for the puppet types @param impl_namespace [String] the namespace for the implementations
# File lib/puppet/pops/types/implementation_registry.rb 47 def register_implementation_namespace(type_namespace, impl_namespace, _ = nil) 48 ns = TypeFormatter::NAME_SEGMENT_SEPARATOR 49 register_implementation_regexp( 50 [/\A#{type_namespace}#{ns}(\w+)\z/, "#{impl_namespace}#{ns}\\1"], 51 [/\A#{impl_namespace}#{ns}(\w+)\z/, "#{type_namespace}#{ns}\\1"]) 52 end
Register a bidirectional regexp mapping
@param type_name_subst [Array(Regexp,String)] regexp and replacement mapping type names to runtime names @param impl_name_subst [Array(Regexp,String)] regexp and replacement mapping runtime names to type names
# File lib/puppet/pops/types/implementation_registry.rb 58 def register_implementation_regexp(type_name_subst, impl_name_subst, _ = nil) 59 @type_name_substitutions << type_name_subst 60 @impl_name_substitutions << impl_name_subst 61 nil 62 end
Register a bidirectional type mapping.
@overload register_type_mapping(runtime_type, puppet_type)
@param runtime_type [PRuntimeType] type that represents the runtime module or class to map to a puppet type @param puppet_type [PAnyType] type that will be mapped to the runtime module or class
@overload register_type_mapping(runtime_type, pattern_replacement)
@param runtime_type [PRuntimeType] type containing the pattern and replacement to map the runtime type to a puppet type @param puppet_type [Array(Regexp,String)] the pattern and replacement to map a puppet type to a runtime type
# File lib/puppet/pops/types/implementation_registry.rb 31 def register_type_mapping(runtime_type, puppet_type_or_pattern, _ = nil) 32 TypeAsserter.assert_assignable('First argument of type mapping', PRuntimeType::RUBY, runtime_type) 33 expr = runtime_type.name_or_pattern 34 if expr.is_a?(Array) 35 TypeAsserter.assert_instance_of('Second argument of type mapping', TYPE_REGEXP_SUBST, puppet_type_or_pattern) 36 register_implementation_regexp(puppet_type_or_pattern, expr) 37 else 38 TypeAsserter.assert_instance_of('Second argument of type mapping', PTypeType::DEFAULT, puppet_type_or_pattern) 39 register_implementation(puppet_type_or_pattern, expr) 40 end 41 end
Find the name for, and then load, the type that corresponds to the given runtime module or module name The method will return `nil` if no mapping is found, a TypeReference if a mapping was found but the loader didn't find the type, or the loaded type.
@param impl_module [Module,String] the implementation class or class name @return [PAnyType,nil] the type, or `nil` if no mapping was found
# File lib/puppet/pops/types/implementation_registry.rb 112 def type_for_module(impl_module) 113 name = type_name_for_module(impl_module) 114 if name.nil? 115 nil 116 else 117 TypeParser.singleton.parse(name) 118 end 119 end
Find the type name and loader that corresponds to the given runtime module or module name
@param impl_module [Module,String] the implementation class or class name @return [String,nil] the name of the type, or `nil` if no mapping was found
# File lib/puppet/pops/types/implementation_registry.rb 100 def type_name_for_module(impl_module) 101 impl_module = impl_module.name if impl_module.is_a?(Module) 102 name = @parent.type_name_for_module(impl_module) unless @parent.nil? 103 name.nil? ? find_mapping(impl_module, @type_names_per_implementation, @impl_name_substitutions) : name 104 end
Private Instance Methods
# File lib/puppet/pops/types/implementation_registry.rb 123 def find_mapping(name, names, substitutions) 124 found = names[name] 125 if found.nil? 126 substitutions.each do |subst| 127 substituted = name.sub(*subst) 128 return substituted unless substituted == name 129 end 130 end 131 found 132 end