const_conf 0.1.2 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +42 -0
- data/README.md +70 -0
- data/const_conf.gemspec +4 -4
- data/lib/const_conf/dir_plugin.rb +12 -6
- data/lib/const_conf/env_dir_extension.rb +4 -2
- data/lib/const_conf/file_plugin.rb +2 -1
- data/lib/const_conf/setting.rb +11 -9
- data/lib/const_conf/setting_accessor.rb +6 -3
- data/lib/const_conf/spec/const_conf_helper.rb +37 -0
- data/lib/const_conf/spec.rb +2 -0
- data/lib/const_conf/version.rb +1 -1
- data/lib/const_conf.rb +49 -31
- metadata +6 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f4332160b0b98f289ef65141551d279cdd3b24e7ff8e0187c04c1c9351ec645
|
4
|
+
data.tar.gz: 1d63082f8583882e65a772a0122b52613eea67eaecc70ccab49fe44c8efc0e8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 241953d4a3f7c6265134bd300a5847a0232b59a8e4a8a62526344f34334e510a422ba313da10816b15bc9d9356f35c77a5b05164c517655bc3f335427bff460d
|
7
|
+
data.tar.gz: 7d30537f86f8828455ba14a3cd08741d162e15fcf1821484743bcf092cb6982e7e4b509e67b8a0b713f9e5b7fcd92d0f03fd810c0c2de288b8cbe8b46f45b12f
|
data/CHANGES.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Changes
|
2
|
+
|
3
|
+
## 2025-09-07 v0.2.1
|
4
|
+
|
5
|
+
- Removed explicit boolean coercion previously applied to test values in
|
6
|
+
predicate methods
|
7
|
+
|
8
|
+
## 2025-09-07 v0.2.0
|
9
|
+
|
10
|
+
- Implemented `ConstConf::ConstConfHelper` module in
|
11
|
+
`lib/const_conf/spec/const_conf_helper.rb`
|
12
|
+
- Added comprehensive testing chapter to README.md explaining usage of
|
13
|
+
`const_conf_as` helper
|
14
|
+
- Helper supports nested module constants and predicate method mocking
|
15
|
+
- Created `lib/const_conf/spec.rb` require file for testing utilities
|
16
|
+
|
17
|
+
## 2025-09-02 v0.1.2
|
18
|
+
|
19
|
+
- Added bold formatting to value display in tree output
|
20
|
+
|
21
|
+
## 2025-09-01 v0.1.1
|
22
|
+
|
23
|
+
- Enables usage in both Rails and non-Rails applications
|
24
|
+
- Maintains Rails integration when Rails is present
|
25
|
+
- Uses more precise dependency on Active Support rather than full Rails
|
26
|
+
framework
|
27
|
+
|
28
|
+
## 2025-08-30 v0.1.0
|
29
|
+
|
30
|
+
- Added `nested_module_constants` tracking set to maintain definition order
|
31
|
+
- Updated `each_nested_configuration` to use depth-first search (DFS) with
|
32
|
+
correct ordering
|
33
|
+
- Implemented override for `remove_const` method to keep tracking consistent
|
34
|
+
- Added comprehensive tests for nested configuration ordering
|
35
|
+
- Updated tree display implementation to use `nested_module_constants` instead
|
36
|
+
of `constants.sort`
|
37
|
+
- Added comprehensive documentation and improved JSON/YAML plugin
|
38
|
+
implementations
|
39
|
+
|
40
|
+
## 2025-08-28 v0.0.0
|
41
|
+
|
42
|
+
* Start
|
data/README.md
CHANGED
@@ -762,6 +762,76 @@ ConstConf automatically integrates with Rails:
|
|
762
762
|
- Configuration is reloaded when the application prepares configuration
|
763
763
|
- Works seamlessly with Rails environment variables
|
764
764
|
|
765
|
+
## Testing with ConstConf
|
766
|
+
|
767
|
+
ConstConf provides built-in testing utilities to make it easy to test your
|
768
|
+
configuration in isolation.
|
769
|
+
|
770
|
+
### Using `const_conf_as` Helper
|
771
|
+
|
772
|
+
The `ConstConf::ConstConfHelper` provides a convenient way to temporarily
|
773
|
+
override constant values during testing:
|
774
|
+
|
775
|
+
```ruby
|
776
|
+
# In your test file
|
777
|
+
require 'const_conf/spec'
|
778
|
+
|
779
|
+
RSpec.describe "MyApp" do
|
780
|
+
include ConstConf::ConstConfHelper
|
781
|
+
|
782
|
+
it "works with overridden configuration" do
|
783
|
+
const_conf_as(
|
784
|
+
'AppConfig::DATABASE_URL' => 'postgresql://test:pass@localhost/test',
|
785
|
+
'AppConfig::API_KEY' => 'test-key-123'
|
786
|
+
)
|
787
|
+
|
788
|
+
# Now your tests can use the overridden values
|
789
|
+
expect(AppConfig::DATABASE_URL).to eq('postgresql://test:pass@localhost/test')
|
790
|
+
expect(AppConfig::API_KEY).to eq('test-key-123')
|
791
|
+
end
|
792
|
+
end
|
793
|
+
```
|
794
|
+
|
795
|
+
To make the helper available throughout your test suite, add this to your
|
796
|
+
`spec/spec_helper.rb` or `rails_helper.rb`:
|
797
|
+
|
798
|
+
```ruby
|
799
|
+
require 'const_conf/spec'
|
800
|
+
|
801
|
+
# spec/spec_helper.rb or spec/rails_helper.rb
|
802
|
+
RSpec.configure do |config|
|
803
|
+
config.include ConstConf::ConstConfHelper
|
804
|
+
end
|
805
|
+
```
|
806
|
+
|
807
|
+
### Testing Nested Modules
|
808
|
+
|
809
|
+
The helper also works with nested configuration modules:
|
810
|
+
|
811
|
+
```ruby
|
812
|
+
const_conf_as(
|
813
|
+
'AppConfig::Database::URL' => 'postgresql://test:pass@localhost/test',
|
814
|
+
'AppConfig::Database::ENABLED' => true
|
815
|
+
)
|
816
|
+
```
|
817
|
+
|
818
|
+
This automatically sets up the predicate methods (`?`) for nested constants, so
|
819
|
+
you can test both the values and their active status:
|
820
|
+
|
821
|
+
```ruby
|
822
|
+
expect(AppConfig::Database::URL?).to be_truthy
|
823
|
+
expect(AppConfig::Database::ENABLED?).to be true
|
824
|
+
```
|
825
|
+
|
826
|
+
The helper ensures that:
|
827
|
+
- Constants exist before attempting to override them
|
828
|
+
- Parent modules are properly handled for nested constants
|
829
|
+
- Predicate methods are correctly mocked for boolean values
|
830
|
+
- Proper error handling is in place for missing constants
|
831
|
+
|
832
|
+
This approach makes testing configuration-dependent code much easier and more
|
833
|
+
reliable than relying on environment variables or manual setup.
|
834
|
+
|
765
835
|
## Debugging and Inspection
|
766
836
|
|
767
837
|
View configuration hierarchies:
|
data/const_conf.gemspec
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
# stub: const_conf 0.1
|
2
|
+
# stub: const_conf 0.2.1 ruby lib
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "const_conf".freeze
|
6
|
-
s.version = "0.1
|
6
|
+
s.version = "0.2.1".freeze
|
7
7
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
9
9
|
s.require_paths = ["lib".freeze]
|
@@ -11,8 +11,8 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.date = "1980-01-02"
|
12
12
|
s.description = "ConstConf is a Ruby configuration library that manages settings\nthrough environment variables, files, and directories with comprehensive\nvalidation and Rails integration.\n".freeze
|
13
13
|
s.email = "flori@ping.de".freeze
|
14
|
-
s.extra_rdoc_files = ["README.md".freeze, "lib/const_conf.rb".freeze, "lib/const_conf/dir_plugin.rb".freeze, "lib/const_conf/env_dir_extension.rb".freeze, "lib/const_conf/errors.rb".freeze, "lib/const_conf/file_plugin.rb".freeze, "lib/const_conf/json_plugin.rb".freeze, "lib/const_conf/railtie.rb".freeze, "lib/const_conf/setting.rb".freeze, "lib/const_conf/setting_accessor.rb".freeze, "lib/const_conf/tree.rb".freeze, "lib/const_conf/version.rb".freeze, "lib/const_conf/yaml_plugin.rb".freeze]
|
15
|
-
s.files = ["Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "const_conf.gemspec".freeze, "lib/const_conf.rb".freeze, "lib/const_conf/dir_plugin.rb".freeze, "lib/const_conf/env_dir_extension.rb".freeze, "lib/const_conf/errors.rb".freeze, "lib/const_conf/file_plugin.rb".freeze, "lib/const_conf/json_plugin.rb".freeze, "lib/const_conf/railtie.rb".freeze, "lib/const_conf/setting.rb".freeze, "lib/const_conf/setting_accessor.rb".freeze, "lib/const_conf/tree.rb".freeze, "lib/const_conf/version.rb".freeze, "lib/const_conf/yaml_plugin.rb".freeze, "spec/assets/.env/API_KEY".freeze, "spec/assets/config.json".freeze, "spec/assets/config.yml".freeze, "spec/assets/config_env.yml".freeze, "spec/const_conf/dir_plugin_spec.rb".freeze, "spec/const_conf/env_dir_extension_spec.rb".freeze, "spec/const_conf/file_plugin_spec.rb".freeze, "spec/const_conf/json_plugin_spec.rb".freeze, "spec/const_conf/setting_accessor_spec.rb".freeze, "spec/const_conf/setting_spec.rb".freeze, "spec/const_conf/tree_spec.rb".freeze, "spec/const_conf/yaml_plugin_spec.rb".freeze, "spec/const_conf_spec.rb".freeze, "spec/spec_helper.rb".freeze]
|
14
|
+
s.extra_rdoc_files = ["README.md".freeze, "lib/const_conf.rb".freeze, "lib/const_conf/dir_plugin.rb".freeze, "lib/const_conf/env_dir_extension.rb".freeze, "lib/const_conf/errors.rb".freeze, "lib/const_conf/file_plugin.rb".freeze, "lib/const_conf/json_plugin.rb".freeze, "lib/const_conf/railtie.rb".freeze, "lib/const_conf/setting.rb".freeze, "lib/const_conf/setting_accessor.rb".freeze, "lib/const_conf/spec.rb".freeze, "lib/const_conf/spec/const_conf_helper.rb".freeze, "lib/const_conf/tree.rb".freeze, "lib/const_conf/version.rb".freeze, "lib/const_conf/yaml_plugin.rb".freeze]
|
15
|
+
s.files = ["CHANGES.md".freeze, "Gemfile".freeze, "LICENSE".freeze, "README.md".freeze, "Rakefile".freeze, "const_conf.gemspec".freeze, "lib/const_conf.rb".freeze, "lib/const_conf/dir_plugin.rb".freeze, "lib/const_conf/env_dir_extension.rb".freeze, "lib/const_conf/errors.rb".freeze, "lib/const_conf/file_plugin.rb".freeze, "lib/const_conf/json_plugin.rb".freeze, "lib/const_conf/railtie.rb".freeze, "lib/const_conf/setting.rb".freeze, "lib/const_conf/setting_accessor.rb".freeze, "lib/const_conf/spec.rb".freeze, "lib/const_conf/spec/const_conf_helper.rb".freeze, "lib/const_conf/tree.rb".freeze, "lib/const_conf/version.rb".freeze, "lib/const_conf/yaml_plugin.rb".freeze, "spec/assets/.env/API_KEY".freeze, "spec/assets/config.json".freeze, "spec/assets/config.yml".freeze, "spec/assets/config_env.yml".freeze, "spec/const_conf/dir_plugin_spec.rb".freeze, "spec/const_conf/env_dir_extension_spec.rb".freeze, "spec/const_conf/file_plugin_spec.rb".freeze, "spec/const_conf/json_plugin_spec.rb".freeze, "spec/const_conf/setting_accessor_spec.rb".freeze, "spec/const_conf/setting_spec.rb".freeze, "spec/const_conf/tree_spec.rb".freeze, "spec/const_conf/yaml_plugin_spec.rb".freeze, "spec/const_conf_spec.rb".freeze, "spec/spec_helper.rb".freeze]
|
16
16
|
s.homepage = "https://github.com/flori/const_conf".freeze
|
17
17
|
s.licenses = ["MIT".freeze]
|
18
18
|
s.rdoc_options = ["--title".freeze, "ConstConf - Clean DSL for config settings with validation and Rails integration".freeze, "--main".freeze, "README.md".freeze]
|
@@ -51,9 +51,11 @@ module ConstConf::DirPlugin
|
|
51
51
|
# configuration.
|
52
52
|
#
|
53
53
|
# @param name [String] the name used to derive the directory path
|
54
|
-
# @param root_path [String, nil] the root path to use for deriving the
|
54
|
+
# @param root_path [String, nil] the root path to use for deriving the
|
55
|
+
# directory path
|
55
56
|
# @param env_var [String, nil] the environment variable value to use
|
56
|
-
# @param env_var_name [String, nil] the name of the environment variable to
|
57
|
+
# @param env_var_name [String, nil] the name of the environment variable to
|
58
|
+
# look up
|
57
59
|
#
|
58
60
|
# @raise [ArgumentError] if env_var and env_var_name were given.
|
59
61
|
def initialize(name, root_path: nil, env_var:, env_var_name: nil)
|
@@ -129,8 +131,10 @@ module ConstConf::DirPlugin
|
|
129
131
|
# Derives the full directory path by combining the root path and the given
|
130
132
|
# name.
|
131
133
|
#
|
132
|
-
# @param name [ String ] the name of the directory to be appended to the
|
133
|
-
#
|
134
|
+
# @param name [ String ] the name of the directory to be appended to the
|
135
|
+
# root path
|
136
|
+
# @param root_path [ String, nil ] the root path to use; if nil, the
|
137
|
+
# default root path is used
|
134
138
|
#
|
135
139
|
# @return [ Pathname ] the combined directory path as a Pathname object
|
136
140
|
def derive_directory_path(name, root_path)
|
@@ -163,9 +167,11 @@ module ConstConf::DirPlugin
|
|
163
167
|
#
|
164
168
|
# @param name [String] the name of the configuration directory
|
165
169
|
# @param path [String] the filesystem path to the directory
|
166
|
-
# @param env_var [String, nil] the environment variable name to use for
|
170
|
+
# @param env_var [String, nil] the environment variable name to use for
|
171
|
+
# configuration
|
167
172
|
# @param env_var_name [String, nil] the full environment variable name to use
|
168
|
-
# @param default [Object] the default value to use when no configuration is
|
173
|
+
# @param default [Object] the default value to use when no configuration is
|
174
|
+
# provided
|
169
175
|
# @param required [Boolean] whether the directory path is required to exist
|
170
176
|
#
|
171
177
|
# @return [Object] the result of reading path from the directory name
|
@@ -53,8 +53,10 @@ module ConstConf::EnvDirExtension
|
|
53
53
|
# the filename and configures them as required and sensitive settings with
|
54
54
|
# chomped values.
|
55
55
|
#
|
56
|
-
# @param globs [Array<String>] glob patterns to match files containing
|
57
|
-
#
|
56
|
+
# @param globs [Array<String>] glob patterns to match files containing
|
57
|
+
# environment variables
|
58
|
+
# @yield [ binding ] yields the binding of the caller to allow evaluation in
|
59
|
+
# the correct context
|
58
60
|
# @yieldparam binding [Binding] the binding to evaluate constants in
|
59
61
|
def load_dotenv_dir(*globs, &block)
|
60
62
|
block or raise ArgumentError, '&block argument is required'
|
@@ -15,7 +15,8 @@ module ConstConf::FilePlugin
|
|
15
15
|
# RequiredValueNotConfigured exception is raised.
|
16
16
|
#
|
17
17
|
# @param path [String] the filesystem path to the file to be read
|
18
|
-
# @param required [Boolean] whether the file is required to exist, defaults
|
18
|
+
# @param required [Boolean] whether the file is required to exist, defaults
|
19
|
+
# to false
|
19
20
|
#
|
20
21
|
# @return [String, nil] the content of the file if it exists, or nil if it
|
21
22
|
# doesn't and required is false
|
data/lib/const_conf/setting.rb
CHANGED
@@ -99,9 +99,9 @@ class ConstConf::Setting
|
|
99
99
|
|
100
100
|
# Checks if the configuration setting passes its validation check.
|
101
101
|
#
|
102
|
-
# @return [Boolean, Symbol] true if the setting's check logic evaluates to
|
103
|
-
# false or false if not. I no check was defined, returns
|
104
|
-
# @see check
|
102
|
+
# @return [Boolean, Symbol] true if the setting's check logic evaluates to
|
103
|
+
# true, # false or false if not. I no check was defined, returns
|
104
|
+
# :unchecked_true. @see check
|
105
105
|
def checked?
|
106
106
|
instance_eval(&check)
|
107
107
|
end
|
@@ -119,8 +119,10 @@ class ConstConf::Setting
|
|
119
119
|
# @param value [Boolean, Proc] the value to set for the required flag
|
120
120
|
# - true/false: Simple boolean requirement check
|
121
121
|
# - Proc: Dynamic validation logic that can be evaluated in two ways:
|
122
|
-
# * With arity 1: Called with the setting's value (e.g., `->(value) {
|
123
|
-
#
|
122
|
+
# * With arity 1: Called with the setting's value (e.g., `->(value) {
|
123
|
+
# value.present? }`)
|
124
|
+
# * With arity 0: Called without arguments (e.g., `-> {
|
125
|
+
# some_value.present? }`)
|
124
126
|
# @return [Boolean, Proc] returns the value that was set
|
125
127
|
# @method required(value = nil, &block)
|
126
128
|
# @see #required?
|
@@ -129,10 +131,10 @@ class ConstConf::Setting
|
|
129
131
|
# Checks if the setting has a required value configured or as a default
|
130
132
|
# value.
|
131
133
|
#
|
132
|
-
# This method evaluates whether the configuration setting is marked as
|
133
|
-
# and determines if a valid value is present. It handles different
|
134
|
-
# required specification including boolean flags and Proc objects
|
135
|
-
# perform dynamic validation based on the current value or context.
|
134
|
+
# This method evaluates whether the configuration setting is marked as
|
135
|
+
# required and determines if a valid value is present. It handles different
|
136
|
+
# forms of required specification including boolean flags and Proc objects
|
137
|
+
# that can perform dynamic validation based on the current value or context.
|
136
138
|
#
|
137
139
|
# @return [Boolean] true if the setting is marked as required and has a valid
|
138
140
|
# value according to its validation logic, false otherwise
|
@@ -55,8 +55,10 @@ module ConstConf::SettingAccessor
|
|
55
55
|
# objects which are evaluated when needed.
|
56
56
|
#
|
57
57
|
# @param name [Symbol] the name of the setting accessor to define
|
58
|
-
# @param default [Object] the default value for the setting, can be a Proc
|
59
|
-
#
|
58
|
+
# @param default [Object] the default value for the setting, can be a Proc
|
59
|
+
# that gets evaluated
|
60
|
+
# @yield [] optional block to evaluate for default value when no explicit
|
61
|
+
# default is provided
|
60
62
|
# @return [Symbol] always returns the name as Symbol as it defines a method
|
61
63
|
def setting_accessor(name, default = nil, transform: nil, &block)
|
62
64
|
variable = "@#{name}"
|
@@ -76,7 +78,8 @@ module ConstConf::SettingAccessor
|
|
76
78
|
if self.class.respond_to?(:setter_mode)
|
77
79
|
if self.class.setter_mode && was_not_set
|
78
80
|
raise ArgumentError,
|
79
|
-
"need an argument for the setting #{name.inspect}
|
81
|
+
"need an argument for the setting #{name.inspect} "\
|
82
|
+
"of #{self}, was nil"
|
80
83
|
end
|
81
84
|
end
|
82
85
|
result =
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# Sets constants and their configured values for testing purposes.
|
2
|
+
#
|
3
|
+
# This module provides a helper method for temporarily overriding constant
|
4
|
+
# values during testing, including nested constants within modules. It ensures
|
5
|
+
# that specified constants exist before stubbing them and handles configuration
|
6
|
+
# of boolean-check methods for nested constants.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# const_conf_as('FooConfig::DATABASE::ENABLED' => true)
|
10
|
+
module ConstConf::ConstConfHelper
|
11
|
+
# Sets constants and their configured values for testing purposes.
|
12
|
+
#
|
13
|
+
# This method is designed to facilitate testing by allowing test code to
|
14
|
+
# temporarily override the values of constants, including nested constants
|
15
|
+
# within modules. It ensures that the specified constants exist before
|
16
|
+
# attempting to stub them, and also handles the configuration of
|
17
|
+
# boolean-check methods for nested constants.
|
18
|
+
#
|
19
|
+
# @param config_hash [Hash] a hash mapping constant names (as strings) to
|
20
|
+
# their intended values
|
21
|
+
def const_conf_as(config_hash)
|
22
|
+
config_hash.each do |const_name, value|
|
23
|
+
Object.const_defined?(const_name) or
|
24
|
+
raise NameError, "constant #{const_name} does not exist"
|
25
|
+
stub_const("#{const_name}", value)
|
26
|
+
const_parts = const_name.split('::')
|
27
|
+
if const_parts.size > 1
|
28
|
+
parent_const_name = const_parts[0..-2] * '::'
|
29
|
+
Object.const_defined?(parent_const_name) or raise NameError,
|
30
|
+
"parent constant #{parent_const_name} does not exist"
|
31
|
+
parent_const = Object.const_get(parent_const_name)
|
32
|
+
allow(parent_const).to receive("#{const_parts.last}?").
|
33
|
+
and_return(value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/const_conf/version.rb
CHANGED
data/lib/const_conf.rb
CHANGED
@@ -32,7 +32,8 @@ module ConstConf
|
|
32
32
|
# synchronize access to shared resources across threads. It ensures that
|
33
33
|
# only one thread can execute critical sections of code at a time.
|
34
34
|
#
|
35
|
-
# @return [Monitor] the singleton Monitor instance used for thread
|
35
|
+
# @return [Monitor] the singleton Monitor instance used for thread
|
36
|
+
# synchronization
|
36
37
|
def monitor
|
37
38
|
@monitor ||= Monitor.new
|
38
39
|
end
|
@@ -43,7 +44,8 @@ module ConstConf
|
|
43
44
|
# This method serves as a getter and setter for the module_files attribute,
|
44
45
|
# which stores a hash mapping modules to their corresponding file paths.
|
45
46
|
#
|
46
|
-
# @return [Hash, nil] the current value of the module_files instance
|
47
|
+
# @return [Hash, nil] the current value of the module_files instance
|
48
|
+
# variable
|
47
49
|
attr_accessor :module_files
|
48
50
|
|
49
51
|
# Registers a module-file mapping in the global registry.
|
@@ -68,7 +70,8 @@ module ConstConf
|
|
68
70
|
# It collects the file paths associated with each module before removing
|
69
71
|
# them, returning an array of the collected file paths.
|
70
72
|
#
|
71
|
-
# @return [Array<String>] an array containing the file paths of the
|
73
|
+
# @return [Array<String>] an array containing the file paths of the
|
74
|
+
# destroyed modules
|
72
75
|
def destroy
|
73
76
|
monitor.synchronize do
|
74
77
|
files = []
|
@@ -111,7 +114,8 @@ module ConstConf
|
|
111
114
|
#
|
112
115
|
# @param plugin [ Module ] the module to be included in ConstConf::Setting
|
113
116
|
#
|
114
|
-
# @return [ Class ] returns the current class (self) to allow for method
|
117
|
+
# @return [ Class ] returns the current class (self) to allow for method
|
118
|
+
# chaining
|
115
119
|
def plugin(plugin)
|
116
120
|
ConstConf::Setting.class_eval { include plugin }
|
117
121
|
self
|
@@ -150,7 +154,8 @@ module ConstConf
|
|
150
154
|
setting = Setting.new(name: [ name, id ], prefix:, &setting_block)
|
151
155
|
if previous_setting = outer_configuration.setting_for(setting.env_var_name)
|
152
156
|
raise ConstConf::SettingAlreadyDefined,
|
153
|
-
"setting for env var #{setting.env_var_name} already defined
|
157
|
+
"setting for env var #{setting.env_var_name} already defined "\
|
158
|
+
"in #{previous_setting.name}"
|
154
159
|
end
|
155
160
|
settings[setting.env_var_name] = setting
|
156
161
|
const_set id, setting.value
|
@@ -202,13 +207,14 @@ module ConstConf
|
|
202
207
|
|
203
208
|
# Returns the settings hash for the configuration module.
|
204
209
|
#
|
205
|
-
# This method provides access to the internal hash that stores all
|
206
|
-
# settings defined within the module. It ensures the hash is
|
207
|
-
# returning it, guaranteeing that subsequent accesses
|
208
|
-
# instance.
|
210
|
+
# This method provides access to the internal hash that stores all
|
211
|
+
# configuration settings defined within the module. It ensures the hash is
|
212
|
+
# initialized before returning it, guaranteeing that subsequent accesses
|
213
|
+
# will return the same hash instance.
|
209
214
|
#
|
210
|
-
# @return [Hash<String, ConstConf::Setting>] the hash containing all
|
211
|
-
#
|
215
|
+
# @return [Hash<String, ConstConf::Setting>] the hash containing all
|
216
|
+
# settings for this configuration module, keyed by their environment
|
217
|
+
# variable names
|
212
218
|
def settings
|
213
219
|
@settings ||= {}
|
214
220
|
end
|
@@ -221,7 +227,8 @@ module ConstConf
|
|
221
227
|
# include ConstConf. It ensures the set is initialized before returning it,
|
222
228
|
# guaranteeing that subsequent accesses will return the same set instance.
|
223
229
|
#
|
224
|
-
# @return [Set<Symbol>] the set containing the names of nested module
|
230
|
+
# @return [Set<Symbol>] the set containing the names of nested module
|
231
|
+
# constants
|
225
232
|
# @see #const_added
|
226
233
|
# @see #nested_configurations
|
227
234
|
# @see #all_configurations
|
@@ -264,7 +271,8 @@ module ConstConf
|
|
264
271
|
# identifying the top-level configuration module that contains the current
|
265
272
|
# one.
|
266
273
|
#
|
267
|
-
# @return [ Module, nil ] the outer configuration module if found, or nil
|
274
|
+
# @return [ Module, nil ] the outer configuration module if found, or nil
|
275
|
+
# if none exists
|
268
276
|
def outer_configuration
|
269
277
|
[ self, *module_parents ].reverse_each.find { it < ConstConf }
|
270
278
|
end
|
@@ -316,10 +324,14 @@ module ConstConf
|
|
316
324
|
# environment variable name. It iterates through the module hierarchy to
|
317
325
|
# find the setting, checking each module's settings hash for a match.
|
318
326
|
#
|
319
|
-
# @param name [ String, Symbol ] the environment variable name to search
|
320
|
-
#
|
327
|
+
# @param name [ String, Symbol ] the environment variable name to search
|
328
|
+
# for
|
321
329
|
#
|
322
|
-
# @
|
330
|
+
# @param modules [ Array<Module> ] the array of modules to search within,
|
331
|
+
# defaults to [ self ]
|
332
|
+
#
|
333
|
+
# @return [ ConstConf::Setting, nil ] the matching setting object if found,
|
334
|
+
# or nil if not found
|
323
335
|
def setting_for(name)
|
324
336
|
name = name.to_s
|
325
337
|
each_nested_configuration do |modul,|
|
@@ -338,8 +350,9 @@ module ConstConf
|
|
338
350
|
# the module hierarchy to gather these names, ensuring that each setting's
|
339
351
|
# environment variable name is included in the final result.
|
340
352
|
#
|
341
|
-
# @return [Array<String>] an array containing all the environment variable
|
342
|
-
#
|
353
|
+
# @return [Array<String>] an array containing all the environment variable
|
354
|
+
# names used in the configuration settings across the module and its nested
|
355
|
+
# modules
|
343
356
|
def env_var_names
|
344
357
|
names = Set[]
|
345
358
|
each_nested_configuration do |modul,|
|
@@ -357,25 +370,27 @@ module ConstConf
|
|
357
370
|
# environment variable names and values are their corresponding
|
358
371
|
# configuration values.
|
359
372
|
#
|
360
|
-
# @return [ Hash<String, Object> ] a hash mapping environment variable
|
373
|
+
# @return [ Hash<String, Object> ] a hash mapping environment variable
|
374
|
+
# names to their values
|
361
375
|
def env_vars
|
362
376
|
env_var_names.each_with_object({}) do |n, hash|
|
363
377
|
hash[n] = setting_value_for(n)
|
364
378
|
end
|
365
379
|
end
|
366
380
|
|
367
|
-
# Retrieves the effective value for a configuration setting identified by
|
368
|
-
# environment variable name.
|
381
|
+
# Retrieves the effective value for a configuration setting identified by
|
382
|
+
# its environment variable name.
|
369
383
|
#
|
370
|
-
# This method looks up a configuration setting using the provided
|
371
|
-
# variable name and returns its effective value, which is
|
372
|
-
# checking the environment variable and falling back to the
|
373
|
-
# not set.
|
384
|
+
# This method looks up a configuration setting using the provided
|
385
|
+
# environment variable name and returns its effective value, which is
|
386
|
+
# determined by checking the environment variable and falling back to the
|
387
|
+
# default value if not set.
|
374
388
|
#
|
375
|
-
# @param name [String, Symbol] the environment variable name used to
|
389
|
+
# @param name [String, Symbol] the environment variable name used to
|
390
|
+
# identify the setting
|
376
391
|
#
|
377
|
-
# @return [Object] the effective configuration value for the specified
|
378
|
-
#
|
392
|
+
# @return [Object] the effective configuration value for the specified
|
393
|
+
# setting, or the default value if the environment variable is not set
|
379
394
|
def setting_value_for(name)
|
380
395
|
setting = setting_for(name)
|
381
396
|
setting&.value
|
@@ -391,7 +406,8 @@ module ConstConf
|
|
391
406
|
# standard output.
|
392
407
|
#
|
393
408
|
# @param object [Object] the ConstConf module or setting to display
|
394
|
-
# @param io [IO, nil] the IO object to write the output to; if nil, uses
|
409
|
+
# @param io [IO, nil] the IO object to write the output to; if nil, uses
|
410
|
+
# STDOUT
|
395
411
|
def view(object: self, io: nil)
|
396
412
|
output = ConstConf::Tree.from_const_conf(object).to_a
|
397
413
|
if io
|
@@ -414,8 +430,10 @@ module ConstConf
|
|
414
430
|
# internally to process all configuration settings across a module
|
415
431
|
# hierarchy.
|
416
432
|
#
|
417
|
-
# @yield [ configuration ] yields each configuration module in the
|
418
|
-
#
|
433
|
+
# @yield [ configuration ] yields each configuration module in the
|
434
|
+
# hierarchy
|
435
|
+
# @yieldparam configuration [ Module ] a configuration module from the
|
436
|
+
# hierarchy
|
419
437
|
#
|
420
438
|
# @return [ Enumerator ] returns an enumerator if no block is given,
|
421
439
|
# otherwise nil.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: const_conf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Frank
|
@@ -167,10 +167,13 @@ extra_rdoc_files:
|
|
167
167
|
- lib/const_conf/railtie.rb
|
168
168
|
- lib/const_conf/setting.rb
|
169
169
|
- lib/const_conf/setting_accessor.rb
|
170
|
+
- lib/const_conf/spec.rb
|
171
|
+
- lib/const_conf/spec/const_conf_helper.rb
|
170
172
|
- lib/const_conf/tree.rb
|
171
173
|
- lib/const_conf/version.rb
|
172
174
|
- lib/const_conf/yaml_plugin.rb
|
173
175
|
files:
|
176
|
+
- CHANGES.md
|
174
177
|
- Gemfile
|
175
178
|
- LICENSE
|
176
179
|
- README.md
|
@@ -185,6 +188,8 @@ files:
|
|
185
188
|
- lib/const_conf/railtie.rb
|
186
189
|
- lib/const_conf/setting.rb
|
187
190
|
- lib/const_conf/setting_accessor.rb
|
191
|
+
- lib/const_conf/spec.rb
|
192
|
+
- lib/const_conf/spec/const_conf_helper.rb
|
188
193
|
- lib/const_conf/tree.rb
|
189
194
|
- lib/const_conf/version.rb
|
190
195
|
- lib/const_conf/yaml_plugin.rb
|