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

ALNUM, ALPHA, AbsolutePathPosix, AbsolutePathWindows, DEFAULT_POSIX_MODE, DEFAULT_WINDOWS_MODE, ESCAPED, HEX, HttpProxy, PUPPET_STACK_INSERTION_FRAME, RESERVED, RFC_3986_URI_REGEX, UNRESERVED, UNSAFE

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, create_erb, default_env, deterministic_rand, deterministic_rand_int, exit_on_fail, format_backtrace_array, format_puppetstack_frame, get_env, get_environment, logmethods, merge_environment, path_to_uri, pretty_backtrace, replace_file, resolve_stackframe, rfc2396_escape, safe_posix_fork, set_env, skip_external_facts, symbolizehash, thinmark, uri_encode, uri_query_encode, uri_to_path, uri_unescape, which, withenv, withumask

Methods included from POSIX

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

Methods included from SymbolicFileMode

#display_mode, #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



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

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



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

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



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

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