env_parser 1.6.2 → 1.7.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/Gemfile +6 -4
- data/Gemfile.lock +104 -30
- data/README.md +2 -2
- data/env_parser.gemspec +19 -12
- data/lib/env_parser/types/chronology_types.rb +1 -1
- data/lib/env_parser/types/internet_types.rb +3 -2
- data/lib/env_parser/version.rb +1 -1
- data/lib/env_parser.rb +18 -18
- metadata +5 -119
- data/.circleci/config.yml +0 -63
- data/.gitignore +0 -14
- data/.rspec +0 -3
- data/.rubocop.yml +0 -64
- data/.ruby-version +0 -1
- data/.yardopts +0 -1
- data/bin/console +0 -14
- data/bin/setup +0 -8
- data/docs/EnvParser/AutoregisterFileNotFound.html +0 -143
- data/docs/EnvParser/Error.html +0 -138
- data/docs/EnvParser/TypeAlreadyDefined.html +0 -143
- data/docs/EnvParser/TypeAlreadyDefinedError.html +0 -142
- data/docs/EnvParser/Types/BaseTypes.html +0 -179
- data/docs/EnvParser/Types/ChronologyTypes.html +0 -160
- data/docs/EnvParser/Types/InternetTypes.html +0 -172
- data/docs/EnvParser/Types.html +0 -128
- data/docs/EnvParser/UnknownTypeError.html +0 -142
- data/docs/EnvParser/UnparseableAutoregisterSpec.html +0 -143
- data/docs/EnvParser/ValueNotAllowed.html +0 -144
- data/docs/EnvParser/ValueNotAllowedError.html +0 -144
- data/docs/EnvParser/ValueNotConvertibleError.html +0 -142
- data/docs/EnvParser.html +0 -1240
- data/docs/EnvParserTypes/BaseTypes.html +0 -178
- data/docs/EnvParserTypes/ChronologyTypes.html +0 -159
- data/docs/EnvParserTypes/InternetTypes.html +0 -159
- data/docs/EnvParserTypes/TimeTypes.html +0 -158
- data/docs/EnvParserTypes.html +0 -129
- data/docs/_index.html +0 -243
- data/docs/class_list.html +0 -54
- data/docs/css/common.css +0 -1
- data/docs/css/full_list.css +0 -58
- data/docs/css/style.css +0 -503
- data/docs/file.README.html +0 -419
- data/docs/file_list.html +0 -59
- data/docs/frames.html +0 -22
- data/docs/index.html +0 -419
- data/docs/js/app.js +0 -344
- data/docs/js/full_list.js +0 -242
- data/docs/js/jquery.js +0 -4
- data/docs/method_list.html +0 -94
- data/docs/top-level-namespace.html +0 -110
- data/spec/env_parser/types/base_types_spec.rb +0 -98
- data/spec/env_parser/types/chronology_types_spec.rb +0 -49
- data/spec/env_parser/types/internet_types_spec.rb +0 -95
- data/spec/env_parser_spec.rb +0 -214
- data/spec/spec_helper.rb +0 -14
data/docs/file.README.html
DELETED
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html>
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8">
|
|
5
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
-
<title>
|
|
7
|
-
File: README
|
|
8
|
-
|
|
9
|
-
— Documentation by YARD 0.9.37
|
|
10
|
-
|
|
11
|
-
</title>
|
|
12
|
-
|
|
13
|
-
<link rel="stylesheet" href="css/style.css" type="text/css" />
|
|
14
|
-
|
|
15
|
-
<link rel="stylesheet" href="css/common.css" type="text/css" />
|
|
16
|
-
|
|
17
|
-
<script type="text/javascript">
|
|
18
|
-
pathId = "README";
|
|
19
|
-
relpath = '';
|
|
20
|
-
</script>
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
|
24
|
-
|
|
25
|
-
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
</head>
|
|
29
|
-
<body>
|
|
30
|
-
<div class="nav_wrap">
|
|
31
|
-
<iframe id="nav" src="file_list.html?1"></iframe>
|
|
32
|
-
<div id="resizer"></div>
|
|
33
|
-
</div>
|
|
34
|
-
|
|
35
|
-
<div id="main" tabindex="-1">
|
|
36
|
-
<div id="header">
|
|
37
|
-
<div id="menu">
|
|
38
|
-
|
|
39
|
-
<a href="_index.html">Index</a> »
|
|
40
|
-
<span class="title">File: README</span>
|
|
41
|
-
|
|
42
|
-
</div>
|
|
43
|
-
|
|
44
|
-
<div id="search">
|
|
45
|
-
|
|
46
|
-
<a class="full_list_link" id="class_list_link"
|
|
47
|
-
href="class_list.html">
|
|
48
|
-
|
|
49
|
-
<svg width="24" height="24">
|
|
50
|
-
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
|
|
51
|
-
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
|
|
52
|
-
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
|
|
53
|
-
</svg>
|
|
54
|
-
</a>
|
|
55
|
-
|
|
56
|
-
</div>
|
|
57
|
-
<div class="clear"></div>
|
|
58
|
-
</div>
|
|
59
|
-
|
|
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://tldrlegal.com/license/mit-license"><img src="https://img.shields.io/github/license/nestor-custodio/env_parser" alt="MIT License" /></a></p>
|
|
62
|
-
|
|
63
|
-
<h1 id="envparser">EnvParser</h1>
|
|
64
|
-
|
|
65
|
-
<p>If your code uses environment variables, you know that <code>ENV</code> will always surface these as strings. Interpreting these strings as the value you <em>actually</em> want to see/use takes some work, however: for numbers you need to cast with <code>to_i</code> or <code>to_f</code> … for booleans you need to check for a specific value (<code>ENV['SOME_VAR'] == 'true'</code>) … maybe you want to set non-trivial defaults (something other than <code>0</code> or <code>''</code>)? … maybe you only want to allow values from a limited set? …</p>
|
|
66
|
-
|
|
67
|
-
<p>Things can get out of control pretty fast, especially as the number of environment variables in play grows. Tools like <a href="https://github.com/bkeepers/dotenv">dotenv</a> help to make sure you’re loading the correct <strong>set</strong> of variables, but <a href="https://github.com/nestor-custodio/env_parser">EnvParser</a> makes <strong>the values themselves</strong> usable with a minimum of effort.</p>
|
|
68
|
-
|
|
69
|
-
<p><a href="http://nestor-custodio.github.io/env_parser/EnvParser.html">Full documentation is available here</a>, but do read below for a crash course on availble featues!</p>
|
|
70
|
-
|
|
71
|
-
<h2 id="installation">Installation</h2>
|
|
72
|
-
|
|
73
|
-
<ul>
|
|
74
|
-
<li>If your project uses <a href="https://github.com/bundler/bundler">Bundler</a>:
|
|
75
|
-
<ul>
|
|
76
|
-
<li>
|
|
77
|
-
<p>Add one of the following to your application’s Gemfile:
|
|
78
|
-
```ruby
|
|
79
|
-
# For on-demand usage …
|
|
80
|
-
#
|
|
81
|
-
gem ‘env_parser’</p>
|
|
82
|
-
|
|
83
|
-
<h1 id="to-automatically-register-env">To automatically register ENV</h1>
|
|
84
|
-
<p># constants per “.env_parser.yml” …
|
|
85
|
-
#
|
|
86
|
-
gem ‘env_parser’, require: ‘env_parser/autoregister’
|
|
87
|
-
```</p>
|
|
88
|
-
</li>
|
|
89
|
-
<li>
|
|
90
|
-
<p>And then run a:
|
|
91
|
-
<code>shell
|
|
92
|
-
$ bundle install
|
|
93
|
-
</code></p>
|
|
94
|
-
</li>
|
|
95
|
-
</ul>
|
|
96
|
-
</li>
|
|
97
|
-
<li>Or, you can keep things simple with a manual install:
|
|
98
|
-
<code>shell
|
|
99
|
-
$ gem install env_parser
|
|
100
|
-
</code></li>
|
|
101
|
-
</ul>
|
|
102
|
-
|
|
103
|
-
<h2 id="syntax-cheat-sheet">Syntax Cheat Sheet</h2>
|
|
104
|
-
|
|
105
|
-
<p>```ruby
|
|
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
|
|
134
|
-
```</p>
|
|
135
|
-
|
|
136
|
-
<h2 id="extended-how-to-use">Extended How-To-Use</h2>
|
|
137
|
-
|
|
138
|
-
<h4 id="basic-usage">Basic Usage</h4>
|
|
139
|
-
|
|
140
|
-
<ul>
|
|
141
|
-
<li>
|
|
142
|
-
<p><strong>Parsing <code>ENV</code> Values</strong></p>
|
|
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>
|
|
145
|
-
|
|
146
|
-
<p><code>ruby
|
|
147
|
-
# Returns ENV['TIMEOUT_MS'] as an Integer,
|
|
148
|
-
# or a sensible default (0) if ENV['TIMEOUT_MS'] is unset.
|
|
149
|
-
#
|
|
150
|
-
timeout_ms = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
|
151
|
-
</code></p>
|
|
152
|
-
|
|
153
|
-
<p>You can check the full documentation for <a href="http://nestor-custodio.github.io/env_parser/EnvParser/Types.html">a list of all <strong><em>as</em></strong> types available right out of the box</a>.</p>
|
|
154
|
-
</li>
|
|
155
|
-
<li>
|
|
156
|
-
<p><strong>How About Less Typing?</strong></p>
|
|
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>
|
|
159
|
-
|
|
160
|
-
<p><code>ruby
|
|
161
|
-
# YAY, LESS TYPING! 😃
|
|
162
|
-
# These two are the same:
|
|
163
|
-
#
|
|
164
|
-
more_typing = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
|
165
|
-
less_typing = EnvParser.parse :TIMEOUT_MS, as: :integer
|
|
166
|
-
</code></p>
|
|
167
|
-
</li>
|
|
168
|
-
<li>
|
|
169
|
-
<p><strong>Registering Constants From <code>ENV</code> Values</strong></p>
|
|
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>
|
|
172
|
-
|
|
173
|
-
<p>```ruby
|
|
174
|
-
ENV[‘API_KEY’] # => ‘unbreakable p4$$w0rd’</p>
|
|
175
|
-
|
|
176
|
-
<p>EnvParser.register :API_KEY, as: :string
|
|
177
|
-
API_KEY # => ‘unbreakable p4$$w0rd’
|
|
178
|
-
```</p>
|
|
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>
|
|
181
|
-
|
|
182
|
-
<p>```ruby
|
|
183
|
-
ENV[‘BEST_VIDEO’] # => ‘https://youtu.be/L_jWHffIx5E’</p>
|
|
184
|
-
|
|
185
|
-
<p>EnvParser.register :BEST_VIDEO, as: :string, within: URI
|
|
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
|
|
200
|
-
```</p>
|
|
201
|
-
|
|
202
|
-
<p>You can also register multiple constants with a single call, which is a bit cleaner.</p>
|
|
203
|
-
|
|
204
|
-
<p>```ruby
|
|
205
|
-
EnvParser.register :USERNAME, as: :string
|
|
206
|
-
EnvParser.register :PASSWORD, as: :string
|
|
207
|
-
EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
|
208
|
-
|
|
209
|
-
<h1 id="is-equivalent-to-">… is equivalent to …</h1>
|
|
210
|
-
|
|
211
|
-
<p>EnvParser.register USERNAME: { as: :string },
|
|
212
|
-
PASSWORD: { as: :string },
|
|
213
|
-
MOCK_API: { as: :boolean, within: MyClassOrModule }
|
|
214
|
-
```</p>
|
|
215
|
-
</li>
|
|
216
|
-
<li>
|
|
217
|
-
<p><strong>Okay, But… How About Even Less Typing?</strong></p>
|
|
218
|
-
|
|
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>
|
|
220
|
-
|
|
221
|
-
<p>```ruby
|
|
222
|
-
ENV[‘SHORT_PI’] # => ‘3.1415926’
|
|
223
|
-
ENV[‘BETTER_PI’] # => ‘[“flaky crust”, “strawberry filling”]’</p>
|
|
224
|
-
|
|
225
|
-
<h1 id="bind-the-proxy-methods">Bind the proxy methods.</h1>
|
|
226
|
-
<p>#
|
|
227
|
-
EnvParser.add_env_bindings</p>
|
|
228
|
-
|
|
229
|
-
<p>ENV.parse :SHORT_PI, as: :float # => 3.1415926
|
|
230
|
-
ENV.register :BETTER_PI, as: :array # Your constant is set!
|
|
231
|
-
```</p>
|
|
232
|
-
|
|
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>
|
|
234
|
-
|
|
235
|
-
<p>```ruby
|
|
236
|
-
ENV[‘SHORT_PI’] # => ‘3.1415926’</p>
|
|
237
|
-
|
|
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>
|
|
240
|
-
|
|
241
|
-
<h1 id="bind-the-proxy-methods-1">Bind the proxy methods.</h1>
|
|
242
|
-
<p>#
|
|
243
|
-
EnvParser.add_env_bindings</p>
|
|
244
|
-
|
|
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
|
|
247
|
-
```</p>
|
|
248
|
-
|
|
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>
|
|
250
|
-
|
|
251
|
-
<p><strong>All additional examples below will assume that <code>ENV</code> bindings are already in place, for brevity’s sake.</strong></p>
|
|
252
|
-
</li>
|
|
253
|
-
</ul>
|
|
254
|
-
|
|
255
|
-
<h4 id="ensuring-usable-values">Ensuring Usable Values</h4>
|
|
256
|
-
|
|
257
|
-
<ul>
|
|
258
|
-
<li>
|
|
259
|
-
<p><strong>Sensible Defaults</strong></p>
|
|
260
|
-
|
|
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>
|
|
262
|
-
|
|
263
|
-
<p><code>ruby
|
|
264
|
-
ENV.parse :MISSING_VAR, as: :integer # => 0
|
|
265
|
-
ENV.parse :MISSING_VAR, as: :integer, if_unset: 250 # => 250
|
|
266
|
-
</code></p>
|
|
267
|
-
|
|
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>
|
|
269
|
-
|
|
270
|
-
<p><code>ruby
|
|
271
|
-
ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!' # => 'Careful!' (NOT AN INTEGER)
|
|
272
|
-
</code></p>
|
|
273
|
-
</li>
|
|
274
|
-
<li>
|
|
275
|
-
<p><strong>Selecting From A Set</strong></p>
|
|
276
|
-
|
|
277
|
-
<p>Sometimes setting the <strong><em>as</em></strong> type is a bit too open-ended. The <strong><em>from_set</em></strong> option lets you restrict the domain of allowed values.</p>
|
|
278
|
-
|
|
279
|
-
<p>```ruby
|
|
280
|
-
ENV.parse :API_TO_USE, as: :symbol, from_set: %i[internal external]
|
|
281
|
-
ENV.parse :NETWORK_PORT, as: :integer, from_set: (1..65535), if_unset: 80</p>
|
|
282
|
-
|
|
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
|
|
286
|
-
```</p>
|
|
287
|
-
</li>
|
|
288
|
-
<li>
|
|
289
|
-
<p><strong>Custom Validation Of Parsed Values</strong></p>
|
|
290
|
-
|
|
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>
|
|
292
|
-
|
|
293
|
-
<p>```ruby
|
|
294
|
-
# Via a “validated_by” lambda …
|
|
295
|
-
#
|
|
296
|
-
ENV.parse :MUST_BE_LOWERCASE, as: :string, validated_by: ->(value) { value == value.downcase }</p>
|
|
297
|
-
|
|
298
|
-
<h1 id="or-with-a-block">… or with a block!</h1>
|
|
299
|
-
<p>#
|
|
300
|
-
ENV.parse(:MUST_BE_LOWERCASE, as: :string) { |value| value == value.downcase }
|
|
301
|
-
ENV.parse(:CONNECTION_RETRIES, as: :integer, &:positive?)
|
|
302
|
-
```</p>
|
|
303
|
-
</li>
|
|
304
|
-
<li>
|
|
305
|
-
<p><strong>Defining Your Own EnvParser “<em>as</em>” Types</strong></p>
|
|
306
|
-
|
|
307
|
-
<p>If you use a particular validation many times or are often manipulating values in the same way after EnvParser has done its thing, you may want to register a new type altogether. Defining a new type makes your code both more maintainable (all the logic for your special type is only defined once) and more readable (your <code>parse</code> calls aren’t littered with type-checking cruft).</p>
|
|
308
|
-
|
|
309
|
-
<p>Something as repetitive as:</p>
|
|
310
|
-
|
|
311
|
-
<p>```ruby
|
|
312
|
-
a = ENV.parse :A, as: :int, if_unset: 6
|
|
313
|
-
raise unless passes_all_my_checks?(a)</p>
|
|
314
|
-
|
|
315
|
-
<p>b = ENV.parse :B, as: :int, if_unset: 6
|
|
316
|
-
raise unless passes_all_my_checks?(b)
|
|
317
|
-
```</p>
|
|
318
|
-
|
|
319
|
-
<p>… is perhaps best handled by defining a new type:</p>
|
|
320
|
-
|
|
321
|
-
<p>```ruby
|
|
322
|
-
EnvParser.define_type(:my_special_type_of_number, if_unset: 6) do |value|
|
|
323
|
-
value = value.to_i
|
|
324
|
-
unless passes_all_my_checks?(value)
|
|
325
|
-
raise(EnvParser::ValueNotConvertibleError, ‘cannot parse as a “special type number”’)
|
|
326
|
-
end</p>
|
|
327
|
-
|
|
328
|
-
<p>value
|
|
329
|
-
end</p>
|
|
330
|
-
|
|
331
|
-
<p>a = ENV.parse :A, as: :my_special_type_of_number
|
|
332
|
-
b = ENV.parse :B, as: :my_special_type_of_number
|
|
333
|
-
```</p>
|
|
334
|
-
</li>
|
|
335
|
-
</ul>
|
|
336
|
-
|
|
337
|
-
<h4 id="auto-registering-constants">Auto-Registering Constants</h4>
|
|
338
|
-
|
|
339
|
-
<ul>
|
|
340
|
-
<li>
|
|
341
|
-
<p><strong>The <code>autoregister</code> Call</strong></p>
|
|
342
|
-
|
|
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>
|
|
344
|
-
|
|
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>
|
|
346
|
-
|
|
347
|
-
<p><code>ruby
|
|
348
|
-
gem 'env_parser', require: 'env_parser/autoregister'
|
|
349
|
-
</code></p>
|
|
350
|
-
|
|
351
|
-
<p>… will automatically make the autoregistration call for you as soon as the gem is loaded (which should be early enough for most uses). If this is <em>still</em> not early enough for your needs, you can always <code>require 'env_parser/autoregister'</code> yourself even before <code>bundler/setup</code> is invoked.</p>
|
|
352
|
-
</li>
|
|
353
|
-
<li>
|
|
354
|
-
<p><strong>The “.env_parser.yml” File</strong></p>
|
|
355
|
-
|
|
356
|
-
<p>If you recall, multiple constants can be registered via a single <code>EnvParser.register</code> call:</p>
|
|
357
|
-
|
|
358
|
-
<p>```ruby
|
|
359
|
-
EnvParser.register :USERNAME, as: :string
|
|
360
|
-
EnvParser.register :PASSWORD, as: :string
|
|
361
|
-
EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
|
362
|
-
|
|
363
|
-
<h1 id="is-equivalent-to--1">… is equivalent to …</h1>
|
|
364
|
-
|
|
365
|
-
<p>EnvParser.register USERNAME: { as: :string },
|
|
366
|
-
PASSWORD: { as: :string },
|
|
367
|
-
MOCK_API: { as: :boolean, within: MyClassOrModule }
|
|
368
|
-
```</p>
|
|
369
|
-
|
|
370
|
-
<p>The autoregistraton file is intended to read as a YAML version of what you’d pass to the single-call version of <code>EnvParser.register</code>: a single hash with keys for each of the constants you’d like to register, with each value being the set of options to parse that constant.</p>
|
|
371
|
-
|
|
372
|
-
<p>The equivalent autoregistration file for the above would be:</p>
|
|
373
|
-
|
|
374
|
-
<p>```yaml
|
|
375
|
-
USERNAME:
|
|
376
|
-
as: :string</p>
|
|
377
|
-
|
|
378
|
-
<p>PASSWORD:
|
|
379
|
-
as: :string</p>
|
|
380
|
-
|
|
381
|
-
<p>MOCK_API:
|
|
382
|
-
as: :boolean
|
|
383
|
-
within: MyClassOrModule
|
|
384
|
-
```</p>
|
|
385
|
-
|
|
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>
|
|
387
|
-
</li>
|
|
388
|
-
</ul>
|
|
389
|
-
|
|
390
|
-
<h2 id="feature-roadmap--future-development">Feature Roadmap / Future Development</h2>
|
|
391
|
-
|
|
392
|
-
<p>Additional features coming in the future:</p>
|
|
393
|
-
|
|
394
|
-
<ul>
|
|
395
|
-
<li>Continue to round out the <strong><em>as</em></strong> type selection as ideas come to mind, suggestions are made, and pull requests are submitted.</li>
|
|
396
|
-
</ul>
|
|
397
|
-
|
|
398
|
-
<h2 id="contribution--development">Contribution / Development</h2>
|
|
399
|
-
|
|
400
|
-
<p>Bug reports and pull requests are welcome at: <a href="https://github.com/nestor-custodio/env_parser">https://github.com/nestor-custodio/env_parser</a></p>
|
|
401
|
-
|
|
402
|
-
<p>After checking out the repo, run <code>bin/setup</code> to install dependencies. Then, run <code>bundle exec rspec</code> to run the tests. You can also run <code>bin/console</code> for an interactive prompt that will allow you to experiment.</p>
|
|
403
|
-
|
|
404
|
-
<p>Linting is courtesy of <a href="https://docs.rubocop.org/">Rubocop</a> (<code>bundle exec rubocop</code>) and documentation is built using <a href="https://yardoc.org/">Yard</a> (<code>bundle exec yard</code>). Please ensure you have a clean bill of health from Rubocop and that any new features and/or changes to behaviour are reflected in the documentation before submitting a pull request.</p>
|
|
405
|
-
|
|
406
|
-
<h2 id="license">License</h2>
|
|
407
|
-
|
|
408
|
-
<p>EnvParser is available as open source under the terms of the <a href="https://tldrlegal.com/license/mit-license">MIT License</a>.</p>
|
|
409
|
-
</div></div>
|
|
410
|
-
|
|
411
|
-
<div id="footer">
|
|
412
|
-
Generated on Mon Jun 9 14:00:25 2025 by
|
|
413
|
-
<a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
|
414
|
-
0.9.37 (ruby-3.4.2).
|
|
415
|
-
</div>
|
|
416
|
-
|
|
417
|
-
</div>
|
|
418
|
-
</body>
|
|
419
|
-
</html>
|
data/docs/file_list.html
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html >
|
|
3
|
-
<head>
|
|
4
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
5
|
-
<meta charset="utf-8" />
|
|
6
|
-
|
|
7
|
-
<link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen" />
|
|
8
|
-
|
|
9
|
-
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" />
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
|
14
|
-
|
|
15
|
-
<script type="text/javascript" charset="utf-8" src="js/full_list.js"></script>
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
<title>File List</title>
|
|
19
|
-
<base id="base_target" target="_parent" />
|
|
20
|
-
</head>
|
|
21
|
-
<body>
|
|
22
|
-
<div id="content">
|
|
23
|
-
<div class="fixed_header">
|
|
24
|
-
<h1 id="full_list_header">File List</h1>
|
|
25
|
-
<div id="full_list_nav">
|
|
26
|
-
|
|
27
|
-
<span><a target="_self" href="class_list.html">
|
|
28
|
-
Classes
|
|
29
|
-
</a></span>
|
|
30
|
-
|
|
31
|
-
<span><a target="_self" href="method_list.html">
|
|
32
|
-
Methods
|
|
33
|
-
</a></span>
|
|
34
|
-
|
|
35
|
-
<span><a target="_self" href="file_list.html">
|
|
36
|
-
Files
|
|
37
|
-
</a></span>
|
|
38
|
-
|
|
39
|
-
</div>
|
|
40
|
-
|
|
41
|
-
<div id="search">
|
|
42
|
-
<label for="search-class">Search:</label>
|
|
43
|
-
<input id="search-class" type="text" />
|
|
44
|
-
</div>
|
|
45
|
-
</div>
|
|
46
|
-
|
|
47
|
-
<ul id="full_list" class="file">
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
<li id="object_README" class="odd">
|
|
51
|
-
<div class="item"><span class="object_link"><a href="index.html" title="README">README</a></span></div>
|
|
52
|
-
</li>
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
</ul>
|
|
57
|
-
</div>
|
|
58
|
-
</body>
|
|
59
|
-
</html>
|
data/docs/frames.html
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html>
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="utf-8">
|
|
5
|
-
<title>Documentation by YARD 0.9.37</title>
|
|
6
|
-
</head>
|
|
7
|
-
<script type="text/javascript">
|
|
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
|
-
}
|
|
17
|
-
</script>
|
|
18
|
-
<noscript>
|
|
19
|
-
<h1>Oops!</h1>
|
|
20
|
-
<h2>YARD requires JavaScript!</h2>
|
|
21
|
-
</noscript>
|
|
22
|
-
</html>
|