Module: ConstConf::SettingAccessor

Included in:
Setting
Defined in:
lib/const_conf/setting_accessor.rb

Overview

A module that provides functionality for defining setting accessors within configuration modules.

The SettingAccessor module enables the creation of dynamic getter and setter methods for configuration settings, supporting features like default values, block evaluation, and lazy initialization. It is designed to be included in classes or modules that need to manage configuration properties with ease and consistency.

Examples:

Defining a setting accessor

class MyClass
  extend ConstConf::SettingAccessor
  setting_accessor :my_setting, 'default_value'
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#setter_modeBoolean

Thread-local flag that controls behavior during configuration block evaluation.

When #setter_mode is true (during ConstConf::Setting initialization), the accessor enforces that required settings must be explicitly provided rather than falling back to defaults. This prevents accidental configuration of settings with nil values when they should be explicitly set.

Returns:

  • (Boolean)

    true when in setter mode, false otherwise

See Also:



29
# File 'lib/const_conf/setting_accessor.rb', line 29

thread_local :setter_mode, false

Instance Method Details

#enable_setter_modeObject

Enables setter mode for configuration block evaluation.

This method temporarily sets the setter_mode flag to true, which affects how setting accessors behave during configuration block evaluation. When setter mode is active, certain validation rules are applied to ensure that required settings are explicitly provided rather than falling back to defaults.

Returns:

  • (Object)

    the return value of the block passed to this method



40
41
42
43
44
45
# File 'lib/const_conf/setting_accessor.rb', line 40

def enable_setter_mode
  old, self.setter_mode = self.setter_mode, true
  yield
ensure
  self.setter_mode = old
end

#setting_accessor(name, default = nil, transform: nil) { ... } ⇒ Symbol

Defines a setting accessor method that creates a dynamic getter/setter for configuration values.

This method generates a singleton method that provides access to a configuration setting, supporting default values, block evaluation for defaults, and lazy initialization of the setting’s value. The generated method can be used to retrieve or set the value of the configuration setting, with support for different types of default values including Proc objects which are evaluated when needed.

that gets evaluated default is provided

Parameters:

  • name (Symbol)

    the name of the setting accessor to define

  • default (Object) (defaults to: nil)

    the default value for the setting, can be a Proc

Yields:

  • optional block to evaluate for default value when no explicit

Returns:

  • (Symbol)

    always returns the name as Symbol as it defines a method



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/const_conf/setting_accessor.rb', line 63

def setting_accessor(name, default = nil, transform: nil, &block)
  variable = "@#{name}"
  define_method(name) do |arg = :not_set, &arg_block|
    was_not_set = if arg.equal?(:not_set)
                    arg = nil
                    true
                  else
                    false
                  end
    if arg_block
      arg.nil? or raise ArgumentError,
        'only either block or positional argument allowed'
      arg = arg_block
    end
    if arg.nil?
      if self.class.respond_to?(:setter_mode)
        if self.class.setter_mode && was_not_set
          raise ArgumentError,
            "need an argument for the setting #{name.inspect} "\
            "of #{self}, was nil"
        end
      end
      result =
        if instance_variable_defined?(variable)
          instance_variable_get(variable)
        end
      if result.nil?
        result = if default.nil?
                   block && instance_eval(&block)
                 elsif default
                   default
                 end
        instance_variable_set(variable, result)
        result
      else
        result
      end
    else
      arg = transform.(arg) if transform
      instance_variable_set(variable, arg)
    end
  end
end