module Puppet::MetaType::Manager
Public Instance Methods
An implementation specific method that removes all type instances during testing. @note Only use this method for testing purposes. @api private
# File lib/puppet/metatype/manager.rb 18 def allclear 19 @types.each { |name, type| 20 type.clear 21 } 22 end
Clears any types that were used but absent when types were last loaded. @note Used after each catalog compile when always_retry_plugins is false @api private
# File lib/puppet/metatype/manager.rb 28 def clear_misses 29 unless @types.nil? 30 @types.delete_if {|_, v| v.nil? } 31 end 32 end
Iterates over all already loaded Type subclasses. @yield [t] a block receiving each type @yieldparam t [Puppet::Type] each defined type @yieldreturn [Object] the last returned object is also returned from this method @return [Object] the last returned value from the block.
# File lib/puppet/metatype/manager.rb 39 def eachtype 40 @types.each do |name, type| 41 # Only consider types that have names 42 #if ! type.parameters.empty? or ! type.validproperties.empty? 43 yield type 44 #end 45 end 46 end
Loads all types. @note Should only be used for purposes such as generating documentation as this is potentially a very
expensive operation.
@return [void]
# File lib/puppet/metatype/manager.rb 53 def loadall 54 typeloader.loadall(Puppet.lookup(:current_environment)) 55 end
Defines a new type or redefines an existing type with the given name. A convenience method on the form `new<name>` where name is the name of the type is also created. (If this generated method happens to clash with an existing method, a warning is issued and the original method is kept).
@param name [String] the name of the type to create or redefine. @param options [Hash] options passed on to {Puppet::Util::ClassGen#genclass} as the option `:attributes`. @option options [Puppet::Type]
Puppet::Type. This option is not passed on as an attribute to genclass.
@yield [ ] a block evaluated in the context of the created class, thus allowing further detailing of
that class.
@return [Class<inherits Puppet::Type>] the created subclass @see Puppet::Util::ClassGen.genclass
@dsl type @api public
# File lib/puppet/metatype/manager.rb 73 def newtype(name, options = {}, &block) 74 @manager_lock.synchronize do 75 # Handle backward compatibility 76 unless options.is_a?(Hash) 77 #TRANSLATORS 'Puppet::Type.newtype' should not be translated 78 Puppet.warning(_("Puppet::Type.newtype(%{name}) now expects a hash as the second argument, not %{argument}") % 79 { name: name, argument: options.inspect}) 80 end 81 82 # First make sure we don't have a method sitting around 83 name = name.intern 84 newmethod = "new#{name}" 85 86 # Used for method manipulation. 87 selfobj = singleton_class 88 89 if @types.include?(name) 90 if self.respond_to?(newmethod) 91 # Remove the old newmethod 92 selfobj.send(:remove_method,newmethod) 93 end 94 end 95 96 # Then create the class. 97 98 klass = genclass( 99 name, 100 :parent => Puppet::Type, 101 :overwrite => true, 102 :hash => @types, 103 :attributes => options, 104 &block 105 ) 106 107 # Now define a "new<type>" method for convenience. 108 if self.respond_to? newmethod 109 # Refuse to overwrite existing methods like 'newparam' or 'newtype'. 110 #TRANSLATORS 'new%{method}' will become a method name, do not translate this string 111 Puppet.warning(_("'new%{method}' method already exists; skipping") % { method: name.to_s }) 112 else 113 selfobj.send(:define_method, newmethod) do |*args| 114 klass.new(*args) 115 end 116 end 117 118 # If they've got all the necessary methods defined and they haven't 119 # already added the property, then do so now. 120 klass.ensurable if klass.ensurable? and ! klass.validproperty?(:ensure) 121 122 # Now set up autoload any providers that might exist for this type. 123 124 klass.providerloader = Puppet::Util::Autoload.new(klass, "puppet/provider/#{klass.name}") 125 126 # We have to load everything so that we can figure out the default provider. 127 klass.providerloader.loadall(Puppet.lookup(:current_environment)) 128 klass.providify unless klass.providers.empty? 129 130 loc = block_given? ? block.source_location : nil 131 uri = loc.nil? ? nil : URI("#{Puppet::Util.path_to_uri(loc[0])}?line=#{loc[1]}") 132 Puppet::Pops::Loaders.register_runtime3_type(name, uri) 133 134 klass 135 end 136 end
Removes an existing type. @note Only use this for testing. @api private
# File lib/puppet/metatype/manager.rb 141 def rmtype(name) 142 # Then create the class. 143 144 rmclass(name, :hash => @types) 145 146 singleton_class.send(:remove_method, "new#{name}") if respond_to?("new#{name}") 147 end
Returns a Type instance by name. This will load the type if not already defined. @param [String, Symbol] name of the wanted Type @return [Puppet::Type, nil] the type or nil if the type was not defined and could not be loaded
# File lib/puppet/metatype/manager.rb 154 def type(name) 155 @manager_lock.synchronize do 156 # Avoid loading if name obviously is not a type name 157 if name.to_s.include?(':') 158 return nil 159 end 160 161 # We are overwhelmingly symbols here, which usually match, so it is worth 162 # having this special-case to return quickly. Like, 25K symbols vs. 300 163 # strings in this method. --daniel 2012-07-17 164 return @types[name] if @types.include? name 165 166 # Try mangling the name, if it is a string. 167 if name.is_a? String 168 name = name.downcase.intern 169 return @types[name] if @types.include? name 170 end 171 # Try loading the type. 172 if typeloader.load(name, Puppet.lookup(:current_environment)) 173 #TRANSLATORS 'puppet/type/%{name}' should not be translated 174 Puppet.warning(_("Loaded puppet/type/%{name} but no class was created") % { name: name }) unless @types.include? name 175 elsif !Puppet[:always_retry_plugins] 176 # PUP-5482 - Only look for a type once if plugin retry is disabled 177 @types[name] = nil 178 end 179 180 # ...and I guess that is that, eh. 181 return @types[name] 182 end 183 end
Creates a loader for Puppet types. Defaults to an instance of {Puppet::Util::Autoload} if no other auto loader has been set. @return [Puppet::Util::Autoload] the loader to use. @api private
# File lib/puppet/metatype/manager.rb 189 def typeloader 190 unless defined?(@typeloader) 191 @typeloader = Puppet::Util::Autoload.new(self, "puppet/type") 192 end 193 194 @typeloader 195 end