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 17 def allclear 18 @types.each { |name, type| 19 type.clear 20 } 21 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 27 def clear_misses 28 unless @types.nil? 29 @types.delete_if {|_, v| v.nil? } 30 end 31 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 38 def eachtype 39 @types.each do |name, type| 40 # Only consider types that have names 41 #if ! type.parameters.empty? or ! type.validproperties.empty? 42 yield type 43 #end 44 end 45 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 52 def loadall 53 typeloader.loadall(Puppet.lookup(:current_environment)) 54 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 72 def newtype(name, options = {}, &block) 73 @manager_lock.synchronize do 74 # Handle backward compatibility 75 unless options.is_a?(Hash) 76 #TRANSLATORS 'Puppet::Type.newtype' should not be translated 77 Puppet.warning(_("Puppet::Type.newtype(%{name}) now expects a hash as the second argument, not %{argument}") % 78 { name: name, argument: options.inspect}) 79 end 80 81 # First make sure we don't have a method sitting around 82 name = name.intern 83 newmethod = "new#{name}" 84 85 # Used for method manipulation. 86 selfobj = singleton_class 87 88 if @types.include?(name) 89 if self.respond_to?(newmethod) 90 # Remove the old newmethod 91 selfobj.send(:remove_method,newmethod) 92 end 93 end 94 95 # Then create the class. 96 97 klass = genclass( 98 name, 99 :parent => Puppet::Type, 100 :overwrite => true, 101 :hash => @types, 102 :attributes => options, 103 &block 104 ) 105 106 # Now define a "new<type>" method for convenience. 107 if self.respond_to? newmethod 108 # Refuse to overwrite existing methods like 'newparam' or 'newtype'. 109 #TRANSLATORS 'new%{method}' will become a method name, do not translate this string 110 Puppet.warning(_("'new%{method}' method already exists; skipping") % { method: name.to_s }) 111 else 112 selfobj.send(:define_method, newmethod) do |*args| 113 klass.new(*args) 114 end 115 end 116 117 # If they've got all the necessary methods defined and they haven't 118 # already added the property, then do so now. 119 klass.ensurable if klass.ensurable? and ! klass.validproperty?(:ensure) 120 121 # Now set up autoload any providers that might exist for this type. 122 123 klass.providerloader = Puppet::Util::Autoload.new(klass, "puppet/provider/#{klass.name}") 124 125 # We have to load everything so that we can figure out the default provider. 126 klass.providerloader.loadall(Puppet.lookup(:current_environment)) 127 klass.providify unless klass.providers.empty? 128 129 loc = block_given? ? block.source_location : nil 130 uri = loc.nil? ? nil : URI("#{Puppet::Util.path_to_uri(loc[0])}?line=#{loc[1]}") 131 Puppet::Pops::Loaders.register_runtime3_type(name, uri) 132 133 klass 134 end 135 end
Removes an existing type. @note Only use this for testing. @api private
# File lib/puppet/metatype/manager.rb 140 def rmtype(name) 141 # Then create the class. 142 143 rmclass(name, :hash => @types) 144 145 singleton_class.send(:remove_method, "new#{name}") if respond_to?("new#{name}") 146 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 153 def type(name) 154 @manager_lock.synchronize do 155 # Avoid loading if name obviously is not a type name 156 if name.to_s.include?(':') 157 return nil 158 end 159 160 # We are overwhelmingly symbols here, which usually match, so it is worth 161 # having this special-case to return quickly. Like, 25K symbols vs. 300 162 # strings in this method. --daniel 2012-07-17 163 return @types[name] if @types.include? name 164 165 # Try mangling the name, if it is a string. 166 if name.is_a? String 167 name = name.downcase.intern 168 return @types[name] if @types.include? name 169 end 170 # Try loading the type. 171 if typeloader.load(name, Puppet.lookup(:current_environment)) 172 #TRANSLATORS 'puppet/type/%{name}' should not be translated 173 Puppet.warning(_("Loaded puppet/type/%{name} but no class was created") % { name: name }) unless @types.include? name 174 elsif !Puppet[:always_retry_plugins] 175 # PUP-5482 - Only look for a type once if plugin retry is disabled 176 @types[name] = nil 177 end 178 179 # ...and I guess that is that, eh. 180 return @types[name] 181 end 182 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 188 def typeloader 189 unless defined?(@typeloader) 190 @typeloader = Puppet::Util::Autoload.new(self, "puppet/type") 191 end 192 193 @typeloader 194 end