Method: Puppet::MetaType::Manager#newtype

Defined in:
lib/puppet/metatype/manager.rb

#newtype(name, options = {}) {|| ... } ⇒ Class<inherits Puppet::Type>

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).

Parameters:

  • name (String)

    the name of the type to create or redefine.

  • options (Hash) (defaults to: {})

    options passed on to Util::ClassGen#genclass as the option ‘:attributes`.

Options Hash (options):

  • Puppet::Type. (Puppet::Type)

    This option is not passed on as an attribute to genclass.

Yields:

  • ()

    a block evaluated in the context of the created class, thus allowing further detailing of that class.

Returns:

See Also:

  • Util::ClassGen.genclass
[View source]

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/puppet/metatype/manager.rb', line 74

def newtype(name, options = {}, &block)
  @manager_lock.synchronize do
    # Handle backward compatibility
    unless options.is_a?(Hash)
      # TRANSLATORS 'Puppet::Type.newtype' should not be translated
      Puppet.warning(_("Puppet::Type.newtype(%{name}) now expects a hash as the second argument, not %{argument}") %
                     { name: name, argument: options.inspect })
    end

    # First make sure we don't have a method sitting around
    name = name.intern
    newmethod = "new#{name}"

    # Used for method manipulation.
    selfobj = singleton_class

    if @types.include?(name)
      if respond_to?(newmethod)
        # Remove the old newmethod
        selfobj.send(:remove_method, newmethod)
      end
    end

    # Then create the class.

    klass = genclass(
      name,
      :parent => Puppet::Type,
      :overwrite => true,
      :hash => @types,
      :attributes => options,
      &block
    )

    # Now define a "new<type>" method for convenience.
    if respond_to? newmethod
      # Refuse to overwrite existing methods like 'newparam' or 'newtype'.
      # TRANSLATORS 'new%{method}' will become a method name, do not translate this string
      Puppet.warning(_("'new%{method}' method already exists; skipping") % { method: name.to_s })
    else
      selfobj.send(:define_method, newmethod) do |*args|
        klass.new(*args)
      end
    end

    # If they've got all the necessary methods defined and they haven't
    # already added the property, then do so now.
    klass.ensurable if klass.ensurable? and !klass.validproperty?(:ensure)

    # Now set up autoload any providers that might exist for this type.

    klass.providerloader = Puppet::Util::Autoload.new(klass, "puppet/provider/#{klass.name}")

    # We have to load everything so that we can figure out the default provider.
    klass.providerloader.loadall(Puppet.lookup(:current_environment))
    klass.providify unless klass.providers.empty?

    loc = block_given? ? block.source_location : nil
    uri = loc.nil? ? nil : URI("#{Puppet::Util.path_to_uri(loc[0])}?line=#{loc[1]}")
    Puppet::Pops::Loaders.register_runtime3_type(name, uri)

    klass
  end
end