Module: SleepingKingStudios::Tools::Toolbox::Configuration::ClassMethods

Included in:
SleepingKingStudios::Tools::Toolbox::Configuration
Defined in:
lib/sleeping_king_studios/tools/toolbox/configuration.rb

Overview

Class methods for configuration objects.

Constant Summary collapse

DEFAULT_OPTION =
Object.new.freeze

Instance Method Summary collapse

Instance Method Details

#namespace(namespace_name) { ... } ⇒ Configuration

Defines a nested namespace for the configuration object.

A namespace is represented by a nested configuration object, which has its own options and namespaces.

Parameters:

  • namespace_name (String)

    The name of the namespace.

Yields:

  • namespace If a block is given, that block will be executed in the context of the newly created namespace.

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sleeping_king_studios/tools/toolbox/configuration.rb', line 24

def namespace(namespace_name, &block)
  guard_abstract_class!

  namespace =
    (@namespaces ||= {}).fetch(namespace_name) do
      @namespaces[namespace_name] = define_namespace namespace_name
    end

  namespace.instance_exec(namespace, &block) if block_given?

  namespace
end

#option(option_name, allow_nil: false, default: DEFAULT_OPTION, enum: nil) ⇒ Object

Defines an option for the configuration object.

A configuration option has a name and a value. It can be defined with a default value, or to allow or prohibit nil values or restrict possible values to a given set.

Parameters:

  • option_name (String)

    The name of the option.

  • allow_nil (true, false) (defaults to: false)

    If false, setting the option value to nil or an empty value will raise an error, as will trying to access the value when it has not been set. Defaults to false.

  • default (Object) (defaults to: DEFAULT_OPTION)

    The default value for the option. If this is not set, the default value for the option will be nil.

  • enum (Array) (defaults to: nil)

    An enumerable list of valid values for the option.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sleeping_king_studios/tools/toolbox/configuration.rb', line 50

def option(
  option_name,
  allow_nil: false,
  default:   DEFAULT_OPTION,
  enum:      nil
)
  guard_abstract_class!

  options = {
    allow_nil: allow_nil,
    default:   default,
    enum:      enum
  }

  define_option_accessor option_name, options
  define_option_mutator  option_name, options

  option_name.intern
end