module Puppet::Util::ClassGen

This is a utility module for generating classes. @api public

Public Instance Methods

genclass(name, options = {}, &block) click to toggle source

Create a new class. @param name [String] the name of the generated class @param options [Hash] a hash of options @option options [Array<Class>] :array if specified, the generated class is appended to this array @option options [Hash<{String => Object}>] :attributes a hash that is applied to the generated class

by calling setter methods corresponding to this hash's keys/value pairs. This is done before the given
block is evaluated.

@option options [Proc] :block a block to evaluate in the context of the class (this block can be provided

this way, or as a normal yield block).

@option options [String] :constant (name with first letter capitalized) what to set the constant that references

the generated class to.

@option options [Hash] :hash a hash of existing classes that this class is appended to (name => class).

This hash must be specified if the `:overwrite` option is set to `true`.

@option options [Boolean] :overwrite whether an overwrite of an existing class should be allowed (requires also

defining the `:hash` with existing classes as the test is based on the content of this hash).

@option options [Class] :parent (self) the parent class of the generated class. @option options [String] (”) :prefix the constant prefix to prepend to the constant name referencing the

generated class.

@return [Class] the generated class

# File lib/puppet/util/classgen.rb, line 33
def genclass(name, options = {}, &block)
  genthing(name, Class, options, block)
end
genmodule(name, options = {}, &block) click to toggle source

Creates a new module.

@param name [String] the name of the generated module @param optinos [Hash] hash with options @option options [Array<Class>] :array if specified, the generated class is appended to this array @option options [Hash<{String => Object}>] :attributes a hash that is applied to the generated class

by calling setter methods corresponding to this hash's keys/value pairs. This is done before the given
block is evaluated.

@option options [Proc] :block a block to evaluate in the context of the class (this block can be provided

this way, or as a normal yield block).

@option options [String] :constant (name with first letter capitalized) what to set the constant that references

the generated class to.

@option options [Hash] :hash a hash of existing classes that this class is appended to (name => class).

This hash must be specified if the `:overwrite` option is set to `true`.

@option options [Boolean] :overwrite whether an overwrite of an existing class should be allowed (requires also

defining the `:hash` with existing classes as the test is based on the content of this hash).
the capitalized name is appended and the result is set as the constant.

@option options [String] (”) :prefix the constant prefix to prepend to the constant name referencing the

generated class.

@return [Module] the generated Module

# File lib/puppet/util/classgen.rb, line 56
def genmodule(name, options = {}, &block)
  genthing(name, Module, options, block)
end
rmclass(name, options) click to toggle source

Removes an existing class. @param name [String] the name of the class to remove @param options [Hash] options @option options [Hash] :hash a hash of existing classes from which the class to be removed is also removed @return [Boolean] whether the class was removed or not

# File lib/puppet/util/classgen.rb, line 66
def rmclass(name, options)
  options = symbolize_options(options)
  const = genconst_string(name, options)
  retval = false
  if const_defined?(const)
    remove_const(const)
    retval = true
  end

  if hash = options[:hash] and hash.include? name
    hash.delete(name)
    retval = true
  end

  # Let them know whether we did actually delete a subclass.
  retval
end