Module: Puppet::Util::ClassGen

Includes:
Puppet::Util
Included in:
MetaType::Manager, Reports, Type, FileType, Log
Defined in:
lib/puppet/util/classgen.rb

Overview

This is a utility module for generating classes.

Constant Summary

Constants included from Puppet::Util

AbsolutePathPosix, AbsolutePathWindows, DEFAULT_POSIX_MODE, DEFAULT_WINDOWS_MODE, RFC_3986_URI_REGEX

Constants included from POSIX

POSIX::LOCALE_ENV_VARS, POSIX::USER_ENV_VARS

Constants included from SymbolicFileMode

SymbolicFileMode::SetGIDBit, SymbolicFileMode::SetUIDBit, SymbolicFileMode::StickyBit, SymbolicFileMode::SymbolicMode, SymbolicFileMode::SymbolicSpecialToBit

Instance Method Summary collapse

Methods included from Puppet::Util

absolute_path?, benchmark, chuser, clear_environment, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, set_env, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, which, withenv, withumask

Methods included from POSIX

#get_posix_field, #gid, groups_of, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Instance Method Details

#genclass(name, options = {}, &block) ⇒ Class

Create a new class.

Parameters:

  • name (String)

    the name of the generated class

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

    a hash of options

Options Hash (options):

  • :array (Array<Class>)

    if specified, the generated class is appended to this array

  • :attributes (Hash<{String => Object}>)

    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.

  • :block (Proc)

    a block to evaluate in the context of the class (this block can be provided this way, or as a normal yield block).

  • :constant (String) — default: name with first letter capitalized

    what to set the constant that references the generated class to.

  • :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`.

  • :overwrite (Boolean)

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

  • :parent (Class) — default: self

    the parent class of the generated class.

  • ('') (String)

    :prefix the constant prefix to prepend to the constant name referencing the generated class.

Returns:

  • (Class)

    the generated class



32
33
34
# File 'lib/puppet/util/classgen.rb', line 32

def genclass(name, options = {}, &block)
  genthing(name, Class, options, block)
end

#genmodule(name, options = {}, &block) ⇒ Module

Creates a new module.

Parameters:

  • name (String)

    the name of the generated module

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

    hash with options

Options Hash (options):

  • :array (Array<Class>)

    if specified, the generated class is appended to this array

  • :attributes (Hash<{String => Object}>)

    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.

  • :block (Proc)

    a block to evaluate in the context of the class (this block can be provided this way, or as a normal yield block).

  • :constant (String) — default: name with first letter capitalized

    what to set the constant that references the generated class to.

  • :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`.

  • :overwrite (Boolean)

    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.

  • ('') (String)

    :prefix the constant prefix to prepend to the constant name referencing the generated class.

Returns:

  • (Module)

    the generated Module



55
56
57
# File 'lib/puppet/util/classgen.rb', line 55

def genmodule(name, options = {}, &block)
  genthing(name, Module, options, block)
end

#rmclass(name, options) ⇒ Boolean

Removes an existing class.

Parameters:

  • name (String)

    the name of the class to remove

  • options (Hash)

    options

Options Hash (options):

  • :hash (Hash)

    a hash of existing classes from which the class to be removed is also removed

Returns:

  • (Boolean)

    whether the class was removed or not



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/util/classgen.rb', line 65

def rmclass(name, options)
  const = genconst_string(name, options)
  retval = false
  if is_constant_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