Class: Puppet::Pops::Types::ImplementationRegistry Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/pops/types/implementation_registry.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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.

Constant Summary collapse

TYPE_REGEXP_SUBST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

TypeFactory.tuple([PRegexpType::DEFAULT, PStringType::NON_EMPTY])

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(static_loader) ⇒ ImplementationRegistry

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new instance. This method is normally only called once

The initializer will create mappings for well known types that can be loaded using the static loader



19
20
21
22
23
24
25
# File 'lib/puppet/pops/types/implementation_registry.rb', line 19

def initialize(static_loader)
  @type_names_per_implementation = {}
  @implementations_per_type_name = {}
  @type_name_substitutions = []
  @impl_name_substitutions = []
  TypeParser.type_map.values.each { |type| register_implementation(type.simple_name, type.class.name, static_loader) }
end

Class Method Details

.singletonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



11
12
13
# File 'lib/puppet/pops/types/implementation_registry.rb', line 11

def self.singleton
  @singleton ||= new(Loaders.static_loader)
end

Instance Method Details

#find_mapping(name, names, substitutions) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



129
130
131
132
133
134
135
136
137
138
# File 'lib/puppet/pops/types/implementation_registry.rb', line 129

def find_mapping(name, names, substitutions)
  found = names[name]
  if found.nil?
    substitutions.each do |subst|
      substituted = name.sub(*subst[0])
      return [substituted, subst[1]] unless substituted == name
    end
  end
  found
end

#module_for_type(type) ⇒ Module?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find the module that corresponds to the given type or type name

Parameters:

  • type (PAnyType, String)

    the name of the type

Returns:

  • (Module, nil)

    the name of the implementation module, or ‘nil` if no mapping was found



99
100
101
102
103
# File 'lib/puppet/pops/types/implementation_registry.rb', line 99

def module_for_type(type)
  name_and_loader = module_name_for_type(type)
  # TODO Shouldn't ClassLoader be module specific?
  name_and_loader.nil? ? nil : ClassLoader.provide(name_and_loader[0])
end

#module_name_for_type(type) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find the name for the module that corresponds to the given type or type name

Parameters:

  • type (PAnyType, String)

    the name of the type

Returns:

  • (String, nil)

    the name of the implementation module, or ‘nil` if no mapping was found



90
91
92
93
# File 'lib/puppet/pops/types/implementation_registry.rb', line 90

def module_name_for_type(type)
  type = type.name if type.is_a?(PAnyType)
  find_mapping(type, @implementations_per_type_name, @type_name_substitutions)
end

#register_implementation(type, impl_module, loader) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register a bidirectional mapping between a type and an implementation

Parameters:

  • type (PAnyType, String)

    the type or type name

  • impl_module (Module, String)

    the module or module name

  • loader (Loader::Loader)

    the loader to use when resolving names



78
79
80
81
82
83
84
# File 'lib/puppet/pops/types/implementation_registry.rb', line 78

def register_implementation(type, impl_module, loader)
  type = type.name if type.is_a?(PAnyType)
  impl_module = impl_module.name if impl_module.is_a?(Module)
  @type_names_per_implementation[impl_module] = [type, loader]
  @implementations_per_type_name[type] = [impl_module, loader]
  nil
end

#register_implementation_namespace(type_namespace, impl_namespace, loader) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register a bidirectional namespace mapping

Parameters:

  • type_namespace (String)

    the namespace for the puppet types

  • impl_namespace (String)

    the namespace for the implementations

  • loader (Loader::Loader)

    the loader to use when resolving names



54
55
56
57
58
59
60
# File 'lib/puppet/pops/types/implementation_registry.rb', line 54

def register_implementation_namespace(type_namespace, impl_namespace, loader)
  ns = TypeFormatter::NAME_SEGMENT_SEPARATOR
  register_implementation_regexp(
    [/\A#{type_namespace}#{ns}(\w+)\z/, "#{impl_namespace}#{ns}\\1"],
    [/\A#{impl_namespace}#{ns}(\w+)\z/, "#{type_namespace}#{ns}\\1"],
    loader)
end

#register_implementation_regexp(type_name_subst, impl_name_subst, loader) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register a bidirectional regexp mapping

Parameters:

  • type_name_subst (Array(Regexp,String))

    regexp and replacement mapping type names to runtime names

  • impl_name_subst (Array(Regexp,String))

    regexp and replacement mapping runtime names to type names

  • loader (Loader::Loader)

    the loader to use when resolving names



67
68
69
70
71
# File 'lib/puppet/pops/types/implementation_registry.rb', line 67

def register_implementation_regexp(type_name_subst, impl_name_subst, loader)
  @type_name_substitutions << [type_name_subst, loader]
  @impl_name_substitutions << [impl_name_subst, loader]
  nil
end

#register_type_mapping(runtime_type, puppet_type) ⇒ Object #register_type_mapping(runtime_type, pattern_replacement) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register a bidirectional type mapping.

Overloads:

  • #register_type_mapping(runtime_type, puppet_type) ⇒ Object

    Parameters:

    • runtime_type (PRuntimeType)

      type that represents the runtime module or class to map to a puppet type

    • puppet_type (PAnyType)

      type that will be mapped to the runtime module or class

    • loader (Loader::Loader)

      the loader to use when resolving names

  • #register_type_mapping(runtime_type, pattern_replacement) ⇒ Object

    Parameters:

    • runtime_type (PRuntimeType)

      type containing the pattern and replacement to map the runtime type to a puppet type

    • puppet_type (Array(Regexp,String))

      the pattern and replacement to map a puppet type to a runtime type

    • loader (Loader::Loader)

      the loader to use when resolving names



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/puppet/pops/types/implementation_registry.rb', line 37

def register_type_mapping(runtime_type, puppet_type_or_pattern, loader)
  TypeAsserter.assert_assignable('First argument of type mapping', PRuntimeType::RUBY, runtime_type)
  expr = runtime_type.name_or_pattern
  if expr.is_a?(Array)
    TypeAsserter.assert_instance_of('Second argument of type mapping', TYPE_REGEXP_SUBST, puppet_type_or_pattern)
    register_implementation_regexp(puppet_type_or_pattern, expr, loader)
  else
    TypeAsserter.assert_instance_of('Second argument of type mapping', PTypeType::DEFAULT, puppet_type_or_pattern)
    register_implementation(puppet_type_or_pattern, expr, loader)
  end
end

#type_for_module(impl_module) ⇒ PAnyType?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.

Parameters:

  • impl_module (Module, String)

    the implementation class or class name

Returns:

  • (PAnyType, nil)

    the type, or ‘nil` if no mapping was found



120
121
122
123
124
125
126
127
# File 'lib/puppet/pops/types/implementation_registry.rb', line 120

def type_for_module(impl_module)
  name_and_loader = type_name_for_module(impl_module)
  if name_and_loader.nil?
    nil
  else
    TypeParser.singleton.parse(*name_and_loader)
  end
end

#type_name_for_module(impl_module) ⇒ Array(String,Loader::Loader)?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Find the type name and loader that corresponds to the given runtime module or module name

Parameters:

  • impl_module (Module, String)

    the implementation class or class name

Returns:

  • (Array(String,Loader::Loader), nil)

    the name and loader of the type, or ‘nil` if no mapping was found



109
110
111
112
# File 'lib/puppet/pops/types/implementation_registry.rb', line 109

def type_name_for_module(impl_module)
  impl_module = impl_module.name if impl_module.is_a?(Module)
  find_mapping(impl_module, @type_names_per_implementation, @impl_name_substitutions)
end