env_parser 1.6.0 → 1.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -5
- data/README.md +1 -1
- data/docs/EnvParser/AutoregisterFileNotFound.html +1 -1
- data/docs/EnvParser/Error.html +1 -1
- data/docs/EnvParser/TypeAlreadyDefinedError.html +1 -1
- data/docs/EnvParser/Types/BaseTypes.html +1 -1
- data/docs/EnvParser/Types/ChronologyTypes.html +1 -1
- data/docs/EnvParser/Types/InternetTypes.html +13 -1
- data/docs/EnvParser/Types.html +1 -1
- data/docs/EnvParser/UnknownTypeError.html +1 -1
- data/docs/EnvParser/UnparseableAutoregisterSpec.html +1 -1
- data/docs/EnvParser/ValueNotAllowedError.html +1 -1
- data/docs/EnvParser/ValueNotConvertibleError.html +1 -1
- data/docs/EnvParser.html +81 -53
- data/docs/_index.html +1 -1
- data/docs/file.README.html +66 -66
- data/docs/index.html +66 -66
- data/docs/top-level-namespace.html +1 -1
- data/env_parser.gemspec +2 -3
- data/lib/env_parser/version.rb +1 -1
- metadata +6 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 897f5a82aae1f8d7c418937e61f72669eba8d846c5ee282bffd7f4ee72970f96
|
4
|
+
data.tar.gz: 6b90041b9ea794d896c9f603e8117377482fdaecb682cda49a925fd9dbdc22a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc132c2bdb3feffef9f294dae4ae929533e65da860e93b360290e655625011640f773f02596cfda8570e87e95877bc5a3c814cc8b23f43078bcf0b99dea4cdf9
|
7
|
+
data.tar.gz: c5d8ff0e7378ff6c94f104bcf1d35ec71ab4952b7fd3826854818b7d01f6a45f000697b5dcc173babcafa073dc6eb25def9721df60210b47be4afc936e4eebfe
|
data/Gemfile.lock
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
env_parser (1.
|
4
|
+
env_parser (1.6.0)
|
5
5
|
activesupport (>= 6.1.0, < 7.1)
|
6
|
-
chronic
|
7
|
-
chronic_duration
|
6
|
+
chronic (~> 0)
|
7
|
+
chronic_duration (~> 0)
|
8
8
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
@@ -29,7 +29,6 @@ GEM
|
|
29
29
|
parser (3.1.3.0)
|
30
30
|
ast (~> 2.4.1)
|
31
31
|
rainbow (3.1.1)
|
32
|
-
rake (13.0.6)
|
33
32
|
regexp_parser (2.6.1)
|
34
33
|
rexml (3.2.5)
|
35
34
|
rspec (3.12.0)
|
@@ -73,7 +72,6 @@ PLATFORMS
|
|
73
72
|
DEPENDENCIES
|
74
73
|
bundler (~> 2)
|
75
74
|
env_parser!
|
76
|
-
rake
|
77
75
|
rspec (~> 3)
|
78
76
|
rspec_junit_formatter
|
79
77
|
rubocop
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
[![Gem Version](https://img.shields.io/github/v/release/nestor-custodio/env_parser?color=green&label=gem%20version)](https://rubygems.org/gems/env_parser)
|
2
|
-
[![MIT License](https://img.shields.io/github/license/nestor-custodio/env_parser)](https://
|
2
|
+
[![MIT License](https://img.shields.io/github/license/nestor-custodio/env_parser)](https://tldrlegal.com/license/mit-license)
|
3
3
|
|
4
4
|
|
5
5
|
# EnvParser
|
@@ -133,7 +133,7 @@ feature).</p>
|
|
133
133
|
</div>
|
134
134
|
|
135
135
|
<div id="footer">
|
136
|
-
Generated on
|
136
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
137
137
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
138
138
|
0.9.28 (ruby-3.0.4).
|
139
139
|
</div>
|
data/docs/EnvParser/Error.html
CHANGED
@@ -128,7 +128,7 @@
|
|
128
128
|
</div>
|
129
129
|
|
130
130
|
<div id="footer">
|
131
|
-
Generated on
|
131
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
132
132
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
133
133
|
0.9.28 (ruby-3.0.4).
|
134
134
|
</div>
|
@@ -132,7 +132,7 @@
|
|
132
132
|
</div>
|
133
133
|
|
134
134
|
<div id="footer">
|
135
|
-
Generated on
|
135
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
136
136
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
137
137
|
0.9.28 (ruby-3.0.4).
|
138
138
|
</div>
|
@@ -169,7 +169,7 @@ valid, parseable JSON).</p>
|
|
169
169
|
</div>
|
170
170
|
|
171
171
|
<div id="footer">
|
172
|
-
Generated on
|
172
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
173
173
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
174
174
|
0.9.28 (ruby-3.0.4).
|
175
175
|
</div>
|
@@ -150,7 +150,7 @@
|
|
150
150
|
</div>
|
151
151
|
|
152
152
|
<div id="footer">
|
153
|
-
Generated on
|
153
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
154
154
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
155
155
|
0.9.28 (ruby-3.0.4).
|
156
156
|
</div>
|
@@ -129,6 +129,18 @@
|
|
129
129
|
Note this does not guarantee RFC5322-conformity.
|
130
130
|
</td>
|
131
131
|
</tr>
|
132
|
+
<tr>
|
133
|
+
<td>:version / :semver</td>
|
134
|
+
<td>MatchData</td>
|
135
|
+
<td><code>nil</code></td>
|
136
|
+
<td>
|
137
|
+
The resulting MatchData has named captures for "major", "minor", "patch", "prerelease", and "buildmetadata".
|
138
|
+
<br />
|
139
|
+
The Regex used for generating this MatchData is available at: https://regex101.com/r/Ly7O1x/3/
|
140
|
+
<br />
|
141
|
+
See https://semver.org for additional info.
|
142
|
+
</td>
|
143
|
+
</tr>
|
132
144
|
</tbody>
|
133
145
|
</table>
|
134
146
|
|
@@ -150,7 +162,7 @@
|
|
150
162
|
</div>
|
151
163
|
|
152
164
|
<div id="footer">
|
153
|
-
Generated on
|
165
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
154
166
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
155
167
|
0.9.28 (ruby-3.0.4).
|
156
168
|
</div>
|
data/docs/EnvParser/Types.html
CHANGED
@@ -118,7 +118,7 @@ Exists only for documentation’s sake.</p>
|
|
118
118
|
</div>
|
119
119
|
|
120
120
|
<div id="footer">
|
121
|
-
Generated on
|
121
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
122
122
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
123
123
|
0.9.28 (ruby-3.0.4).
|
124
124
|
</div>
|
@@ -132,7 +132,7 @@
|
|
132
132
|
</div>
|
133
133
|
|
134
134
|
<div id="footer">
|
135
|
-
Generated on
|
135
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
136
136
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
137
137
|
0.9.28 (ruby-3.0.4).
|
138
138
|
</div>
|
@@ -133,7 +133,7 @@ feature).</p>
|
|
133
133
|
</div>
|
134
134
|
|
135
135
|
<div id="footer">
|
136
|
-
Generated on
|
136
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
137
137
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
138
138
|
0.9.28 (ruby-3.0.4).
|
139
139
|
</div>
|
@@ -134,7 +134,7 @@ check.</p>
|
|
134
134
|
</div>
|
135
135
|
|
136
136
|
<div id="footer">
|
137
|
-
Generated on
|
137
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
138
138
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
139
139
|
0.9.28 (ruby-3.0.4).
|
140
140
|
</div>
|
@@ -132,7 +132,7 @@
|
|
132
132
|
</div>
|
133
133
|
|
134
134
|
<div id="footer">
|
135
|
-
Generated on
|
135
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
136
136
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
137
137
|
0.9.28 (ruby-3.0.4).
|
138
138
|
</div>
|
data/docs/EnvParser.html
CHANGED
@@ -150,7 +150,7 @@
|
|
150
150
|
<dt id="VERSION-constant" class="">VERSION =
|
151
151
|
|
152
152
|
</dt>
|
153
|
-
<dd><pre class="code"><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>1.
|
153
|
+
<dd><pre class="code"><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>1.6.0</span><span class='tstring_end'>'</span></span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span></pre></dd>
|
154
154
|
|
155
155
|
</dl>
|
156
156
|
|
@@ -339,22 +339,22 @@ itself. i.e. ENV.parse(‘XYZ’, …) is equivalent to EnvParser.parse(ENV[‘
|
|
339
339
|
<pre class="lines">
|
340
340
|
|
341
341
|
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
342
|
+
232
|
343
|
+
233
|
344
|
+
234
|
345
|
+
235
|
346
|
+
236
|
347
|
+
237
|
348
|
+
238
|
349
|
+
239
|
350
|
+
240
|
351
|
+
241
|
352
|
+
242
|
353
|
+
243
|
354
|
+
244</pre>
|
355
355
|
</td>
|
356
356
|
<td>
|
357
|
-
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line
|
357
|
+
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 232</span>
|
358
358
|
|
359
359
|
<span class='kw'>def</span> <span class='id identifier rubyid_add_env_bindings'>add_env_bindings</span>
|
360
360
|
<span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_instance_eval'>instance_eval</span> <span class='kw'>do</span>
|
@@ -455,22 +455,6 @@ itself. i.e. ENV.parse(‘XYZ’, …) is equivalent to EnvParser.parse(ENV[‘
|
|
455
455
|
<pre class="lines">
|
456
456
|
|
457
457
|
|
458
|
-
245
|
459
|
-
246
|
460
|
-
247
|
461
|
-
248
|
462
|
-
249
|
463
|
-
250
|
464
|
-
251
|
465
|
-
252
|
466
|
-
253
|
467
|
-
254
|
468
|
-
255
|
469
|
-
256
|
470
|
-
257
|
471
|
-
258
|
472
|
-
259
|
473
|
-
260
|
474
458
|
261
|
475
459
|
262
|
476
460
|
263
|
@@ -479,10 +463,26 @@ itself. i.e. ENV.parse(‘XYZ’, …) is equivalent to EnvParser.parse(ENV[‘
|
|
479
463
|
266
|
480
464
|
267
|
481
465
|
268
|
482
|
-
269
|
466
|
+
269
|
467
|
+
270
|
468
|
+
271
|
469
|
+
272
|
470
|
+
273
|
471
|
+
274
|
472
|
+
275
|
473
|
+
276
|
474
|
+
277
|
475
|
+
278
|
476
|
+
279
|
477
|
+
280
|
478
|
+
281
|
479
|
+
282
|
480
|
+
283
|
481
|
+
284
|
482
|
+
285</pre>
|
483
483
|
</td>
|
484
484
|
<td>
|
485
|
-
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line
|
485
|
+
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 261</span>
|
486
486
|
|
487
487
|
<span class='kw'>def</span> <span class='id identifier rubyid_autoregister'>autoregister</span><span class='lparen'>(</span><span class='id identifier rubyid_filename'>filename</span> <span class='op'>=</span> <span class='kw'>nil</span><span class='rparen'>)</span>
|
488
488
|
<span class='id identifier rubyid_filename'>filename</span> <span class='op'>||=</span> <span class='const'><span class='object_link'><a href="#AUTOREGISTER_FILE-constant" title="EnvParser::AUTOREGISTER_FILE (constant)">AUTOREGISTER_FILE</a></span></span>
|
@@ -490,7 +490,7 @@ itself. i.e. ENV.parse(‘XYZ’, …) is equivalent to EnvParser.parse(ENV[‘
|
|
490
490
|
|
491
491
|
<span class='id identifier rubyid_autoregister_spec'>autoregister_spec</span><span class='period'>.</span><span class='id identifier rubyid_deep_symbolize_keys!'>deep_symbolize_keys!</span>
|
492
492
|
<span class='id identifier rubyid_autoregister_spec'>autoregister_spec</span><span class='period'>.</span><span class='id identifier rubyid_transform_values!'>transform_values!</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_spec'>spec</span><span class='op'>|</span>
|
493
|
-
<span class='id identifier rubyid_sanitized'>sanitized</span> <span class='op'>=</span> <span class='id identifier rubyid_spec'>spec</span><span class='period'>.</span><span class='id identifier rubyid_slice'>slice</span><span class='lparen'>(</span><span class='symbol'>:as</span><span class='comma'>,</span> <span class='symbol'>:within</span><span class='comma'>,</span> <span class='symbol'>:if_unset</span><span class='comma'>,</span> <span class='symbol'>:from_set</span><span class='rparen'>)</span>
|
493
|
+
<span class='id identifier rubyid_sanitized'>sanitized</span> <span class='op'>=</span> <span class='id identifier rubyid_spec'>spec</span><span class='period'>.</span><span class='id identifier rubyid_slice'>slice</span><span class='lparen'>(</span><span class='symbol'>:as</span><span class='comma'>,</span> <span class='symbol'>:named</span><span class='comma'>,</span> <span class='symbol'>:within</span><span class='comma'>,</span> <span class='symbol'>:if_unset</span><span class='comma'>,</span> <span class='symbol'>:from_set</span><span class='rparen'>)</span>
|
494
494
|
<span class='id identifier rubyid_sanitized'>sanitized</span><span class='lbracket'>[</span><span class='symbol'>:as</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_sanitized'>sanitized</span><span class='lbracket'>[</span><span class='symbol'>:as</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span> <span class='kw'>if</span> <span class='id identifier rubyid_sanitized'>sanitized</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span> <span class='symbol'>:as</span>
|
495
495
|
<span class='id identifier rubyid_sanitized'>sanitized</span><span class='lbracket'>[</span><span class='symbol'>:within</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='id identifier rubyid_sanitized'>sanitized</span><span class='lbracket'>[</span><span class='symbol'>:within</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_constantize'>constantize</span> <span class='kw'>if</span> <span class='id identifier rubyid_sanitized'>sanitized</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span> <span class='symbol'>:within</span>
|
496
496
|
|
@@ -948,12 +948,12 @@ both validation checks must pass.</strong></p>
|
|
948
948
|
variable names and whose values are the options set for each variable’s <span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">register</a></span> call.</p>
|
949
949
|
|
950
950
|
<pre class="code ruby"><code class="ruby">
|
951
|
-
|
951
|
+
# Example shortcut usage:
|
952
952
|
|
953
953
|
EnvParser.register :A, from: one_hash, as: :integer
|
954
954
|
EnvParser.register :B, from: another_hash, as: :string, if_unset: 'none'
|
955
955
|
|
956
|
-
|
956
|
+
# ... is equivalent to ...
|
957
957
|
|
958
958
|
EnvParser.register(
|
959
959
|
A: { from: one_hash, as: :integer }
|
@@ -1026,6 +1026,28 @@ variable names and whose values are the options set for each variable’s <span
|
|
1026
1026
|
|
1027
1027
|
</li>
|
1028
1028
|
|
1029
|
+
<li>
|
1030
|
+
<span class="name">named</span>
|
1031
|
+
<span class="type">(<tt>Symbol</tt>)</span>
|
1032
|
+
<span class="default">
|
1033
|
+
|
1034
|
+
</span>
|
1035
|
+
|
1036
|
+
— <div class='inline'><p>The name the constant should be given. Valid only when a “within” value is <em>explicitly</em>
|
1037
|
+
given. This allows for decoupling ENV variable names from the constant name defined
|
1038
|
+
within its target class or module, allowing for the ENV variables to be namespaced in
|
1039
|
+
some way.</p>
|
1040
|
+
|
1041
|
+
<pre class="code ruby"><code class="ruby">
|
1042
|
+
<span class='const'><span class='object_link'><a href="" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_register'>register</span><span class='lparen'>(</span>
|
1043
|
+
<span class='label'>CUSTOM_CLIENT_DEFAULT_HOSTNAME:</span> <span class='lbrace'>{</span> <span class='label'>as:</span> <span class='symbol'>:string</span><span class='comma'>,</span> <span class='label'>named:</span> <span class='symbol'>:DEFAULT_HOSTNAME</span><span class='comma'>,</span> <span class='label'>within:</span> <span class='const'>CustomClient</span> <span class='rbrace'>}</span><span class='comma'>,</span>
|
1044
|
+
<span class='label'>CUSTOM_CLIENT_DEFAULT_PORT:</span> <span class='lbrace'>{</span> <span class='label'>as:</span> <span class='symbol'>:integer</span><span class='comma'>,</span> <span class='label'>named:</span> <span class='symbol'>:DEFAULT_PORT</span><span class='comma'>,</span> <span class='label'>within:</span> <span class='const'>CustomClient</span> <span class='rbrace'>}</span>
|
1045
|
+
<span class='rparen'>)</span>
|
1046
|
+
</code></pre>
|
1047
|
+
</div>
|
1048
|
+
|
1049
|
+
</li>
|
1050
|
+
|
1029
1051
|
<li>
|
1030
1052
|
<span class="name">within</span>
|
1031
1053
|
<span class="type">(<tt>Module</tt>, <tt>Class</tt>)</span>
|
@@ -1132,19 +1154,6 @@ validation must give its own “validated_by” Proc.</p>
|
|
1132
1154
|
<pre class="lines">
|
1133
1155
|
|
1134
1156
|
|
1135
|
-
178
|
1136
|
-
179
|
1137
|
-
180
|
1138
|
-
181
|
1139
|
-
182
|
1140
|
-
183
|
1141
|
-
184
|
1142
|
-
185
|
1143
|
-
186
|
1144
|
-
187
|
1145
|
-
188
|
1146
|
-
189
|
1147
|
-
190
|
1148
1157
|
191
|
1149
1158
|
192
|
1150
1159
|
193
|
@@ -1159,10 +1168,26 @@ validation must give its own “validated_by” Proc.</p>
|
|
1159
1168
|
202
|
1160
1169
|
203
|
1161
1170
|
204
|
1162
|
-
205
|
1171
|
+
205
|
1172
|
+
206
|
1173
|
+
207
|
1174
|
+
208
|
1175
|
+
209
|
1176
|
+
210
|
1177
|
+
211
|
1178
|
+
212
|
1179
|
+
213
|
1180
|
+
214
|
1181
|
+
215
|
1182
|
+
216
|
1183
|
+
217
|
1184
|
+
218
|
1185
|
+
219
|
1186
|
+
220
|
1187
|
+
221</pre>
|
1163
1188
|
</td>
|
1164
1189
|
<td>
|
1165
|
-
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line
|
1190
|
+
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 191</span>
|
1166
1191
|
|
1167
1192
|
<span class='kw'>def</span> <span class='id identifier rubyid_register'>register</span><span class='lparen'>(</span><span class='id identifier rubyid_name'>name</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_validation_block'>validation_block</span><span class='rparen'>)</span>
|
1168
1193
|
<span class='comment'># Allow for registering multiple variables simultaneously via a single call.
|
@@ -1174,6 +1199,9 @@ validation must give its own “validated_by” Proc.</p>
|
|
1174
1199
|
<span class='id identifier rubyid_from'>from</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:from</span><span class='comma'>,</span> <span class='const'>ENV</span><span class='rparen'>)</span>
|
1175
1200
|
<span class='id identifier rubyid_within'>within</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:within</span><span class='comma'>,</span> <span class='const'>Kernel</span><span class='rparen'>)</span>
|
1176
1201
|
|
1202
|
+
<span class='id identifier rubyid_named'>named</span> <span class='op'>=</span> <span class='id identifier rubyid_name'>name</span>
|
1203
|
+
<span class='id identifier rubyid_named'>named</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:named</span><span class='comma'>,</span> <span class='id identifier rubyid_name'>name</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span> <span class='symbol'>:within</span>
|
1204
|
+
|
1177
1205
|
<span class='comment'># ENV *seems* like a Hash and it does *some* Hash-y things, but it is NOT a Hash and that can
|
1178
1206
|
</span> <span class='comment'># bite you in some cases. Making sure we're working with a straight-up Hash saves a lot of
|
1179
1207
|
</span> <span class='comment'># sanity checks later on. This is also a good place to make sure we're working with a String
|
@@ -1188,7 +1216,7 @@ validation must give its own “validated_by” Proc.</p>
|
|
1188
1216
|
|
1189
1217
|
<span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='id identifier rubyid_from'>from</span><span class='lbracket'>[</span><span class='id identifier rubyid_name'>name</span><span class='rbracket'>]</span>
|
1190
1218
|
<span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_validation_block'>validation_block</span><span class='rparen'>)</span>
|
1191
|
-
<span class='id identifier rubyid_within'>within</span><span class='period'>.</span><span class='id identifier rubyid_const_set'>const_set</span><span class='lparen'>(</span><span class='id identifier
|
1219
|
+
<span class='id identifier rubyid_within'>within</span><span class='period'>.</span><span class='id identifier rubyid_const_set'>const_set</span><span class='lparen'>(</span><span class='id identifier rubyid_named'>named</span><span class='period'>.</span><span class='id identifier rubyid_upcase'>upcase</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span><span class='rparen'>)</span>
|
1192
1220
|
|
1193
1221
|
<span class='id identifier rubyid_value'>value</span>
|
1194
1222
|
<span class='kw'>end</span></pre>
|
@@ -1202,7 +1230,7 @@ validation must give its own “validated_by” Proc.</p>
|
|
1202
1230
|
</div>
|
1203
1231
|
|
1204
1232
|
<div id="footer">
|
1205
|
-
Generated on
|
1233
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
1206
1234
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
1207
1235
|
0.9.28 (ruby-3.0.4).
|
1208
1236
|
</div>
|
data/docs/_index.html
CHANGED
@@ -233,7 +233,7 @@
|
|
233
233
|
</div>
|
234
234
|
|
235
235
|
<div id="footer">
|
236
|
-
Generated on
|
236
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
237
237
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
238
238
|
0.9.28 (ruby-3.0.4).
|
239
239
|
</div>
|
data/docs/file.README.html
CHANGED
@@ -76,13 +76,13 @@
|
|
76
76
|
<li>
|
77
77
|
<p>Add one of the following to your application’s Gemfile:
|
78
78
|
```ruby
|
79
|
-
|
80
|
-
|
79
|
+
# For on-demand usage …
|
80
|
+
#
|
81
81
|
gem ‘env_parser’</p>
|
82
82
|
|
83
|
-
<
|
84
|
-
<p
|
85
|
-
|
83
|
+
<h1 id="to-automatically-register-env">To automatically register ENV</h1>
|
84
|
+
<p># constants per “.env_parser.yml” …
|
85
|
+
#
|
86
86
|
gem ‘env_parser’, require: ‘env_parser/autoregister’
|
87
87
|
```</p>
|
88
88
|
</li>
|
@@ -103,33 +103,33 @@ $ gem install env_parser
|
|
103
103
|
<h2 id="syntax-cheat-sheet">Syntax Cheat Sheet</h2>
|
104
104
|
|
105
105
|
<p>```ruby
|
106
|
-
|
107
|
-
|
106
|
+
# Returns an ENV value parsed “as” a specific type:
|
107
|
+
#
|
108
108
|
EnvParser.parse env_key_as_a_symbol
|
109
|
-
as: …
|
110
|
-
if_unset: …
|
111
|
-
from_set: …
|
112
|
-
validated_by: ->(value) { … }
|
109
|
+
as: … # ➜ required
|
110
|
+
if_unset: … # ➜ optional; default value
|
111
|
+
from_set: … # ➜ optional; an Array or Range
|
112
|
+
validated_by: ->(value) { … } # ➜ optional; may also be given as a block</p>
|
113
113
|
|
114
|
-
<
|
115
|
-
<p
|
114
|
+
<h1 id="parse-an-env-value-and-register-it-as-a-constant">Parse an ENV value and register it as a constant:</h1>
|
115
|
+
<p>#
|
116
116
|
EnvParser.register env_key_as_a_symbol
|
117
|
-
as: …
|
118
|
-
within: …
|
119
|
-
if_unset: …
|
120
|
-
from_set: …
|
121
|
-
validated_by: ->(value) { … }
|
122
|
-
|
123
|
-
<
|
124
|
-
<p
|
125
|
-
EnvParser.autoregister
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
<
|
130
|
-
<p
|
131
|
-
EnvParser.add_env_bindings
|
132
|
-
|
117
|
+
as: … # ➜ required
|
118
|
+
within: … # ➜ optional; Class or Module
|
119
|
+
if_unset: … # ➜ optional; default value
|
120
|
+
from_set: … # ➜ optional; an Array or Range
|
121
|
+
validated_by: ->(value) { … } # ➜ optional; may also be given as a block</p>
|
122
|
+
|
123
|
+
<h1 id="registers-all-env-variables-as-speced-in-envparseryml">Registers all ENV variables as spec’ed in “.env_parser.yml”:</h1>
|
124
|
+
<p>#
|
125
|
+
EnvParser.autoregister # Note this is automatically called if your
|
126
|
+
# Gemfile included the “env_parser” gem with
|
127
|
+
# the “require: ‘env_parser/autoregister’” option.</p>
|
128
|
+
|
129
|
+
<h1 id="lets-you-call-parse-and-register-on-env-itself">Lets you call “parse” and “register” on ENV itself:</h1>
|
130
|
+
<p>#
|
131
|
+
EnvParser.add_env_bindings # ENV.parse will now be a proxy for EnvParser.parse
|
132
|
+
# and ENV.register will now be a proxy for EnvParser.register
|
133
133
|
```</p>
|
134
134
|
|
135
135
|
<h2 id="extended-how-to-use">Extended How-To-Use</h2>
|
@@ -143,9 +143,9 @@ EnvParser.add_env_bindings ## ENV.parse will now be a proxy for EnvParser.parse
|
|
143
143
|
<p>At its core, EnvParser is a straight-forward parser for string values (since that’s all <code>ENV</code> ever gives you), allowing you to read a given string <strong><em>as</em></strong> a variety of types.</p>
|
144
144
|
|
145
145
|
<p><code>ruby
|
146
|
-
|
147
|
-
|
148
|
-
|
146
|
+
# Returns ENV['TIMEOUT_MS'] as an Integer,
|
147
|
+
# or a sensible default (0) if ENV['TIMEOUT_MS'] is unset.
|
148
|
+
#
|
149
149
|
timeout_ms = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
150
150
|
</code></p>
|
151
151
|
|
@@ -157,9 +157,9 @@ timeout_ms = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
|
157
157
|
<p>EnvParser is all about ~~simplification~~ ~~less typing~~ <em>laziness</em>. If you pass in a symbol instead of a string, EnvParser will look to <code>ENV</code> and use the value from the corresponding (string) key.</p>
|
158
158
|
|
159
159
|
<p><code>ruby
|
160
|
-
|
161
|
-
|
162
|
-
|
160
|
+
# YAY, LESS TYPING! 😃
|
161
|
+
# These two are the same:
|
162
|
+
#
|
163
163
|
more_typing = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
164
164
|
less_typing = EnvParser.parse :TIMEOUT_MS, as: :integer
|
165
165
|
</code></p>
|
@@ -170,20 +170,20 @@ less_typing = EnvParser.parse :TIMEOUT_MS, as: :integer
|
|
170
170
|
<p>The <code>EnvParser.register</code> method lets you “promote” <code>ENV</code> variables into their own constants, already parsed into the correct type.</p>
|
171
171
|
|
172
172
|
<p>```ruby
|
173
|
-
ENV[‘API_KEY’]
|
173
|
+
ENV[‘API_KEY’] # => ‘unbreakable p4$$w0rd’</p>
|
174
174
|
|
175
175
|
<p>EnvParser.register :API_KEY, as: :string
|
176
|
-
API_KEY
|
176
|
+
API_KEY # => ‘unbreakable p4$$w0rd’
|
177
177
|
```</p>
|
178
178
|
|
179
179
|
<p>By default, <code>EnvParser.register</code> will create the requested constant within the Kernel module (making it available everywhere), but you can specify any class or module you like.</p>
|
180
180
|
|
181
181
|
<p>```ruby
|
182
|
-
ENV[‘BEST_VIDEO’]
|
182
|
+
ENV[‘BEST_VIDEO’] # => ‘https://youtu.be/L_jWHffIx5E’</p>
|
183
183
|
|
184
184
|
<p>EnvParser.register :BEST_VIDEO, as: :string, within: URI
|
185
|
-
URI::BEST_VIDEO
|
186
|
-
BEST_VIDEO
|
185
|
+
URI::BEST_VIDEO # => ‘https://youtu.be/L_jWHffIx5E’
|
186
|
+
BEST_VIDEO # => raises NameError
|
187
187
|
```</p>
|
188
188
|
|
189
189
|
<p>You can also register multiple constants with a single call, which is a bit cleaner.</p>
|
@@ -193,7 +193,7 @@ EnvParser.register :USERNAME, as: :string
|
|
193
193
|
EnvParser.register :PASSWORD, as: :string
|
194
194
|
EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
195
195
|
|
196
|
-
<
|
196
|
+
<h1 id="is-equivalent-to-">… is equivalent to …</h1>
|
197
197
|
|
198
198
|
<p>EnvParser.register USERNAME: { as: :string },
|
199
199
|
PASSWORD: { as: :string },
|
@@ -206,31 +206,31 @@ EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
|
206
206
|
<p>Calling <code>EnvParser.add_env_bindings</code> binds proxy <code>parse</code> and <code>register</code> methods onto <code>ENV</code>. With these bindings in place, you can call <code>parse</code> or <code>register</code> on <code>ENV</code> itself, which is more legible and feels more straight-forward.</p>
|
207
207
|
|
208
208
|
<p>```ruby
|
209
|
-
ENV[‘SHORT_PI’]
|
210
|
-
ENV[‘BETTER_PI’]
|
209
|
+
ENV[‘SHORT_PI’] # => ‘3.1415926’
|
210
|
+
ENV[‘BETTER_PI’] # => ‘[“flaky crust”, “strawberry filling”]’</p>
|
211
211
|
|
212
|
-
<
|
213
|
-
<p
|
212
|
+
<h1 id="bind-the-proxy-methods">Bind the proxy methods.</h1>
|
213
|
+
<p>#
|
214
214
|
EnvParser.add_env_bindings</p>
|
215
215
|
|
216
|
-
<p>ENV.parse :SHORT_PI, as: :float
|
217
|
-
ENV.register :BETTER_PI, as: :array
|
216
|
+
<p>ENV.parse :SHORT_PI, as: :float # => 3.1415926
|
217
|
+
ENV.register :BETTER_PI, as: :array # Your constant is set!
|
218
218
|
```</p>
|
219
219
|
|
220
220
|
<p>Note that the proxy <code>ENV.parse</code> method will (naturally) <em>always</em> interpret the value given as an <code>ENV</code> key (converting it to a string, if necessary), which is slightly different from the original <code>EnvParser.parse</code> method.</p>
|
221
221
|
|
222
222
|
<p>```ruby
|
223
|
-
ENV[‘SHORT_PI’]
|
223
|
+
ENV[‘SHORT_PI’] # => ‘3.1415926’</p>
|
224
224
|
|
225
|
-
<p>EnvParser.parse ‘SHORT_PI’, as: :float
|
226
|
-
EnvParser.parse :SHORT_PI , as: :float
|
225
|
+
<p>EnvParser.parse ‘SHORT_PI’, as: :float # => ‘SHORT_PI’ as a float: 0.0
|
226
|
+
EnvParser.parse :SHORT_PI , as: :float # => ENV[‘SHORT_PI’] as a float: 3.1415926</p>
|
227
227
|
|
228
|
-
<
|
229
|
-
<p
|
228
|
+
<h1 id="bind-the-proxy-methods-1">Bind the proxy methods.</h1>
|
229
|
+
<p>#
|
230
230
|
EnvParser.add_env_bindings</p>
|
231
231
|
|
232
|
-
<p>ENV.parse ‘SHORT_PI’, as: :float
|
233
|
-
ENV.parse :SHORT_PI , as: :float
|
232
|
+
<p>ENV.parse ‘SHORT_PI’, as: :float # => ENV[‘SHORT_PI’] as a float: 3.1415926
|
233
|
+
ENV.parse :SHORT_PI , as: :float # => ENV[‘SHORT_PI’] as a float: 3.1415926
|
234
234
|
```</p>
|
235
235
|
|
236
236
|
<p>Note also that the <code>ENV.parse</code> and <code>ENV.register</code> binding is done safely and without polluting the method space for other objects.</p>
|
@@ -248,14 +248,14 @@ ENV.parse :SHORT_PI , as: :float ## => ENV[‘SHORT_PI’] as a float: 3.141
|
|
248
248
|
<p>If the <code>ENV</code> variable you want is unset (<code>nil</code>) or blank (<code>''</code>), the return value is a sensible default for the given <strong><em>as</em></strong> type: 0 or 0.0 for numbers, an empty string/array/hash, etc. Sometimes you want a non-trivial default, however. The <strong><em>if_unset</em></strong> option lets you specify a default that better meets your needs.</p>
|
249
249
|
|
250
250
|
<p><code>ruby
|
251
|
-
ENV.parse :MISSING_VAR, as: :integer
|
252
|
-
ENV.parse :MISSING_VAR, as: :integer, if_unset: 250
|
251
|
+
ENV.parse :MISSING_VAR, as: :integer # => 0
|
252
|
+
ENV.parse :MISSING_VAR, as: :integer, if_unset: 250 # => 250
|
253
253
|
</code></p>
|
254
254
|
|
255
255
|
<p>Note these default values are used as-is with no type conversion, so exercise caution.</p>
|
256
256
|
|
257
257
|
<p><code>ruby
|
258
|
-
ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!'
|
258
|
+
ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!' # => 'Careful!' (NOT AN INTEGER)
|
259
259
|
</code></p>
|
260
260
|
</li>
|
261
261
|
<li>
|
@@ -267,9 +267,9 @@ ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!' ## => 'Careful!'
|
|
267
267
|
ENV.parse :API_TO_USE, as: :symbol, from_set: %i[internal external]
|
268
268
|
ENV.parse :NETWORK_PORT, as: :integer, from_set: (1..65535), if_unset: 80</p>
|
269
269
|
|
270
|
-
<
|
271
|
-
<p
|
272
|
-
ENV.parse :TWELVE, as: :integer, from_set: (1..5)
|
270
|
+
<h1 id="and-if-the-value-is-not-in-the-allowed-set-">And if the value is not in the allowed set …</h1>
|
271
|
+
<p>#
|
272
|
+
ENV.parse :TWELVE, as: :integer, from_set: (1..5) # => raises EnvParser::ValueNotAllowedError
|
273
273
|
```</p>
|
274
274
|
</li>
|
275
275
|
<li>
|
@@ -278,12 +278,12 @@ ENV.parse :TWELVE, as: :integer, from_set: (1..5) ## => raises EnvParser::Va
|
|
278
278
|
<p>You can write your own, more complex validations by passing in a <strong><em>validated_by</em></strong> lambda or an equivalent block. The lambda/block should take one value and return true if the given value passes the custom validation.</p>
|
279
279
|
|
280
280
|
<p>```ruby
|
281
|
-
|
282
|
-
|
281
|
+
# Via a “validated_by” lambda …
|
282
|
+
#
|
283
283
|
ENV.parse :MUST_BE_LOWERCASE, as: :string, validated_by: ->(value) { value == value.downcase }</p>
|
284
284
|
|
285
|
-
<
|
286
|
-
<p
|
285
|
+
<h1 id="or-with-a-block">… or with a block!</h1>
|
286
|
+
<p>#
|
287
287
|
ENV.parse(:MUST_BE_LOWERCASE, as: :string) { |value| value == value.downcase }
|
288
288
|
ENV.parse(:CONNECTION_RETRIES, as: :integer, &:positive?)
|
289
289
|
```</p>
|
@@ -347,7 +347,7 @@ EnvParser.register :USERNAME, as: :string
|
|
347
347
|
EnvParser.register :PASSWORD, as: :string
|
348
348
|
EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
349
349
|
|
350
|
-
<
|
350
|
+
<h1 id="is-equivalent-to--1">… is equivalent to …</h1>
|
351
351
|
|
352
352
|
<p>EnvParser.register USERNAME: { as: :string },
|
353
353
|
PASSWORD: { as: :string },
|
@@ -396,7 +396,7 @@ USERNAME:
|
|
396
396
|
</div></div>
|
397
397
|
|
398
398
|
<div id="footer">
|
399
|
-
Generated on
|
399
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
400
400
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
401
401
|
0.9.28 (ruby-3.0.4).
|
402
402
|
</div>
|
data/docs/index.html
CHANGED
@@ -76,13 +76,13 @@
|
|
76
76
|
<li>
|
77
77
|
<p>Add one of the following to your application’s Gemfile:
|
78
78
|
```ruby
|
79
|
-
|
80
|
-
|
79
|
+
# For on-demand usage …
|
80
|
+
#
|
81
81
|
gem ‘env_parser’</p>
|
82
82
|
|
83
|
-
<
|
84
|
-
<p
|
85
|
-
|
83
|
+
<h1 id="to-automatically-register-env">To automatically register ENV</h1>
|
84
|
+
<p># constants per “.env_parser.yml” …
|
85
|
+
#
|
86
86
|
gem ‘env_parser’, require: ‘env_parser/autoregister’
|
87
87
|
```</p>
|
88
88
|
</li>
|
@@ -103,33 +103,33 @@ $ gem install env_parser
|
|
103
103
|
<h2 id="syntax-cheat-sheet">Syntax Cheat Sheet</h2>
|
104
104
|
|
105
105
|
<p>```ruby
|
106
|
-
|
107
|
-
|
106
|
+
# Returns an ENV value parsed “as” a specific type:
|
107
|
+
#
|
108
108
|
EnvParser.parse env_key_as_a_symbol
|
109
|
-
as: …
|
110
|
-
if_unset: …
|
111
|
-
from_set: …
|
112
|
-
validated_by: ->(value) { … }
|
109
|
+
as: … # ➜ required
|
110
|
+
if_unset: … # ➜ optional; default value
|
111
|
+
from_set: … # ➜ optional; an Array or Range
|
112
|
+
validated_by: ->(value) { … } # ➜ optional; may also be given as a block</p>
|
113
113
|
|
114
|
-
<
|
115
|
-
<p
|
114
|
+
<h1 id="parse-an-env-value-and-register-it-as-a-constant">Parse an ENV value and register it as a constant:</h1>
|
115
|
+
<p>#
|
116
116
|
EnvParser.register env_key_as_a_symbol
|
117
|
-
as: …
|
118
|
-
within: …
|
119
|
-
if_unset: …
|
120
|
-
from_set: …
|
121
|
-
validated_by: ->(value) { … }
|
122
|
-
|
123
|
-
<
|
124
|
-
<p
|
125
|
-
EnvParser.autoregister
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
<
|
130
|
-
<p
|
131
|
-
EnvParser.add_env_bindings
|
132
|
-
|
117
|
+
as: … # ➜ required
|
118
|
+
within: … # ➜ optional; Class or Module
|
119
|
+
if_unset: … # ➜ optional; default value
|
120
|
+
from_set: … # ➜ optional; an Array or Range
|
121
|
+
validated_by: ->(value) { … } # ➜ optional; may also be given as a block</p>
|
122
|
+
|
123
|
+
<h1 id="registers-all-env-variables-as-speced-in-envparseryml">Registers all ENV variables as spec’ed in “.env_parser.yml”:</h1>
|
124
|
+
<p>#
|
125
|
+
EnvParser.autoregister # Note this is automatically called if your
|
126
|
+
# Gemfile included the “env_parser” gem with
|
127
|
+
# the “require: ‘env_parser/autoregister’” option.</p>
|
128
|
+
|
129
|
+
<h1 id="lets-you-call-parse-and-register-on-env-itself">Lets you call “parse” and “register” on ENV itself:</h1>
|
130
|
+
<p>#
|
131
|
+
EnvParser.add_env_bindings # ENV.parse will now be a proxy for EnvParser.parse
|
132
|
+
# and ENV.register will now be a proxy for EnvParser.register
|
133
133
|
```</p>
|
134
134
|
|
135
135
|
<h2 id="extended-how-to-use">Extended How-To-Use</h2>
|
@@ -143,9 +143,9 @@ EnvParser.add_env_bindings ## ENV.parse will now be a proxy for EnvParser.parse
|
|
143
143
|
<p>At its core, EnvParser is a straight-forward parser for string values (since that’s all <code>ENV</code> ever gives you), allowing you to read a given string <strong><em>as</em></strong> a variety of types.</p>
|
144
144
|
|
145
145
|
<p><code>ruby
|
146
|
-
|
147
|
-
|
148
|
-
|
146
|
+
# Returns ENV['TIMEOUT_MS'] as an Integer,
|
147
|
+
# or a sensible default (0) if ENV['TIMEOUT_MS'] is unset.
|
148
|
+
#
|
149
149
|
timeout_ms = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
150
150
|
</code></p>
|
151
151
|
|
@@ -157,9 +157,9 @@ timeout_ms = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
|
157
157
|
<p>EnvParser is all about ~~simplification~~ ~~less typing~~ <em>laziness</em>. If you pass in a symbol instead of a string, EnvParser will look to <code>ENV</code> and use the value from the corresponding (string) key.</p>
|
158
158
|
|
159
159
|
<p><code>ruby
|
160
|
-
|
161
|
-
|
162
|
-
|
160
|
+
# YAY, LESS TYPING! 😃
|
161
|
+
# These two are the same:
|
162
|
+
#
|
163
163
|
more_typing = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
164
164
|
less_typing = EnvParser.parse :TIMEOUT_MS, as: :integer
|
165
165
|
</code></p>
|
@@ -170,20 +170,20 @@ less_typing = EnvParser.parse :TIMEOUT_MS, as: :integer
|
|
170
170
|
<p>The <code>EnvParser.register</code> method lets you “promote” <code>ENV</code> variables into their own constants, already parsed into the correct type.</p>
|
171
171
|
|
172
172
|
<p>```ruby
|
173
|
-
ENV[‘API_KEY’]
|
173
|
+
ENV[‘API_KEY’] # => ‘unbreakable p4$$w0rd’</p>
|
174
174
|
|
175
175
|
<p>EnvParser.register :API_KEY, as: :string
|
176
|
-
API_KEY
|
176
|
+
API_KEY # => ‘unbreakable p4$$w0rd’
|
177
177
|
```</p>
|
178
178
|
|
179
179
|
<p>By default, <code>EnvParser.register</code> will create the requested constant within the Kernel module (making it available everywhere), but you can specify any class or module you like.</p>
|
180
180
|
|
181
181
|
<p>```ruby
|
182
|
-
ENV[‘BEST_VIDEO’]
|
182
|
+
ENV[‘BEST_VIDEO’] # => ‘https://youtu.be/L_jWHffIx5E’</p>
|
183
183
|
|
184
184
|
<p>EnvParser.register :BEST_VIDEO, as: :string, within: URI
|
185
|
-
URI::BEST_VIDEO
|
186
|
-
BEST_VIDEO
|
185
|
+
URI::BEST_VIDEO # => ‘https://youtu.be/L_jWHffIx5E’
|
186
|
+
BEST_VIDEO # => raises NameError
|
187
187
|
```</p>
|
188
188
|
|
189
189
|
<p>You can also register multiple constants with a single call, which is a bit cleaner.</p>
|
@@ -193,7 +193,7 @@ EnvParser.register :USERNAME, as: :string
|
|
193
193
|
EnvParser.register :PASSWORD, as: :string
|
194
194
|
EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
195
195
|
|
196
|
-
<
|
196
|
+
<h1 id="is-equivalent-to-">… is equivalent to …</h1>
|
197
197
|
|
198
198
|
<p>EnvParser.register USERNAME: { as: :string },
|
199
199
|
PASSWORD: { as: :string },
|
@@ -206,31 +206,31 @@ EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
|
206
206
|
<p>Calling <code>EnvParser.add_env_bindings</code> binds proxy <code>parse</code> and <code>register</code> methods onto <code>ENV</code>. With these bindings in place, you can call <code>parse</code> or <code>register</code> on <code>ENV</code> itself, which is more legible and feels more straight-forward.</p>
|
207
207
|
|
208
208
|
<p>```ruby
|
209
|
-
ENV[‘SHORT_PI’]
|
210
|
-
ENV[‘BETTER_PI’]
|
209
|
+
ENV[‘SHORT_PI’] # => ‘3.1415926’
|
210
|
+
ENV[‘BETTER_PI’] # => ‘[“flaky crust”, “strawberry filling”]’</p>
|
211
211
|
|
212
|
-
<
|
213
|
-
<p
|
212
|
+
<h1 id="bind-the-proxy-methods">Bind the proxy methods.</h1>
|
213
|
+
<p>#
|
214
214
|
EnvParser.add_env_bindings</p>
|
215
215
|
|
216
|
-
<p>ENV.parse :SHORT_PI, as: :float
|
217
|
-
ENV.register :BETTER_PI, as: :array
|
216
|
+
<p>ENV.parse :SHORT_PI, as: :float # => 3.1415926
|
217
|
+
ENV.register :BETTER_PI, as: :array # Your constant is set!
|
218
218
|
```</p>
|
219
219
|
|
220
220
|
<p>Note that the proxy <code>ENV.parse</code> method will (naturally) <em>always</em> interpret the value given as an <code>ENV</code> key (converting it to a string, if necessary), which is slightly different from the original <code>EnvParser.parse</code> method.</p>
|
221
221
|
|
222
222
|
<p>```ruby
|
223
|
-
ENV[‘SHORT_PI’]
|
223
|
+
ENV[‘SHORT_PI’] # => ‘3.1415926’</p>
|
224
224
|
|
225
|
-
<p>EnvParser.parse ‘SHORT_PI’, as: :float
|
226
|
-
EnvParser.parse :SHORT_PI , as: :float
|
225
|
+
<p>EnvParser.parse ‘SHORT_PI’, as: :float # => ‘SHORT_PI’ as a float: 0.0
|
226
|
+
EnvParser.parse :SHORT_PI , as: :float # => ENV[‘SHORT_PI’] as a float: 3.1415926</p>
|
227
227
|
|
228
|
-
<
|
229
|
-
<p
|
228
|
+
<h1 id="bind-the-proxy-methods-1">Bind the proxy methods.</h1>
|
229
|
+
<p>#
|
230
230
|
EnvParser.add_env_bindings</p>
|
231
231
|
|
232
|
-
<p>ENV.parse ‘SHORT_PI’, as: :float
|
233
|
-
ENV.parse :SHORT_PI , as: :float
|
232
|
+
<p>ENV.parse ‘SHORT_PI’, as: :float # => ENV[‘SHORT_PI’] as a float: 3.1415926
|
233
|
+
ENV.parse :SHORT_PI , as: :float # => ENV[‘SHORT_PI’] as a float: 3.1415926
|
234
234
|
```</p>
|
235
235
|
|
236
236
|
<p>Note also that the <code>ENV.parse</code> and <code>ENV.register</code> binding is done safely and without polluting the method space for other objects.</p>
|
@@ -248,14 +248,14 @@ ENV.parse :SHORT_PI , as: :float ## => ENV[‘SHORT_PI’] as a float: 3.141
|
|
248
248
|
<p>If the <code>ENV</code> variable you want is unset (<code>nil</code>) or blank (<code>''</code>), the return value is a sensible default for the given <strong><em>as</em></strong> type: 0 or 0.0 for numbers, an empty string/array/hash, etc. Sometimes you want a non-trivial default, however. The <strong><em>if_unset</em></strong> option lets you specify a default that better meets your needs.</p>
|
249
249
|
|
250
250
|
<p><code>ruby
|
251
|
-
ENV.parse :MISSING_VAR, as: :integer
|
252
|
-
ENV.parse :MISSING_VAR, as: :integer, if_unset: 250
|
251
|
+
ENV.parse :MISSING_VAR, as: :integer # => 0
|
252
|
+
ENV.parse :MISSING_VAR, as: :integer, if_unset: 250 # => 250
|
253
253
|
</code></p>
|
254
254
|
|
255
255
|
<p>Note these default values are used as-is with no type conversion, so exercise caution.</p>
|
256
256
|
|
257
257
|
<p><code>ruby
|
258
|
-
ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!'
|
258
|
+
ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!' # => 'Careful!' (NOT AN INTEGER)
|
259
259
|
</code></p>
|
260
260
|
</li>
|
261
261
|
<li>
|
@@ -267,9 +267,9 @@ ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!' ## => 'Careful!'
|
|
267
267
|
ENV.parse :API_TO_USE, as: :symbol, from_set: %i[internal external]
|
268
268
|
ENV.parse :NETWORK_PORT, as: :integer, from_set: (1..65535), if_unset: 80</p>
|
269
269
|
|
270
|
-
<
|
271
|
-
<p
|
272
|
-
ENV.parse :TWELVE, as: :integer, from_set: (1..5)
|
270
|
+
<h1 id="and-if-the-value-is-not-in-the-allowed-set-">And if the value is not in the allowed set …</h1>
|
271
|
+
<p>#
|
272
|
+
ENV.parse :TWELVE, as: :integer, from_set: (1..5) # => raises EnvParser::ValueNotAllowedError
|
273
273
|
```</p>
|
274
274
|
</li>
|
275
275
|
<li>
|
@@ -278,12 +278,12 @@ ENV.parse :TWELVE, as: :integer, from_set: (1..5) ## => raises EnvParser::Va
|
|
278
278
|
<p>You can write your own, more complex validations by passing in a <strong><em>validated_by</em></strong> lambda or an equivalent block. The lambda/block should take one value and return true if the given value passes the custom validation.</p>
|
279
279
|
|
280
280
|
<p>```ruby
|
281
|
-
|
282
|
-
|
281
|
+
# Via a “validated_by” lambda …
|
282
|
+
#
|
283
283
|
ENV.parse :MUST_BE_LOWERCASE, as: :string, validated_by: ->(value) { value == value.downcase }</p>
|
284
284
|
|
285
|
-
<
|
286
|
-
<p
|
285
|
+
<h1 id="or-with-a-block">… or with a block!</h1>
|
286
|
+
<p>#
|
287
287
|
ENV.parse(:MUST_BE_LOWERCASE, as: :string) { |value| value == value.downcase }
|
288
288
|
ENV.parse(:CONNECTION_RETRIES, as: :integer, &:positive?)
|
289
289
|
```</p>
|
@@ -347,7 +347,7 @@ EnvParser.register :USERNAME, as: :string
|
|
347
347
|
EnvParser.register :PASSWORD, as: :string
|
348
348
|
EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
349
349
|
|
350
|
-
<
|
350
|
+
<h1 id="is-equivalent-to--1">… is equivalent to …</h1>
|
351
351
|
|
352
352
|
<p>EnvParser.register USERNAME: { as: :string },
|
353
353
|
PASSWORD: { as: :string },
|
@@ -396,7 +396,7 @@ USERNAME:
|
|
396
396
|
</div></div>
|
397
397
|
|
398
398
|
<div id="footer">
|
399
|
-
Generated on
|
399
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
400
400
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
401
401
|
0.9.28 (ruby-3.0.4).
|
402
402
|
</div>
|
@@ -100,7 +100,7 @@
|
|
100
100
|
</div>
|
101
101
|
|
102
102
|
<div id="footer">
|
103
|
-
Generated on
|
103
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
104
104
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
105
105
|
0.9.28 (ruby-3.0.4).
|
106
106
|
</div>
|
data/env_parser.gemspec
CHANGED
@@ -21,11 +21,10 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.metadata['rubygems_mfa_required'] = 'true'
|
22
22
|
|
23
23
|
spec.add_dependency 'activesupport', ['>= 6.1.0', '< 7.1']
|
24
|
-
spec.add_dependency 'chronic'
|
25
|
-
spec.add_dependency 'chronic_duration'
|
24
|
+
spec.add_dependency 'chronic', '~> 0'
|
25
|
+
spec.add_dependency 'chronic_duration', '~> 0'
|
26
26
|
|
27
27
|
spec.add_development_dependency 'bundler', '~> 2'
|
28
|
-
spec.add_development_dependency 'rake'
|
29
28
|
spec.add_development_dependency 'rspec', '~> 3'
|
30
29
|
spec.add_development_dependency 'rspec_junit_formatter'
|
31
30
|
spec.add_development_dependency 'rubocop'
|
data/lib/env_parser/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: env_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
4
|
+
version: 1.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nestor Custodio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -34,28 +34,28 @@ dependencies:
|
|
34
34
|
name: chronic
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
|
-
- - "
|
37
|
+
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '0'
|
40
40
|
type: :runtime
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
|
-
- - "
|
44
|
+
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: chronic_duration
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- - "
|
51
|
+
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: '0'
|
54
54
|
type: :runtime
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- - "
|
58
|
+
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
@@ -72,20 +72,6 @@ dependencies:
|
|
72
72
|
- - "~>"
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: '2'
|
75
|
-
- !ruby/object:Gem::Dependency
|
76
|
-
name: rake
|
77
|
-
requirement: !ruby/object:Gem::Requirement
|
78
|
-
requirements:
|
79
|
-
- - ">="
|
80
|
-
- !ruby/object:Gem::Version
|
81
|
-
version: '0'
|
82
|
-
type: :development
|
83
|
-
prerelease: false
|
84
|
-
version_requirements: !ruby/object:Gem::Requirement
|
85
|
-
requirements:
|
86
|
-
- - ">="
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
version: '0'
|
89
75
|
- !ruby/object:Gem::Dependency
|
90
76
|
name: rspec
|
91
77
|
requirement: !ruby/object:Gem::Requirement
|