module SleepingKingStudios::Tools::Toolbox::Configuration::ClassMethods
Class methods for configuration objects.
Constants
- DEFAULT_OPTION
Public Instance Methods
Defines a nested namespace for the configuration object.
A namespace is represented by a nested configuration object, which has its own options and namespaces.
@param namespace_name [String] The name of the namespace.
@yield namespace If a block is given, that block will be executed in the
context of the newly created namespace.
@return [Configuration] the created namespace object.
# File lib/sleeping_king_studios/tools/toolbox/configuration.rb, line 25 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
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.
@param option_name [String] The name of the option. @param allow_nil [true, 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.
@param default [Object] The default value for the option. If this is not
set, the default value for the option will be nil.
@param enum [Array] An enumerable list of valid values for the option.
# File lib/sleeping_king_studios/tools/toolbox/configuration.rb, line 51 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
Private Instance Methods
# File lib/sleeping_king_studios/tools/toolbox/configuration.rb, line 73 def define_namespace(namespace_name) namespace = Class.new(SleepingKingStudios::Tools::Toolbox::Configuration) define_namespace_accessor(namespace_name, namespace) namespace end
# File lib/sleeping_king_studios/tools/toolbox/configuration.rb, line 82 def define_namespace_accessor(namespace_name, namespace_class) namespace_ivar = :"@#{namespace_name}" define_method namespace_name do |&block| if instance_variable_defined?(namespace_ivar) return instance_variable_get(namespace_ivar).tap do |config| block&.call(config) end end initialize_namespace(namespace_name, namespace_class, &block) end end
# File lib/sleeping_king_studios/tools/toolbox/configuration.rb, line 96 def define_option_accessor(option_name, options) define_method option_name do get_value(option_name, options) end end
# File lib/sleeping_king_studios/tools/toolbox/configuration.rb, line 102 def define_option_mutator(option_name, options) writer_name = :"#{option_name}=" define_method writer_name do |value| set_value(option_name, value, options) end end
# File lib/sleeping_king_studios/tools/toolbox/configuration.rb, line 110 def guard_abstract_class! return unless self == SleepingKingStudios::Tools::Toolbox::Configuration raise "can't define namespace or option on abstract class" end