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 SymbolicFileMode

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

Constants included from POSIX

POSIX::LOCALE_ENV_VARS, POSIX::USER_ENV_VARS

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 SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from POSIX

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

Instance Method Details

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

Create a new class.

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.


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.

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.


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.

Options Hash (options):

  • :hash (Hash)

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


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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

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

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