env_parser 1.3.3 → 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 +66 -66
- 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 +86 -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/types/internet_types.rb +22 -0
- data/lib/env_parser/version.rb +1 -1
- data/lib/env_parser.rb +20 -4
- data/spec/env_parser/types/internet_types_spec.rb +50 -0
- data/spec/env_parser_spec.rb +25 -3
- metadata +6 -20
data/docs/EnvParser.html
CHANGED
@@ -95,7 +95,7 @@
|
|
95
95
|
<dl>
|
96
96
|
<dt>Defined in:</dt>
|
97
97
|
<dd>lib/env_parser.rb<span class="defines">,<br />
|
98
|
-
lib/env_parser/errors.rb</span>
|
98
|
+
lib/env_parser/errors.rb,<br /> lib/env_parser/version.rb</span>
|
99
99
|
</dd>
|
100
100
|
</dl>
|
101
101
|
|
@@ -147,6 +147,11 @@
|
|
147
147
|
</dt>
|
148
148
|
<dd><pre class="code"><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>.env_parser.yml</span><span class='tstring_end'>'</span></span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span></pre></dd>
|
149
149
|
|
150
|
+
<dt id="VERSION-constant" class="">VERSION =
|
151
|
+
|
152
|
+
</dt>
|
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
|
+
|
150
155
|
</dl>
|
151
156
|
|
152
157
|
|
@@ -334,22 +339,22 @@ itself. i.e. ENV.parse(‘XYZ’, …) is equivalent to EnvParser.parse(ENV[‘
|
|
334
339
|
<pre class="lines">
|
335
340
|
|
336
341
|
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
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>
|
350
355
|
</td>
|
351
356
|
<td>
|
352
|
-
<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>
|
353
358
|
|
354
359
|
<span class='kw'>def</span> <span class='id identifier rubyid_add_env_bindings'>add_env_bindings</span>
|
355
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>
|
@@ -450,22 +455,6 @@ itself. i.e. ENV.parse(‘XYZ’, …) is equivalent to EnvParser.parse(ENV[‘
|
|
450
455
|
<pre class="lines">
|
451
456
|
|
452
457
|
|
453
|
-
245
|
454
|
-
246
|
455
|
-
247
|
456
|
-
248
|
457
|
-
249
|
458
|
-
250
|
459
|
-
251
|
460
|
-
252
|
461
|
-
253
|
462
|
-
254
|
463
|
-
255
|
464
|
-
256
|
465
|
-
257
|
466
|
-
258
|
467
|
-
259
|
468
|
-
260
|
469
458
|
261
|
470
459
|
262
|
471
460
|
263
|
@@ -474,10 +463,26 @@ itself. i.e. ENV.parse(‘XYZ’, …) is equivalent to EnvParser.parse(ENV[‘
|
|
474
463
|
266
|
475
464
|
267
|
476
465
|
268
|
477
|
-
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>
|
478
483
|
</td>
|
479
484
|
<td>
|
480
|
-
<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>
|
481
486
|
|
482
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>
|
483
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>
|
@@ -485,7 +490,7 @@ itself. i.e. ENV.parse(‘XYZ’, …) is equivalent to EnvParser.parse(ENV[‘
|
|
485
490
|
|
486
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>
|
487
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>
|
488
|
-
<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>
|
489
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>
|
490
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>
|
491
496
|
|
@@ -943,12 +948,12 @@ both validation checks must pass.</strong></p>
|
|
943
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>
|
944
949
|
|
945
950
|
<pre class="code ruby"><code class="ruby">
|
946
|
-
|
951
|
+
# Example shortcut usage:
|
947
952
|
|
948
953
|
EnvParser.register :A, from: one_hash, as: :integer
|
949
954
|
EnvParser.register :B, from: another_hash, as: :string, if_unset: 'none'
|
950
955
|
|
951
|
-
|
956
|
+
# ... is equivalent to ...
|
952
957
|
|
953
958
|
EnvParser.register(
|
954
959
|
A: { from: one_hash, as: :integer }
|
@@ -1021,6 +1026,28 @@ variable names and whose values are the options set for each variable’s <span
|
|
1021
1026
|
|
1022
1027
|
</li>
|
1023
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
|
+
|
1024
1051
|
<li>
|
1025
1052
|
<span class="name">within</span>
|
1026
1053
|
<span class="type">(<tt>Module</tt>, <tt>Class</tt>)</span>
|
@@ -1127,19 +1154,6 @@ validation must give its own “validated_by” Proc.</p>
|
|
1127
1154
|
<pre class="lines">
|
1128
1155
|
|
1129
1156
|
|
1130
|
-
178
|
1131
|
-
179
|
1132
|
-
180
|
1133
|
-
181
|
1134
|
-
182
|
1135
|
-
183
|
1136
|
-
184
|
1137
|
-
185
|
1138
|
-
186
|
1139
|
-
187
|
1140
|
-
188
|
1141
|
-
189
|
1142
|
-
190
|
1143
1157
|
191
|
1144
1158
|
192
|
1145
1159
|
193
|
@@ -1154,10 +1168,26 @@ validation must give its own “validated_by” Proc.</p>
|
|
1154
1168
|
202
|
1155
1169
|
203
|
1156
1170
|
204
|
1157
|
-
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>
|
1158
1188
|
</td>
|
1159
1189
|
<td>
|
1160
|
-
<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>
|
1161
1191
|
|
1162
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>
|
1163
1193
|
<span class='comment'># Allow for registering multiple variables simultaneously via a single call.
|
@@ -1169,6 +1199,9 @@ validation must give its own “validated_by” Proc.</p>
|
|
1169
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>
|
1170
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>
|
1171
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
|
+
|
1172
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
|
1173
1206
|
</span> <span class='comment'># bite you in some cases. Making sure we're working with a straight-up Hash saves a lot of
|
1174
1207
|
</span> <span class='comment'># sanity checks later on. This is also a good place to make sure we're working with a String
|
@@ -1183,7 +1216,7 @@ validation must give its own “validated_by” Proc.</p>
|
|
1183
1216
|
|
1184
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>
|
1185
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>
|
1186
|
-
<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>
|
1187
1220
|
|
1188
1221
|
<span class='id identifier rubyid_value'>value</span>
|
1189
1222
|
<span class='kw'>end</span></pre>
|
@@ -1197,7 +1230,7 @@ validation must give its own “validated_by” Proc.</p>
|
|
1197
1230
|
</div>
|
1198
1231
|
|
1199
1232
|
<div id="footer">
|
1200
|
-
Generated on
|
1233
|
+
Generated on Fri Dec 30 17:49:58 2022 by
|
1201
1234
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
1202
1235
|
0.9.28 (ruby-3.0.4).
|
1203
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>
|