superconfig 2.2.1 → 3.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c4cefd39e531ddd3bd73508b42f10594c5723d57b3c78655b64a4a745c41be90
4
- data.tar.gz: f115b83ecf9165eccadb65658f486274988f8741b7dc1031d80894d492194a35
3
+ metadata.gz: fae15b25d13e8edeb01416840600001b47723b09842466d40cfc65b491a4e236
4
+ data.tar.gz: 0d7e291466bf767592b1b7f16e64fd62fb865659212b58664c8c78ff65699407
5
5
  SHA512:
6
- metadata.gz: c84799398211996ea64120c5d3d41cfb800cc17f4555719e3c7b6e857f6ed4b4b97dd28b067a982d6686c679888c77267da61d252913033a1e85848ab42f5cfd
7
- data.tar.gz: ae6415b1f50f06eb63cdcf393486ecf2ccbaf23ab05663ba3641a3e4a0e242a2871b0ade58c1602e759c60a0506418cb8dd16536c9049098b3ba7ddf39be82de
6
+ metadata.gz: 3a301a35a1e4daf689ab1d50c026d1c620328ad50ad24e2d09625d1fc9f97141b6d9079b1f06af9f4d26ddf6aa23c79905d428dc21572983ac812940e550f26f
7
+ data.tar.gz: 326dc0c6f7e035520e9df57ba3f55fcb7dfc4041ea77925a5e66442420add636a4690856fbe56a0f81b19a848524e61eeaa3872e04a9edf8d5fa0c998c1c062d
data/.gitignore CHANGED
@@ -8,3 +8,4 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.enc
11
+ .minitestfailures
data/CHANGELOG.md CHANGED
@@ -11,6 +11,13 @@ Prefix your message with one of the following:
11
11
  - [Security] in case of vulnerabilities.
12
12
  -->
13
13
 
14
+ # v3.0.0
15
+
16
+ - [Added] Add support for prefixed env vars with (e.g. `MYAPP_`).
17
+ - [Added] Add `SuperConfig#get` method to retrieve values.
18
+ - [Changed] Define boolean properties defined with `SuperConfig#set` as
19
+ predicates.
20
+
14
21
  # v2.2.1
15
22
 
16
23
  - [Changed] Silence warnings when reassigning a method with `set`.
data/README.md CHANGED
@@ -38,6 +38,12 @@ Config.timeout
38
38
  Config.force_ssl?
39
39
  ```
40
40
 
41
+ > [!NOTE]
42
+ >
43
+ > Properties defined with `bool` will always be named as predicates, ending with
44
+ > `?`. The same thing happens if you define properties using `#set(name, value)`
45
+ > where the value is a boolean.
46
+
41
47
  You can specify the description for both `mandatory` and `optional` methods;
42
48
  this will be used in exceptions.
43
49
 
@@ -49,6 +55,21 @@ end
49
55
  #=> SuperConfig::MissingEnvironmentVariable: MISSING_VAR (this is important) is not defined
50
56
  ```
51
57
 
58
+ ### Using prefixed environment variables
59
+
60
+ If you're creating a library, you may want to use prefixed env vars. In this
61
+ case, you can pass a prefix to `SuperConfig.new`.
62
+
63
+ ```ruby
64
+ Config = SuperConfig.new(prefix: "MYAPP") do
65
+ optional :redis_url, string, "redis://127.0.0.1"
66
+ end
67
+ ```
68
+
69
+ This will look for `MYAPP_REDIS_URL` in the environment variables.
70
+
71
+ ### Setting arbitrary properties
72
+
52
73
  If you're going to use `SuperConfig` as your main configuration object, you can
53
74
  also set arbitrary properties, like the following:
54
75
 
@@ -64,6 +85,8 @@ Config.redis.get("key")
64
85
  #=> "value"
65
86
  ```
66
87
 
88
+ ### Caching values
89
+
67
90
  Values are cached by default. If you want to dynamically generate new values,
68
91
  set `cache: false`.
69
92
 
@@ -73,6 +96,8 @@ Config = SuperConfig.new do
73
96
  end
74
97
  ```
75
98
 
99
+ ### Setting values
100
+
76
101
  You can also set values using `SuperConfig#set`.
77
102
 
78
103
  ```ruby
@@ -81,6 +106,8 @@ Config = SuperConfig.new do
81
106
  end
82
107
  ```
83
108
 
109
+ ### Troubleshooting
110
+
84
111
  You may want to start a debug session without raising exceptions for missing
85
112
  variables. In this case, just pass `raise_exception: false` instead to log error
86
113
  messages to `$stderr`. This is especially great with Rails' credentials command
@@ -94,8 +121,10 @@ end
94
121
  #=> [SUPERCONFIG] DATABASE_URL (the leader database) is not defined
95
122
  ```
96
123
 
97
- I'd like to centralize access to my credentials; there's a handy mechanism for
98
- doing that with `SuperConfig`:
124
+ ### Rails credentials
125
+
126
+ I like to centralize access to my Rails credentials; there's a handy mechanism
127
+ for doing that with `SuperConfig`:
99
128
 
100
129
  ```ruby
101
130
  Config = SuperConfig.new do
data/lib/superconfig.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SuperConfig
4
- VERSION = "2.2.1"
4
+ VERSION = "3.0.0"
5
5
 
6
6
  MissingEnvironmentVariable = Class.new(StandardError)
7
7
  MissingCallable = Class.new(StandardError)
@@ -14,8 +14,15 @@ module SuperConfig
14
14
  BOOL_TRUE = ["yes", "true", "1", true].freeze
15
15
  BOOL_FALSE = %w[no false].freeze
16
16
 
17
- def initialize(env: ENV, raise_exception: true, stderr: $stderr, &block)
17
+ def initialize(
18
+ env: ENV,
19
+ prefix: nil,
20
+ raise_exception: true,
21
+ stderr: $stderr,
22
+ &block
23
+ )
18
24
  @env = env
25
+ @prefix = prefix
19
26
  @raise_exception = raise_exception
20
27
  @stderr = stderr
21
28
  @attributes = {}
@@ -29,6 +36,8 @@ module SuperConfig
29
36
  alias inspect to_s
30
37
 
31
38
  def validate!(env_var, required, description)
39
+ env_var = [@prefix, env_var].compact.join("_").upcase
40
+
32
41
  return unless required
33
42
  return if @env.key?(env_var)
34
43
 
@@ -57,7 +66,19 @@ module SuperConfig
57
66
  assign(name, type, default, aliases:, description:)
58
67
  end
59
68
 
69
+ def get(name)
70
+ predicate = "#{name}?"
71
+
72
+ if respond_to?(predicate)
73
+ public_send(predicate)
74
+ else
75
+ public_send(name)
76
+ end
77
+ end
78
+
60
79
  def set(name, value)
80
+ name = "#{name}?" if value.is_a?(TrueClass) || value.is_a?(FalseClass)
81
+
61
82
  silence_warnings do
62
83
  property(name) { value }
63
84
  end
@@ -198,7 +219,7 @@ module SuperConfig
198
219
  description: nil
199
220
  )
200
221
  name = name.to_s
201
- env_var = name.upcase
222
+ env_var = [@prefix, name].compact.join("_").upcase
202
223
 
203
224
  @attributes[env_var] = {required:, default:}
204
225
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: superconfig
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-02-11 00:00:00.000000000 Z
10
+ date: 2025-03-29 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: bundler