Module: Puppet::Util::ClassGen

Includes:
Puppet::Util, MethodHelper
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

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, deterministic_rand, deterministic_rand_int, exit_on_fail, logmethods, path_to_uri, pretty_backtrace, replace_file, safe_posix_fork, symbolizehash, thinmark, uri_to_path, which, withenv, withumask

Methods included from POSIX

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

Methods included from SymbolicFileMode

#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?

Methods included from MethodHelper

#requiredopts, #set_options, #symbolize_options

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.



35
36
37
# File 'lib/puppet/util/classgen.rb', line 35

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.



58
59
60
# File 'lib/puppet/util/classgen.rb', line 58

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/puppet/util/classgen.rb', line 68

def rmclass(name, options)
  options = symbolize_options(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