env_parser 1.6.0 → 1.6.2
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/.circleci/config.yml +3 -3
- data/.ruby-version +1 -1
- data/Gemfile.lock +64 -44
- data/README.md +27 -14
- data/docs/EnvParser/AutoregisterFileNotFound.html +3 -3
- data/docs/EnvParser/Error.html +3 -3
- data/docs/EnvParser/TypeAlreadyDefinedError.html +3 -3
- data/docs/EnvParser/Types/BaseTypes.html +3 -3
- data/docs/EnvParser/Types/ChronologyTypes.html +3 -3
- data/docs/EnvParser/Types/InternetTypes.html +15 -3
- data/docs/EnvParser/Types.html +3 -3
- data/docs/EnvParser/UnknownTypeError.html +3 -3
- data/docs/EnvParser/UnparseableAutoregisterSpec.html +3 -3
- data/docs/EnvParser/ValueNotAllowedError.html +3 -3
- data/docs/EnvParser/ValueNotConvertibleError.html +3 -3
- data/docs/EnvParser.html +83 -55
- data/docs/_index.html +4 -4
- data/docs/class_list.html +6 -3
- data/docs/css/full_list.css +3 -3
- data/docs/css/style.css +6 -0
- data/docs/file.README.html +89 -76
- data/docs/file_list.html +5 -2
- data/docs/frames.html +10 -5
- data/docs/index.html +89 -76
- data/docs/js/app.js +294 -264
- data/docs/js/full_list.js +30 -4
- data/docs/method_list.html +5 -2
- data/docs/top-level-namespace.html +3 -3
- data/env_parser.gemspec +5 -6
- data/lib/env_parser/version.rb +1 -1
- metadata +12 -33
data/docs/file.README.html
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
<title>
|
7
7
|
File: README
|
8
8
|
|
9
|
-
— Documentation by YARD 0.9.
|
9
|
+
— Documentation by YARD 0.9.37
|
10
10
|
|
11
11
|
</title>
|
12
12
|
|
@@ -58,7 +58,7 @@
|
|
58
58
|
</div>
|
59
59
|
|
60
60
|
<div id="content"><div id='filecontents'><p><a href="https://rubygems.org/gems/env_parser"><img src="https://img.shields.io/github/v/release/nestor-custodio/env_parser?color=green&label=gem%20version" alt="Gem Version" /></a>
|
61
|
-
<a href="https://
|
61
|
+
<a href="https://tldrlegal.com/license/mit-license"><img src="https://img.shields.io/github/license/nestor-custodio/env_parser" alt="MIT License" /></a></p>
|
62
62
|
|
63
63
|
<h1 id="envparser">EnvParser</h1>
|
64
64
|
|
@@ -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,34 @@ $ gem install env_parser
|
|
103
103
|
<h2 id="syntax-cheat-sheet">Syntax Cheat Sheet</h2>
|
104
104
|
|
105
105
|
<p>```ruby
|
106
|
-
|
107
|
-
|
108
|
-
EnvParser.parse env_key_as_a_symbol
|
109
|
-
as:
|
110
|
-
if_unset:
|
111
|
-
from_set:
|
112
|
-
validated_by: ->(value) { … }
|
113
|
-
|
114
|
-
<
|
115
|
-
<p
|
116
|
-
EnvParser.register env_key_as_a_symbol
|
117
|
-
as:
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
<
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
<
|
131
|
-
|
132
|
-
|
106
|
+
# Returns an ENV value parsed “as” a specific type:
|
107
|
+
#
|
108
|
+
EnvParser.parse env_key_as_a_symbol,
|
109
|
+
as: …, # ➜ required; Symbol
|
110
|
+
if_unset: …, # ➜ optional; default value (of any type)
|
111
|
+
from_set: …, # ➜ optional; Array or Range
|
112
|
+
validated_by: ->(value) { … } # ➜ optional; may also be given as a block</p>
|
113
|
+
|
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
|
+
EnvParser.register env_key_as_a_symbol,
|
117
|
+
as: …, # ➜ required; Symbol
|
118
|
+
named: …, # ➜ optional; String or Symbol; available only if <code>within</code> is also given
|
119
|
+
within: …, # ➜ optional; Class or Module
|
120
|
+
if_unset: …, # ➜ optional; default value (of any type)
|
121
|
+
from_set: …, # ➜ optional; Array or Range
|
122
|
+
validated_by: ->(value) { … } # ➜ optional; may also be given as a block</p>
|
123
|
+
|
124
|
+
<h1 id="registers-all-env-variables-as-speced-in-envparseryml">Registers all ENV variables as spec’ed in “.env_parser.yml”:</h1>
|
125
|
+
<p>#
|
126
|
+
EnvParser.autoregister # Note this is automatically called if your
|
127
|
+
# Gemfile included the “env_parser” gem with
|
128
|
+
# the “require: ‘env_parser/autoregister’” option.</p>
|
129
|
+
|
130
|
+
<h1 id="lets-you-call-parse-and-register-on-env-itself">Lets you call “parse” and “register” on ENV itself:</h1>
|
131
|
+
<p>#
|
132
|
+
EnvParser.add_env_bindings # ENV.parse will now be a proxy for EnvParser.parse
|
133
|
+
# and ENV.register will now be a proxy for EnvParser.register
|
133
134
|
```</p>
|
134
135
|
|
135
136
|
<h2 id="extended-how-to-use">Extended How-To-Use</h2>
|
@@ -143,9 +144,9 @@ EnvParser.add_env_bindings ## ENV.parse will now be a proxy for EnvParser.parse
|
|
143
144
|
<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
145
|
|
145
146
|
<p><code>ruby
|
146
|
-
|
147
|
-
|
148
|
-
|
147
|
+
# Returns ENV['TIMEOUT_MS'] as an Integer,
|
148
|
+
# or a sensible default (0) if ENV['TIMEOUT_MS'] is unset.
|
149
|
+
#
|
149
150
|
timeout_ms = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
150
151
|
</code></p>
|
151
152
|
|
@@ -157,9 +158,9 @@ timeout_ms = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
|
157
158
|
<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
159
|
|
159
160
|
<p><code>ruby
|
160
|
-
|
161
|
-
|
162
|
-
|
161
|
+
# YAY, LESS TYPING! 😃
|
162
|
+
# These two are the same:
|
163
|
+
#
|
163
164
|
more_typing = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
164
165
|
less_typing = EnvParser.parse :TIMEOUT_MS, as: :integer
|
165
166
|
</code></p>
|
@@ -170,20 +171,32 @@ less_typing = EnvParser.parse :TIMEOUT_MS, as: :integer
|
|
170
171
|
<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
172
|
|
172
173
|
<p>```ruby
|
173
|
-
ENV[‘API_KEY’]
|
174
|
+
ENV[‘API_KEY’] # => ‘unbreakable p4$$w0rd’</p>
|
174
175
|
|
175
176
|
<p>EnvParser.register :API_KEY, as: :string
|
176
|
-
API_KEY
|
177
|
+
API_KEY # => ‘unbreakable p4$$w0rd’
|
177
178
|
```</p>
|
178
179
|
|
179
180
|
<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
181
|
|
181
182
|
<p>```ruby
|
182
|
-
ENV[‘BEST_VIDEO’]
|
183
|
+
ENV[‘BEST_VIDEO’] # => ‘https://youtu.be/L_jWHffIx5E’</p>
|
183
184
|
|
184
185
|
<p>EnvParser.register :BEST_VIDEO, as: :string, within: URI
|
185
|
-
URI::BEST_VIDEO
|
186
|
-
BEST_VIDEO
|
186
|
+
URI::BEST_VIDEO # => ‘https://youtu.be/L_jWHffIx5E’
|
187
|
+
BEST_VIDEO # => raises NameError
|
188
|
+
```</p>
|
189
|
+
|
190
|
+
<p><code>EnvParser.register</code>’s <strong><em>within</em></strong> option also allows for specifying what you would like the registered constant to be <strong><em>named</em></strong>, since related ENV variables will tend to have redundant names once namespaced within a single class or module. Note that <code>named</code> is only available when used alongside <code>within</code>, as it exists solely as a namespacing aid; registering ENV variables as <em>global</em> constants with different names would be a debugging nightmare.</p>
|
191
|
+
|
192
|
+
<p>```ruby
|
193
|
+
ENV[‘CUSTOM_CLIENT_DEFAULT_HOSTNAME’] # => ‘localhost’
|
194
|
+
ENV[‘CUSTOM_CLIENT_DEFAULT_PORT’ ] # => ‘3000’</p>
|
195
|
+
|
196
|
+
<p>EnvParser.register :CUSTOM_CLIENT_DEFAULT_HOSTNAME, as: :string , named: :DEFAULT_HOSTNAME, within: CustomClient
|
197
|
+
EnvParser.register :CUSTOM_CLIENT_DEFAULT_PORT , as: :integer, named: :DEFAULT_PORT , within: CustomClient
|
198
|
+
CustomClient::DEFAULT_HOSTNAME # => ‘localhost’
|
199
|
+
CustomClient::DEFAULT_PORT # => 3000
|
187
200
|
```</p>
|
188
201
|
|
189
202
|
<p>You can also register multiple constants with a single call, which is a bit cleaner.</p>
|
@@ -193,7 +206,7 @@ EnvParser.register :USERNAME, as: :string
|
|
193
206
|
EnvParser.register :PASSWORD, as: :string
|
194
207
|
EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
195
208
|
|
196
|
-
<
|
209
|
+
<h1 id="is-equivalent-to-">… is equivalent to …</h1>
|
197
210
|
|
198
211
|
<p>EnvParser.register USERNAME: { as: :string },
|
199
212
|
PASSWORD: { as: :string },
|
@@ -206,31 +219,31 @@ EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
|
206
219
|
<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
220
|
|
208
221
|
<p>```ruby
|
209
|
-
ENV[‘SHORT_PI’]
|
210
|
-
ENV[‘BETTER_PI’]
|
222
|
+
ENV[‘SHORT_PI’] # => ‘3.1415926’
|
223
|
+
ENV[‘BETTER_PI’] # => ‘[“flaky crust”, “strawberry filling”]’</p>
|
211
224
|
|
212
|
-
<
|
213
|
-
<p
|
225
|
+
<h1 id="bind-the-proxy-methods">Bind the proxy methods.</h1>
|
226
|
+
<p>#
|
214
227
|
EnvParser.add_env_bindings</p>
|
215
228
|
|
216
|
-
<p>ENV.parse :SHORT_PI, as: :float
|
217
|
-
ENV.register :BETTER_PI, as: :array
|
229
|
+
<p>ENV.parse :SHORT_PI, as: :float # => 3.1415926
|
230
|
+
ENV.register :BETTER_PI, as: :array # Your constant is set!
|
218
231
|
```</p>
|
219
232
|
|
220
233
|
<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
234
|
|
222
235
|
<p>```ruby
|
223
|
-
ENV[‘SHORT_PI’]
|
236
|
+
ENV[‘SHORT_PI’] # => ‘3.1415926’</p>
|
224
237
|
|
225
|
-
<p>EnvParser.parse ‘SHORT_PI’, as: :float
|
226
|
-
EnvParser.parse :SHORT_PI , as: :float
|
238
|
+
<p>EnvParser.parse ‘SHORT_PI’, as: :float # => ‘SHORT_PI’ as a float: 0.0
|
239
|
+
EnvParser.parse :SHORT_PI , as: :float # => ENV[‘SHORT_PI’] as a float: 3.1415926</p>
|
227
240
|
|
228
|
-
<
|
229
|
-
<p
|
241
|
+
<h1 id="bind-the-proxy-methods-1">Bind the proxy methods.</h1>
|
242
|
+
<p>#
|
230
243
|
EnvParser.add_env_bindings</p>
|
231
244
|
|
232
|
-
<p>ENV.parse ‘SHORT_PI’, as: :float
|
233
|
-
ENV.parse :SHORT_PI , as: :float
|
245
|
+
<p>ENV.parse ‘SHORT_PI’, as: :float # => ENV[‘SHORT_PI’] as a float: 3.1415926
|
246
|
+
ENV.parse :SHORT_PI , as: :float # => ENV[‘SHORT_PI’] as a float: 3.1415926
|
234
247
|
```</p>
|
235
248
|
|
236
249
|
<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 +261,14 @@ ENV.parse :SHORT_PI , as: :float ## => ENV[‘SHORT_PI’] as a float: 3.141
|
|
248
261
|
<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
262
|
|
250
263
|
<p><code>ruby
|
251
|
-
ENV.parse :MISSING_VAR, as: :integer
|
252
|
-
ENV.parse :MISSING_VAR, as: :integer, if_unset: 250
|
264
|
+
ENV.parse :MISSING_VAR, as: :integer # => 0
|
265
|
+
ENV.parse :MISSING_VAR, as: :integer, if_unset: 250 # => 250
|
253
266
|
</code></p>
|
254
267
|
|
255
|
-
<p>Note these default values are used as-is with no type conversion, so exercise caution.</p>
|
268
|
+
<p>Note these default values are used as-is, with no type conversion (because sometimes you just want <code>nil</code> 🤷), so exercise caution.</p>
|
256
269
|
|
257
270
|
<p><code>ruby
|
258
|
-
ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!'
|
271
|
+
ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!' # => 'Careful!' (NOT AN INTEGER)
|
259
272
|
</code></p>
|
260
273
|
</li>
|
261
274
|
<li>
|
@@ -267,23 +280,23 @@ ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!' ## => 'Careful!'
|
|
267
280
|
ENV.parse :API_TO_USE, as: :symbol, from_set: %i[internal external]
|
268
281
|
ENV.parse :NETWORK_PORT, as: :integer, from_set: (1..65535), if_unset: 80</p>
|
269
282
|
|
270
|
-
<
|
271
|
-
<p
|
272
|
-
ENV.parse :TWELVE, as: :integer, from_set: (1..5)
|
283
|
+
<h1 id="and-if-the-value-is-not-in-the-allowed-set-">And if the value is not in the allowed set …</h1>
|
284
|
+
<p>#
|
285
|
+
ENV.parse :TWELVE, as: :integer, from_set: (1..5) # => raises EnvParser::ValueNotAllowedError
|
273
286
|
```</p>
|
274
287
|
</li>
|
275
288
|
<li>
|
276
289
|
<p><strong>Custom Validation Of Parsed Values</strong></p>
|
277
290
|
|
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
|
291
|
+
<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 expect one value (of the requested <strong><em>as</em></strong> type) and return true if the given value passes the custom validation.</p>
|
279
292
|
|
280
293
|
<p>```ruby
|
281
|
-
|
282
|
-
|
294
|
+
# Via a “validated_by” lambda …
|
295
|
+
#
|
283
296
|
ENV.parse :MUST_BE_LOWERCASE, as: :string, validated_by: ->(value) { value == value.downcase }</p>
|
284
297
|
|
285
|
-
<
|
286
|
-
<p
|
298
|
+
<h1 id="or-with-a-block">… or with a block!</h1>
|
299
|
+
<p>#
|
287
300
|
ENV.parse(:MUST_BE_LOWERCASE, as: :string) { |value| value == value.downcase }
|
288
301
|
ENV.parse(:CONNECTION_RETRIES, as: :integer, &:positive?)
|
289
302
|
```</p>
|
@@ -327,7 +340,7 @@ b = ENV.parse :B, as: :my_special_type_of_number
|
|
327
340
|
<li>
|
328
341
|
<p><strong>The <code>autoregister</code> Call</strong></p>
|
329
342
|
|
330
|
-
<p>Consolidating all of your <code>EnvParser.register</code> calls into a single place only makes sense. A single <code>EnvParser.autoregister</code> call
|
343
|
+
<p>Consolidating all of your <code>EnvParser.register</code> calls into a single place only makes sense. A single <code>EnvParser.autoregister</code> call takes a filename to read and process as a series of constant registration requests. If no filename is given, the default <code>".env_parser.yml"</code> is assumed.</p>
|
331
344
|
|
332
345
|
<p>You’ll normally want to call <code>EnvParser.autoregister</code> as early in your application as possible. For Rails applications (and other frameworks that call <code>require 'bundler/setup'</code>), requiring the EnvParser gem via …</p>
|
333
346
|
|
@@ -347,7 +360,7 @@ EnvParser.register :USERNAME, as: :string
|
|
347
360
|
EnvParser.register :PASSWORD, as: :string
|
348
361
|
EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
349
362
|
|
350
|
-
<
|
363
|
+
<h1 id="is-equivalent-to--1">… is equivalent to …</h1>
|
351
364
|
|
352
365
|
<p>EnvParser.register USERNAME: { as: :string },
|
353
366
|
PASSWORD: { as: :string },
|
@@ -370,7 +383,7 @@ USERNAME:
|
|
370
383
|
within: MyClassOrModule
|
371
384
|
```</p>
|
372
385
|
|
373
|
-
<p>Because no Ruby <em>statements</em> can be safely represented via YAML, the set of <code>EnvParser.register</code> options available via autoregistration is limited to <strong><em>as</em></strong>, <strong><em>within</em></strong>, <strong><em>if_unset</em></strong>, and <strong><em>from_set</em></strong>. As an additional restriction, <strong><em>from_set</em></strong> (if given) must be an array, as ranges cannot be represented in YAML.</p>
|
386
|
+
<p>Because no Ruby <em>statements</em> can be safely represented via YAML, the set of <code>EnvParser.register</code> options available via autoregistration is limited to <strong><em>as</em></strong>, <strong><em>named</em></strong>, <strong><em>within</em></strong>, <strong><em>if_unset</em></strong>, and <strong><em>from_set</em></strong>. As an additional restriction, <strong><em>from_set</em></strong> (if given) must be an array, as ranges cannot be represented in YAML.</p>
|
374
387
|
</li>
|
375
388
|
</ul>
|
376
389
|
|
@@ -396,9 +409,9 @@ USERNAME:
|
|
396
409
|
</div></div>
|
397
410
|
|
398
411
|
<div id="footer">
|
399
|
-
Generated on
|
412
|
+
Generated on Mon Jun 9 14:00:25 2025 by
|
400
413
|
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
401
|
-
0.9.
|
414
|
+
0.9.37 (ruby-3.4.2).
|
402
415
|
</div>
|
403
416
|
|
404
417
|
</div>
|
data/docs/file_list.html
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
<!DOCTYPE html>
|
2
|
-
<html>
|
2
|
+
<html >
|
3
3
|
<head>
|
4
4
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
5
5
|
<meta charset="utf-8" />
|
@@ -38,7 +38,10 @@
|
|
38
38
|
|
39
39
|
</div>
|
40
40
|
|
41
|
-
<div id="search">
|
41
|
+
<div id="search">
|
42
|
+
<label for="search-class">Search:</label>
|
43
|
+
<input id="search-class" type="text" />
|
44
|
+
</div>
|
42
45
|
</div>
|
43
46
|
|
44
47
|
<ul id="full_list" class="file">
|
data/docs/frames.html
CHANGED
@@ -2,13 +2,18 @@
|
|
2
2
|
<html>
|
3
3
|
<head>
|
4
4
|
<meta charset="utf-8">
|
5
|
-
<title>Documentation by YARD 0.9.
|
5
|
+
<title>Documentation by YARD 0.9.37</title>
|
6
6
|
</head>
|
7
7
|
<script type="text/javascript">
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
var mainUrl = 'index.html';
|
9
|
+
try {
|
10
|
+
var match = decodeURIComponent(window.location.hash).match(/^#!(.+)/);
|
11
|
+
var name = match ? match[1] : mainUrl;
|
12
|
+
var url = new URL(name, location.href);
|
13
|
+
window.top.location.replace(url.origin === location.origin ? name : mainUrl);
|
14
|
+
} catch (e) {
|
15
|
+
window.top.location.replace(mainUrl);
|
16
|
+
}
|
12
17
|
</script>
|
13
18
|
<noscript>
|
14
19
|
<h1>Oops!</h1>
|