Class: ConstConf::Setting
- Inherits:
-
Object
- Object
- ConstConf::Setting
- Extended by:
- SettingAccessor
- Includes:
- DSLKit::BlockSelf
- Defined in:
- lib/const_conf/setting.rb
Overview
A configuration setting definition that encapsulates the properties and behavior of a single environment variable-based setting.
The Setting class provides a structured way to define, validate, and retrieve configuration values from environment variables, including support for default values, required validation, decoding logic, and descriptive metadata.
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
The name reader accessor returns the name of the setting.
-
#parent_namespace ⇒ Module?
readonly
The parent_namespace reader accessor returns the parent namespace of the setting.
Attributes included from SettingAccessor
Instance Method Summary collapse
-
#activated ⇒ Object
The activated reader and writer accessor for configuration settings.
-
#active? ⇒ Boolean
Checks if the configuration setting is active based on its activated state.
-
#check ⇒ Proc, Object
Sets or retrieves the confirmation check logic for the configuration setting.
-
#checked? ⇒ Boolean, Symbol
Checks if the configuration setting passes its validation check.
-
#configured? ⇒ Boolean
Checks if the configuration setting has been configured with a value.
-
#configured_value ⇒ String?
Returns the configured value for the setting, considering ignore status and environment variable presence.
-
#configured_value_or_default_value ⇒ Object?
Returns the configured value for the setting, or falls back to the default value if not configured.
-
#confirm! ⇒ ConstConf::Setting
Confirms that the configuration setting and its parent module meet all required criteria.
-
#decode(value = nil) ⇒ Proc?
Sets or retrieves the decoding configuration for the setting.
-
#decoded_value(value_arg) ⇒ Object
private
Decodes a value using the configured decoder if present, otherwise returns the value as-is.
-
#decoding? ⇒ Boolean
True if the setting has a Proc-based decoding configuration, false otherwise.
-
#default_value ⇒ Object
Returns the default value for the configuration option, evaluating it if it’s a Proc.
-
#description ⇒ String
Sets or retrieves the description for the configuration setting.
-
#env_var ⇒ String?
Retrieves the value of the environment variable associated with this configuration option.
-
#env_var_name ⇒ String
Generates the environment variable name for this configuration option by constructing it from the configured prefix and name components, replacing namespace separators with underscores.
-
#ignored? ⇒ Boolean
Checks if the configuration setting is marked as ignored.
-
#initialize(name:, prefix: nil) { ... } ⇒ Setting
constructor
Initializes a new Setting instance with the given name and prefix.
-
#inspect ⇒ String
The inspect method returns a string representation of the object, with special handling for IRB colorization.
-
#inspect_original ⇒ Object
The original inspect method.
-
#prefix ⇒ String?
The prefix reader accessor returns the configured prefix for the setting.
-
#required(value = nil, &block) ⇒ Boolean, Proc
Sets whether the setting is required.
-
#required? ⇒ Boolean
Checks if the setting has a required value configured or as a default value.
-
#sensitive ⇒ Boolean
(also: #sensitive?)
Checks if the setting has a required value configured.
-
#to_s ⇒ String
Returns the string representation of the tree structure.
-
#value ⇒ Object
Retrieves the effective value for the configuration setting.
-
#value_provided? ⇒ Boolean
Checks if a configuration setting has been provided with a valid value.
-
#view(io: nil) ⇒ Object
Displays a textual representation of this configuration setting.
Methods included from SettingAccessor
enable_setter_mode, setting_accessor
Constructor Details
#initialize(name:, prefix: nil) { ... } ⇒ Setting
Initializes a new Setting instance with the given name and prefix.
constructing environment variable names
24 25 26 27 28 29 30 |
# File 'lib/const_conf/setting.rb', line 24 def initialize(name:, prefix: nil, &block) @parent_namespace = block_self(&block) @name = Array(name) * '::' self.prefix(prefix) block and self.class.enable_setter_mode { instance_eval(&block) } end |
Instance Attribute Details
#name ⇒ String (readonly)
The name reader accessor returns the name of the setting.
35 36 37 |
# File 'lib/const_conf/setting.rb', line 35 def name @name end |
#parent_namespace ⇒ Module? (readonly)
The parent_namespace reader accessor returns the parent namespace of the setting.
41 42 43 |
# File 'lib/const_conf/setting.rb', line 41 def parent_namespace @parent_namespace end |
Instance Method Details
#activated ⇒ Object
The activated reader and writer accessor for configuration settings.
This method provides access to the activated state of a configuration setting, which determines whether the setting should be considered active based on its current value or other conditions. It defaults to ‘:present?` of not set.
58 |
# File 'lib/const_conf/setting.rb', line 58 setting_accessor :activated, :present? |
#active? ⇒ Boolean
Checks if the configuration setting is active based on its activated state.
This method evaluates whether the configuration setting is considered active by examining its activated property. If activated is set to true, it checks if the setting’s value is present. If activated is a Symbol, it sends that symbol as a message to the value and returns the result. If activated is a Proc, it evaluates the proc with or without the value depending on its arity. For any other value, it returns false.
state, false otherwise
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/const_conf/setting.rb', line 71 def active? case activated when true value.present? when Symbol !!value.send(activated) when Proc if activated.arity == 1 !!activated.(value) else !!activated.() end else false end end |
#check ⇒ Proc, Object
Sets or retrieves the confirmation check logic for the configuration setting.
This method provides access to the check configuration, which is used to validate that a setting meets certain criteria beyond basic required and default value checks. The check can be a Proc that evaluates to true, :unchecked_true (truthy), or false, allowing for custom validation logic. It deffaults to true, if not set otherwise.
98 |
# File 'lib/const_conf/setting.rb', line 98 setting_accessor :check, -> setting { :unchecked_true } |
#checked? ⇒ Boolean, Symbol
Checks if the configuration setting passes its validation check.
true, # false or false if not. I no check was defined, returns :unchecked_true. @see check
105 106 107 |
# File 'lib/const_conf/setting.rb', line 105 def checked? instance_eval(&check) end |
#configured? ⇒ Boolean
Checks if the configuration setting has been configured with a value.
This method determines whether the configuration setting has been provided with a value, either through an environment variable or a default value. It returns true if a valid value is present, and false otherwise.
265 266 267 |
# File 'lib/const_conf/setting.rb', line 265 def configured? !configured_value.nil? end |
#configured_value ⇒ String?
Returns the configured value for the setting, considering ignore status and environment variable presence.
ignored and the environment variable is set, nil otherwise
247 248 249 250 251 252 253 254 255 |
# File 'lib/const_conf/setting.rb', line 247 def configured_value if ignored nil elsif env_var.nil? nil else env_var end end |
#configured_value_or_default_value ⇒ Object?
Returns the configured value for the setting, or falls back to the default value if not configured.
default value, or nil if neither is set
274 275 276 |
# File 'lib/const_conf/setting.rb', line 274 def configured_value_or_default_value configured_value || default_value end |
#confirm! ⇒ ConstConf::Setting
Confirms that the configuration setting and its parent module meet all required criteria.
This method ensures that the setting has a description, that its parent module (if it’s a ConstConf module) has a description, that any required values are provided, and that the setting passes its confirmation check. It raises appropriate exceptions if any of these validation rules are not satisfied.
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/const_conf/setting.rb', line 308 def confirm! if parent_namespace.is_a?(Module) && parent_namespace < ConstConf parent_namespace.description.present? or raise ConstConf::RequiredDescriptionNotConfigured, "required description for ConstConf module #{parent_namespace} not configured" end if description.blank? raise ConstConf::RequiredDescriptionNotConfigured, "required description for setting #{env_var_name} not configured" end if required? && !value_provided? raise ConstConf::RequiredValueNotConfigured, "required value for #{env_var_name} not configured" end unless checked? raise ConstConf::SettingCheckFailed, "check failed for #{name} setting" end self end |
#decode(value = nil) ⇒ Proc?
Sets or retrieves the decoding configuration for the setting.
175 |
# File 'lib/const_conf/setting.rb', line 175 setting_accessor :decode |
#decoded_value(value_arg) ⇒ Object (private)
Decodes a value using the configured decoder if present, otherwise returns the value as-is.
configured
378 379 380 381 382 383 384 |
# File 'lib/const_conf/setting.rb', line 378 def decoded_value(value_arg) if decode.is_a?(Proc) decode.(value_arg) else value_arg end end |
#decoding? ⇒ Boolean
Returns true if the setting has a Proc-based decoding configuration, false otherwise.
181 182 183 |
# File 'lib/const_conf/setting.rb', line 181 def decoding? !!decode&.is_a?(Proc) end |
#default_value ⇒ Object
Returns the default value for the configuration option, evaluating it if it’s a Proc.
default if it’s a Proc
196 197 198 199 200 201 202 203 |
# File 'lib/const_conf/setting.rb', line 196 def default_value case default when Proc default.() else default end end |
#description ⇒ String
Sets or retrieves the description for the configuration setting.
165 |
# File 'lib/const_conf/setting.rb', line 165 setting_accessor :description |
#env_var ⇒ String?
Retrieves the value of the environment variable associated with this configuration option.
or nil if not set
238 239 240 |
# File 'lib/const_conf/setting.rb', line 238 def env_var ENV[env_var_name] end |
#env_var_name ⇒ String
Generates the environment variable name for this configuration option by constructing it from the configured prefix and name components, replacing namespace separators with underscores.
228 229 230 231 |
# File 'lib/const_conf/setting.rb', line 228 def env_var_name prefix = @prefix.full? { "#{_1}::" }.to_s name.sub(/^#{parent_namespace}::/, prefix).gsub(/::/, ?_) end |
#ignored? ⇒ Boolean
Checks if the configuration setting is marked as ignored.
This method returns true if the setting has been explicitly marked to be ignored when reading values from environment variables, indicating that it should be skipped during configuration processing.
219 220 221 |
# File 'lib/const_conf/setting.rb', line 219 def ignored? !!ignored end |
#inspect ⇒ String
The inspect method returns a string representation of the object, with special handling for IRB colorization.
363 364 365 366 367 368 369 |
# File 'lib/const_conf/setting.rb', line 363 def inspect if defined?(IRB) && IRB.conf[:USE_COLORIZE] Term::ANSIColor.uncolor(to_s) else to_s end end |
#inspect_original ⇒ Object
The original inspect method.
355 |
# File 'lib/const_conf/setting.rb', line 355 alias inspect_original inspect |
#prefix ⇒ String?
The prefix reader accessor returns the configured prefix for the setting.
46 47 |
# File 'lib/const_conf/setting.rb', line 46 setting_accessor :prefix, transform: -> value { value.ask_and_send_or_self(:upcase) } |
#required(value = nil, &block) ⇒ Boolean, Proc
Sets whether the setting is required.
129 |
# File 'lib/const_conf/setting.rb', line 129 setting_accessor :required, false |
#required? ⇒ Boolean
Checks if the setting has a required value configured or as a default value.
This method evaluates whether the configuration setting is marked as required and determines if a valid value is present. It handles different forms of required specification including boolean flags and Proc objects that can perform dynamic validation based on the current value or context.
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/const_conf/setting.rb', line 141 def required? !!case required when Proc if required.arity == 1 required.(configured_value_or_default_value) else required.() end else required end end |
#sensitive ⇒ Boolean Also known as: sensitive?
Checks if the setting has a required value configured.
value, false otherwise
158 |
# File 'lib/const_conf/setting.rb', line 158 setting_accessor :sensitive, false |
#to_s ⇒ String
Returns the string representation of the tree structure.
This method generates a formatted string representation of the tree node and its children, including the node’s name and any associated value, with proper indentation to show the hierarchical relationship between nodes.
348 349 350 351 352 |
# File 'lib/const_conf/setting.rb', line 348 def to_s io = StringIO.new view(io:) io.string end |
#value ⇒ Object
Retrieves the effective value for the configuration setting.
This method determines the appropriate value for the configuration setting by first checking if the setting is not ignored and an environment variable value is present. If so, it uses the environment variable value. Otherwise, it falls back to the default value. The resulting value is then passed through any configured decoding logic.
288 289 290 |
# File 'lib/const_conf/setting.rb', line 288 def value decoded_value(configured_value_or_default_value) end |
#value_provided? ⇒ Boolean
Checks if a configuration setting has been provided with a valid value.
value or a default value that is not nil, false otherwise
113 114 115 |
# File 'lib/const_conf/setting.rb', line 113 def value_provided? !configured_value_or_default_value.nil? end |
#view(io: nil) ⇒ Object
Displays a textual representation of this configuration setting.
This method generates a formatted tree-like view of the current configuration setting, including its name, description, environment variable name, and associated metadata such as prefix, default value, and configuration status. The output can be directed to a specified IO object or displayed to the standard output.
337 338 339 |
# File 'lib/const_conf/setting.rb', line 337 def view(io: nil) parent_namespace.view(object: self, io:) end |