env_parser 0.8.0 → 1.3.1
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 +5 -5
- data/.circleci/config.yml +7 -6
- data/.rubocop.yml +56 -57
- data/.ruby-version +1 -1
- data/Gemfile.lock +58 -28
- data/README.md +252 -176
- data/docs/EnvParser.html +305 -169
- data/docs/EnvParser/AutoregisterFileNotFound.html +143 -0
- data/docs/EnvParser/Error.html +9 -9
- data/docs/EnvParser/TypeAlreadyDefinedError.html +143 -0
- data/docs/EnvParser/Types.html +128 -0
- data/docs/EnvParser/Types/BaseTypes.html +177 -0
- data/docs/EnvParser/Types/ChronologyTypes.html +159 -0
- data/docs/EnvParser/Types/InternetTypes.html +159 -0
- data/docs/EnvParser/UnknownTypeError.html +143 -0
- data/docs/EnvParser/UnparseableAutoregisterSpec.html +143 -0
- data/docs/EnvParser/ValueNotAllowedError.html +143 -0
- data/docs/EnvParser/ValueNotConvertibleError.html +143 -0
- data/docs/EnvParserTypes.html +129 -0
- data/docs/EnvParserTypes/BaseTypes.html +178 -0
- data/docs/EnvParserTypes/ChronologyTypes.html +159 -0
- data/docs/EnvParserTypes/InternetTypes.html +159 -0
- data/docs/EnvParserTypes/TimeTypes.html +158 -0
- data/docs/_index.html +108 -9
- data/docs/class_list.html +3 -3
- data/docs/css/style.css +7 -9
- data/docs/file.README.html +215 -226
- data/docs/file_list.html +2 -2
- data/docs/frames.html +2 -2
- data/docs/index.html +215 -226
- data/docs/js/app.js +69 -3
- data/docs/method_list.html +13 -5
- data/docs/top-level-namespace.html +9 -9
- data/env_parser.gemspec +6 -2
- data/lib/env_parser.rb +111 -83
- data/lib/env_parser/autoregister.rb +3 -0
- data/lib/env_parser/errors.rb +40 -0
- data/lib/env_parser/types.rb +3 -0
- data/lib/env_parser/types/base_types.rb +99 -26
- data/lib/env_parser/types/chronology_types.rb +104 -0
- data/lib/env_parser/types/internet_types.rb +99 -0
- data/lib/env_parser/version.rb +1 -1
- metadata +85 -10
data/docs/file_list.html
CHANGED
@@ -4,9 +4,9 @@
|
|
4
4
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
5
5
|
<meta charset="utf-8" />
|
6
6
|
|
7
|
-
<link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen"
|
7
|
+
<link rel="stylesheet" href="css/full_list.css" type="text/css" media="screen" />
|
8
8
|
|
9
|
-
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen"
|
9
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" />
|
10
10
|
|
11
11
|
|
12
12
|
|
data/docs/frames.html
CHANGED
@@ -2,9 +2,9 @@
|
|
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.26</title>
|
6
6
|
</head>
|
7
|
-
<script type="text/javascript"
|
7
|
+
<script type="text/javascript">
|
8
8
|
var match = unescape(window.location.hash).match(/^#!(.+)/);
|
9
9
|
var name = match ? match[1] : 'index.html';
|
10
10
|
name = name.replace(/^(\w+):\/\//, '').replace(/^\/\//, '');
|
data/docs/index.html
CHANGED
@@ -6,15 +6,15 @@
|
|
6
6
|
<title>
|
7
7
|
File: README
|
8
8
|
|
9
|
-
— Documentation by YARD 0.9.
|
9
|
+
— Documentation by YARD 0.9.26
|
10
10
|
|
11
11
|
</title>
|
12
12
|
|
13
|
-
<link rel="stylesheet" href="css/style.css" type="text/css"
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" />
|
14
14
|
|
15
|
-
<link rel="stylesheet" href="css/common.css" type="text/css"
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" />
|
16
16
|
|
17
|
-
<script type="text/javascript"
|
17
|
+
<script type="text/javascript">
|
18
18
|
pathId = "README";
|
19
19
|
relpath = '';
|
20
20
|
</script>
|
@@ -58,260 +58,249 @@
|
|
58
58
|
</div>
|
59
59
|
|
60
60
|
<div id="content"><div id='filecontents'>
|
61
|
-
<
|
62
|
-
|
63
|
-
<
|
64
|
-
|
65
|
-
value you <em>actually</em> want to see/use takes some work, however: for
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
only want to allow values from a limited set? …</p>
|
71
|
-
|
72
|
-
<p>Things can get out of control pretty fast, especially as the number of
|
73
|
-
environment variables in play grows. Tools like <a
|
74
|
-
href="https://github.com/bkeepers/dotenv">dotenv</a> help to make sure
|
75
|
-
you're loading the correct set of <em>variables</em>, but EnvParser
|
76
|
-
makes <em>the values themselves</em> usable with a minimum of effort.</p>
|
61
|
+
<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"></a> <a href="https://github.com/nestor-custodio/env_parser/blob/master/LICENSE.txt"><img src="https://img.shields.io/github/license/nestor-custodio/env_parser"></a></p>
|
62
|
+
|
63
|
+
<h1 id="label-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>
|
77
70
|
|
78
71
|
<h2 id="label-Installation">Installation</h2>
|
72
|
+
<ul><li>
|
73
|
+
<p>If your project uses <a href="https://github.com/bundler/bundler">Bundler</a>:</p>
|
74
|
+
</li><li>
|
75
|
+
<p>Add one of the following to your application's Gemfile: "`ruby</p>
|
79
76
|
|
80
|
-
<
|
77
|
+
<h2 id="label-For+on-demand+usage+...">For on-demand usage ...</h2>
|
81
78
|
|
82
|
-
<
|
83
|
-
</code></pre>
|
79
|
+
<p>## gem 'env_parser'</p>
|
84
80
|
|
85
|
-
<
|
81
|
+
<h2 id="label-To+automatically+register+ENV">To automatically register ENV</h2>
|
86
82
|
|
87
|
-
<
|
83
|
+
<h2 id="label-constants+per+-22.env_parser.yml-22+...">constants per ".env_parser.yml" ...</h2>
|
88
84
|
|
89
|
-
<p
|
85
|
+
<p>## gem 'env_parser', require: 'env_parser/autoregister' "`</p>
|
86
|
+
</li><li>
|
87
|
+
<p>And then run a: <code>shell $ bundle install </code></p>
|
88
|
+
</li><li>
|
89
|
+
<p>Or, you can keep things simple with a manual install: <code>shell $ gem install env_parser </code></p>
|
90
|
+
</li></ul>
|
90
91
|
|
91
|
-
<
|
92
|
+
<h2 id="label-Syntax+Cheat+Sheet">Syntax Cheat Sheet</h2>
|
93
|
+
|
94
|
+
<pre class="code ruby"><code class="ruby">## Returns an ENV value parsed "as" a specific type:
|
95
|
+
##
|
96
|
+
EnvParser.parse env_key_as_a_symbol
|
97
|
+
as: … ## ➜ required
|
98
|
+
if_unset: … ## ➜ optional; default value
|
99
|
+
from_set: … ## ➜ optional; an Array or Range
|
100
|
+
validated_by: ->(value) { … } ## ➜ optional; may also be given as a block
|
101
|
+
|
102
|
+
## Parse an ENV value and register it as a constant:
|
103
|
+
##
|
104
|
+
EnvParser.register env_key_as_a_symbol
|
105
|
+
as: … ## ➜ required
|
106
|
+
within: … ## ➜ optional; Class or Module
|
107
|
+
if_unset: … ## ➜ optional; default value
|
108
|
+
from_set: … ## ➜ optional; an Array or Range
|
109
|
+
validated_by: ->(value) { … } ## ➜ optional; may also be given as a block
|
110
|
+
|
111
|
+
## Registers all ENV variables as spec'ed in ".env_parser.yml":
|
112
|
+
##
|
113
|
+
EnvParser.autoregister ## Note this is automatically called if your
|
114
|
+
## Gemfile included the "env_parser" gem with
|
115
|
+
## the "require: 'env_parser/autoregister'" option.
|
116
|
+
|
117
|
+
## Lets you call "parse" and "register" on ENV itself:
|
118
|
+
##
|
119
|
+
EnvParser.add_env_bindings ## ENV.parse will now be a proxy for EnvParser.parse
|
120
|
+
## and ENV.register will now be a proxy for EnvParser.register
|
121
|
+
</code></pre>
|
92
122
|
|
93
|
-
<h2 id="label-
|
123
|
+
<h2 id="label-Extended+How-To-Use">Extended How-To-Use</h2>
|
94
124
|
|
95
125
|
<h4 id="label-Basic+Usage">Basic Usage</h4>
|
126
|
+
<ul><li>
|
127
|
+
<p><strong>Parsing <code>ENV</code> Values</strong></p>
|
128
|
+
</li></ul>
|
96
129
|
|
97
|
-
<
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
<
|
103
|
-
|
104
|
-
</
|
105
|
-
|
106
|
-
</
|
107
|
-
|
108
|
-
</code></
|
109
|
-
<
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
<
|
114
|
-
<tbody>
|
115
|
-
<tr>
|
116
|
-
<th><code>:as</code> value</th>
|
117
|
-
<th>type returned</th>
|
118
|
-
</tr>
|
119
|
-
</tbody>
|
120
|
-
<tbody>
|
121
|
-
<tr>
|
122
|
-
<td>:string</td>
|
123
|
-
<td>String</td>
|
124
|
-
</tr>
|
125
|
-
<tr>
|
126
|
-
<td>:symbol</td>
|
127
|
-
<td>Symbol</td>
|
128
|
-
</tr>
|
129
|
-
<tr>
|
130
|
-
<td>:boolean</td>
|
131
|
-
<td>TrueValue / FalseValue</td>
|
132
|
-
</tr>
|
133
|
-
<tr>
|
134
|
-
<td>:int / :integer</td>
|
135
|
-
<td>Integer</td>
|
136
|
-
</tr>
|
137
|
-
<tr>
|
138
|
-
<td>:float / :decimal / :number</td>
|
139
|
-
<td>Float</td>
|
140
|
-
</tr>
|
141
|
-
<tr>
|
142
|
-
<td>:json</td>
|
143
|
-
<td>< depends on JSON given ></td>
|
144
|
-
</tr>
|
145
|
-
<tr>
|
146
|
-
<td>:array</td>
|
147
|
-
<td>Array</td>
|
148
|
-
</tr>
|
149
|
-
<tr>
|
150
|
-
<td>:hash</td>
|
151
|
-
<td>Hash</td>
|
152
|
-
</tr>
|
153
|
-
</tbody>
|
154
|
-
</table>
|
155
|
-
<p>Note JSON is parsed using <em>quirks-mode</em> (meaning 'true',
|
156
|
-
'25', and 'null' are all considered valid, parseable JSON).</p>
|
157
|
-
|
158
|
-
<h4 id="label-Setting+Non-Trivial+Defaults">Setting Non-Trivial Defaults</h4>
|
159
|
-
|
160
|
-
<pre class="code ruby"><code class="ruby"><span class='comment'>## If the ENV variable you want is unset (nil) or blank (''),
|
161
|
-
</span><span class='comment'>## the return value is a sensible default for the given "as" type
|
162
|
-
</span><span class='comment'>## (0 or 0.0 for numbers, an empty tring, an empty Array or Hash, etc).
|
163
|
-
</span><span class='comment'>## Sometimes you want a non-trivial default, however.
|
164
|
-
</span><span class='comment'>##
|
165
|
-
</span><span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span> <span class='symbol'>:MISSING_ENV_VARIABLE</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span> <span class='comment'>## => 0
|
166
|
-
</span><span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span> <span class='symbol'>:MISSING_ENV_VARIABLE</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span><span class='comma'>,</span> <span class='label'>if_unset:</span> <span class='int'>250</span> <span class='comment'>## => 250
|
167
|
-
</span>
|
168
|
-
<span class='comment'>## Note that "if_unset" values are used as-is, with no type conversion.
|
169
|
-
</span><span class='comment'>##
|
170
|
-
</span><span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span> <span class='symbol'>:MISSING_ENV_VARIABLE</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span><span class='comma'>,</span> <span class='label'>if_unset:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Whoops!</span><span class='tstring_end'>'</span></span> <span class='comment'>## => 'Whoops!'
|
171
|
-
</span></code></pre>
|
172
|
-
|
173
|
-
<h4 id="label-Validating+Parsed+ENV+Values">Validating Parsed ENV Values</h4>
|
174
|
-
|
175
|
-
<pre class="code ruby"><code class="ruby"><span class='comment'>## Sometimes setting the type alone is a bit too open-ended.
|
176
|
-
</span><span class='comment'>## The "from_set" option lets you restrict the set of allowed values.
|
177
|
-
</span><span class='comment'>##
|
178
|
-
</span><span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span> <span class='symbol'>:API_TO_USE</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:symbol</span><span class='comma'>,</span> <span class='label'>from_set:</span> <span class='qsymbols_beg'>%i[</span><span class='tstring_content'>internal</span><span class='words_sep'> </span><span class='tstring_content'>external</span><span class='words_sep'>]</span>
|
179
|
-
<span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span> <span class='symbol'>:SOME_CUSTOM_NETWORK_PORT</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span><span class='comma'>,</span> <span class='label'>from_set:</span> <span class='lparen'>(</span><span class='int'>1</span><span class='op'>..</span><span class='int'>65535</span><span class='rparen'>)</span><span class='comma'>,</span> <span class='label'>if_unset:</span> <span class='int'>80</span>
|
180
|
-
|
181
|
-
<span class='comment'>## And if the value is not allowed...
|
182
|
-
</span><span class='comment'>##
|
183
|
-
</span><span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span> <span class='symbol'>:NEGATIVE_NUMBER</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span><span class='comma'>,</span> <span class='label'>from_set:</span> <span class='lparen'>(</span><span class='int'>1</span><span class='op'>..</span><span class='int'>5</span><span class='rparen'>)</span> <span class='comment'>## => raises EnvParser::ValueNotAllowed
|
184
|
-
</span>
|
185
|
-
|
186
|
-
<span class='comment'>## The "validated_by" option allows for more complex validation.
|
187
|
-
</span><span class='comment'>##
|
188
|
-
</span><span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span> <span class='symbol'>:MUST_BE_LOWERCASE</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:string</span><span class='comma'>,</span> <span class='label'>validated_by:</span> <span class='tlambda'>-></span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span> <span class='tlambeg'>{</span> <span class='id identifier rubyid_value'>value</span> <span class='op'>==</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_downcase'>downcase</span> <span class='rbrace'>}</span>
|
189
|
-
|
190
|
-
<span class='comment'>## ... but a block will also do the trick!
|
191
|
-
</span><span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span><span class='lparen'>(</span><span class='symbol'>:MUST_BE_LOWERCASE</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:string</span><span class='rparen'>)</span> <span class='lbrace'>{</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span> <span class='id identifier rubyid_value'>value</span> <span class='op'>==</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_downcase'>downcase</span> <span class='rbrace'>}</span>
|
192
|
-
<span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span><span class='lparen'>(</span><span class='symbol'>:CONNECTION_RETRIES</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span><span class='comma'>,</span> <span class='op'>&</span><span class='symbol'>:nonzero?</span><span class='rparen'>)</span>
|
193
|
-
</code></pre>
|
130
|
+
<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>
|
131
|
+
|
132
|
+
<p><code>ruby ## Returns ENV['TIMEOUT_MS'] as an Integer, ## or a sensible default (0) if ENV['TIMEOUT_MS'] is unset. ## timeout_ms = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer </code></p>
|
133
|
+
|
134
|
+
<p>You can check the full documentation for <a href="http://nestor-custodio.github.io/env_parser/EnvParser/Types.html">a list of all as types available right out of the box</a>.</p>
|
135
|
+
<ul><li>
|
136
|
+
<p><strong>How About Less Typing?</strong></p>
|
137
|
+
</li></ul>
|
138
|
+
|
139
|
+
<p>EnvParser is all about ~simplification~ <s>less typing</s> <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>
|
140
|
+
|
141
|
+
<p><code>ruby ## YAY, LESS TYPING! 😃 ## These two are the same: ## more_typing = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer less_typing = EnvParser.parse :TIMEOUT_MS, as: :integer </code></p>
|
142
|
+
<ul><li>
|
143
|
+
<p><strong>Registering Constants From <code>ENV</code> Values</strong></p>
|
144
|
+
</li></ul>
|
145
|
+
|
146
|
+
<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>
|
194
147
|
|
195
|
-
<
|
196
|
-
|
197
|
-
<
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
<
|
202
|
-
|
203
|
-
<
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
<
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
<
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
</
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
148
|
+
<p>“`ruby <a href="'API_KEY'">ENV</a> ## => 'unbreakable p4$$w0rd'</p>
|
149
|
+
|
150
|
+
<p>EnvParser.register :API_KEY, as: :string API_KEY ## => 'unbreakable p4$$w0rd' “`</p>
|
151
|
+
|
152
|
+
<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>
|
153
|
+
|
154
|
+
<p>“`ruby <a href="'BEST_VIDEO'">ENV</a> ## => '<a href="https://youtu.be/L_jWHffIx5E">youtu.be/L_jWHffIx5E</a>'</p>
|
155
|
+
|
156
|
+
<p>EnvParser.register :BEST_VIDEO, as: :string, within: URI URI::BEST_VIDEO ## => '<a href="https://youtu.be/L_jWHffIx5E">youtu.be/L_jWHffIx5E</a>' BEST_VIDEO ## => raises NameError “`</p>
|
157
|
+
|
158
|
+
<p>You can also register multiple constants with a single call, which is a bit cleaner.</p>
|
159
|
+
|
160
|
+
<p>“`ruby EnvParser.register :USERNAME, as: :string EnvParser.register :PASSWORD, as: :string EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
161
|
+
|
162
|
+
<p>## … is equivalent to … ##</p>
|
163
|
+
|
164
|
+
<p>EnvParser.register USERNAME: { as: :string }, PASSWORD: { as: :string }, MOCK_API: { as: :boolean, within: MyClassOrModule } “`</p>
|
165
|
+
<ul><li>
|
166
|
+
<p><strong>Okay, But… How About Even Less Typing?</strong></p>
|
167
|
+
</li></ul>
|
168
|
+
|
169
|
+
<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>
|
170
|
+
|
171
|
+
<p>“`ruby <a href="'SHORT_PI'">ENV</a> ## => '3.1415926' <a href="'BETTER_PI'">ENV</a> ## => '[“flaky crust”, “strawberry filling”]'</p>
|
172
|
+
|
173
|
+
<p>## Bind the proxy methods. ## EnvParser.add_env_bindings</p>
|
174
|
+
|
175
|
+
<p>ENV.parse :SHORT_PI, as: :float ## => 3.1415926 ENV.register :BETTER_PI, as: :array ## Your constant is set! “`</p>
|
176
|
+
|
177
|
+
<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>
|
178
|
+
|
179
|
+
<p>“`ruby <a href="'SHORT_PI'">ENV</a> ## => '3.1415926'</p>
|
180
|
+
|
181
|
+
<p>EnvParser.parse 'SHORT_PI', as: :float ## => 'SHORT_PI' as a float: 0.0 EnvParser.parse :SHORT_PI , as: :float ## => <a href="'SHORT_PI'">ENV</a> as a float: 3.1415926</p>
|
182
|
+
|
183
|
+
<p>## Bind the proxy methods. ## EnvParser.add_env_bindings</p>
|
184
|
+
|
185
|
+
<p>ENV.parse 'SHORT_PI', as: :float ## => <a href="'SHORT_PI'">ENV</a> as a float: 3.1415926 ENV.parse :SHORT_PI , as: :float ## => <a href="'SHORT_PI'">ENV</a> as a float: 3.1415926 “`</p>
|
186
|
+
|
187
|
+
<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>
|
188
|
+
|
189
|
+
<p><strong>All additional examples below will assume that <code>ENV</code> bindings are already in place, for brevity's sake.</strong></p>
|
190
|
+
|
191
|
+
<h4 id="label-Ensuring+Usable+Values">Ensuring Usable Values</h4>
|
192
|
+
<ul><li>
|
193
|
+
<p><strong>Sensible Defaults</strong></p>
|
194
|
+
</li></ul>
|
195
|
+
|
196
|
+
<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>
|
197
|
+
|
198
|
+
<p><code>ruby ENV.parse :MISSING_VAR, as: :integer ## => 0 ENV.parse :MISSING_VAR, as: :integer, if_unset: 250 ## => 250 </code></p>
|
199
|
+
|
200
|
+
<p>Note these default values are used as-is with no type conversion, so exercise caution.</p>
|
201
|
+
|
202
|
+
<p><code>ruby ENV.parse :MISSING_VAR, as: :integer, if_unset: 'Careful!' ## => 'Careful!' (NOT AN INTEGER) </code></p>
|
203
|
+
<ul><li>
|
204
|
+
<p><strong>Selecting From A Set</strong></p>
|
205
|
+
</li></ul>
|
206
|
+
|
207
|
+
<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>
|
208
|
+
|
209
|
+
<p>“`ruby ENV.parse :API_TO_USE, as: :symbol, from_set: %i[internal external] ENV.parse :NETWORK_PORT, as: :integer, from_set: (1..65535), if_unset: 80</p>
|
210
|
+
|
211
|
+
<p>## And if the value is not in the allowed set … ## ENV.parse :TWELVE, as: :integer, from_set: (1..5) ## => raises EnvParser::ValueNotAllowedError “`</p>
|
212
|
+
<ul><li>
|
213
|
+
<p><strong>Custom Validation Of Parsed Values</strong></p>
|
214
|
+
</li></ul>
|
215
|
+
|
216
|
+
<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>
|
217
|
+
|
218
|
+
<p>“`ruby ## Via a “validated_by” lambda … ## ENV.parse :MUST_BE_LOWERCASE, as: :string, validated_by: ->(value) { value == value.downcase }</p>
|
219
|
+
|
220
|
+
<p>## … or with a block! ## ENV.parse(:MUST_BE_LOWERCASE, as: :string) { |value| value == value.downcase } ENV.parse(:CONNECTION_RETRIES, as: :integer, &:positive?) “`</p>
|
221
|
+
<ul><li>
|
222
|
+
<p><strong>Defining Your Own EnvParser “<em>as</em>” Types</strong></p>
|
223
|
+
</li></ul>
|
224
|
+
|
225
|
+
<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>
|
226
|
+
|
227
|
+
<p>Something as repetitive as:</p>
|
228
|
+
|
229
|
+
<p>“`ruby a = ENV.parse :A, as: :int, if_unset: 6 raise unless passes_all_my_checks?(a)</p>
|
230
|
+
|
231
|
+
<p>b = ENV.parse :B, as: :int, if_unset: 6 raise unless passes_all_my_checks?(b) “`</p>
|
232
|
+
|
233
|
+
<p>… is perhaps best handled by defining a new type:</p>
|
234
|
+
|
235
|
+
<p>“`ruby EnvParser.define_type(:my_special_type_of_number, if_unset: 6) do |value| value = value.to_i unless passes_all_my_checks?(value) raise(EnvParser::ValueNotConvertibleError, 'cannot parse as a “special type number”') end</p>
|
236
|
+
|
237
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_value'>value</span>
|
224
238
|
</code></pre>
|
225
239
|
|
226
|
-
<
|
227
|
-
|
228
|
-
<
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
</
|
233
|
-
|
234
|
-
|
235
|
-
</
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
</
|
240
|
-
|
241
|
-
<
|
242
|
-
|
243
|
-
<
|
244
|
-
</
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
<
|
251
|
-
|
252
|
-
<
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
<
|
263
|
-
|
264
|
-
<
|
265
|
-
</span><span class='comment'>## is only defined once) and more readable (your "parse" calls aren't littered with type-checking
|
266
|
-
</span><span class='comment'>## cruft).
|
267
|
-
</span></code></pre>
|
268
|
-
<hr>
|
269
|
-
|
270
|
-
<p><a href="http://nestor-custodio.github.io/env_parser">Consult the repo
|
271
|
-
docs</a> for the full EnvParser documentation.</p>
|
240
|
+
<p>end</p>
|
241
|
+
|
242
|
+
<p>a = ENV.parse :A, as: :my_special_type_of_number b = ENV.parse :B, as: :my_special_type_of_number “`</p>
|
243
|
+
|
244
|
+
<h4 id="label-Auto-Registering+Constants">Auto-Registering Constants</h4>
|
245
|
+
<ul><li>
|
246
|
+
<p><strong>The <code>autoregister</code> Call</strong></p>
|
247
|
+
</li></ul>
|
248
|
+
|
249
|
+
<p>Consolidating all of your <code>EnvParser.register</code> calls into a single place only makes sense. A single <code>EnvParser.autoregister</code> call take 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>
|
250
|
+
|
251
|
+
<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>
|
252
|
+
|
253
|
+
<p><code>ruby gem 'env_parser', require: 'env_parser/autoregister' </code></p>
|
254
|
+
|
255
|
+
<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>
|
256
|
+
<ul><li>
|
257
|
+
<p><strong>The “.env_parser.yml” File</strong></p>
|
258
|
+
</li></ul>
|
259
|
+
|
260
|
+
<p>If you recall, multiple constants can be registered via a single <code>EnvParser.register</code> call:</p>
|
261
|
+
|
262
|
+
<p>“`ruby EnvParser.register :USERNAME, as: :string EnvParser.register :PASSWORD, as: :string EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }</p>
|
263
|
+
|
264
|
+
<p>## … is equivalent to … ##</p>
|
265
|
+
|
266
|
+
<p>EnvParser.register USERNAME: { as: :string }, PASSWORD: { as: :string }, MOCK_API: { as: :boolean, within: MyClassOrModule } “`</p>
|
267
|
+
|
268
|
+
<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>
|
269
|
+
|
270
|
+
<p>The equivalent autoregistration file for the above would be:</p>
|
271
|
+
|
272
|
+
<p>“`yaml USERNAME: as: :string</p>
|
273
|
+
|
274
|
+
<p>PASSWORD: as: :string</p>
|
275
|
+
|
276
|
+
<p>MOCK_API: as: :boolean within: MyClassOrModule “`</p>
|
277
|
+
|
278
|
+
<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>
|
272
279
|
|
273
280
|
<h2 id="label-Feature+Roadmap+-2F+Future+Development">Feature Roadmap / Future Development</h2>
|
274
281
|
|
275
|
-
<p>Additional features
|
282
|
+
<p>Additional features coming in the future:</p>
|
276
283
|
<ul><li>
|
277
|
-
<p>
|
278
|
-
<code>:email</code>, etc.</p>
|
279
|
-
</li><li>
|
280
|
-
<p>… ?</p>
|
284
|
+
<p>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.</p>
|
281
285
|
</li></ul>
|
282
286
|
|
283
287
|
<h2 id="label-Contribution+-2F+Development">Contribution / Development</h2>
|
284
288
|
|
285
|
-
<p>Bug reports and pull requests are welcome
|
286
|
-
href="https://github.com/nestor-custodio/env_parser">github.com/nestor-custodio/env_parser</a>.</p>
|
287
|
-
|
288
|
-
<p>After checking out the repo, run <code>bin/setup</code> to install
|
289
|
-
dependencies. Then, run <code>rake spec</code> to run the tests. You can
|
290
|
-
also run <code>bin/console</code> for an interactive prompt that will allow
|
291
|
-
you to experiment.</p>
|
289
|
+
<p>Bug reports and pull requests are welcome at: <a href="https://github.com/nestor-custodio/env_parser">github.com/nestor-custodio/env_parser</a></p>
|
292
290
|
|
293
|
-
<p>
|
294
|
-
href="https://github.com/bbatsov/rubocop">Rubocop</a> and documentation is
|
295
|
-
built using <a href="https://yardoc.org/">Yard</a>. Neither is included in
|
296
|
-
the Gemspec; you'll need to install these locally to take advantage.</p>
|
291
|
+
<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>
|
297
292
|
|
298
|
-
<p>
|
299
|
-
install</code>. To release a new version, update the version number in
|
300
|
-
<code>version.rb</code>, and then run <code>bundle exec rake
|
301
|
-
release</code>, which will create a git tag for the version, push git
|
302
|
-
commits and tags, and push the <code>.gem</code> file to <a
|
303
|
-
href="https://rubygems.org">rubygems.org</a>.</p>
|
293
|
+
<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>
|
304
294
|
|
305
295
|
<h2 id="label-License">License</h2>
|
306
296
|
|
307
|
-
<p>
|
308
|
-
href="https://opensource.org/licenses/MIT">MIT License</a>.</p>
|
297
|
+
<p>EnvParser is available as open source under the terms of the <a href="https://tldrlegal.com/license/mit-license">MIT License</a>.</p>
|
309
298
|
</div></div>
|
310
299
|
|
311
300
|
<div id="footer">
|
312
|
-
Generated on
|
301
|
+
Generated on Sat Jan 2 17:31:16 2021 by
|
313
302
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
314
|
-
0.9.
|
303
|
+
0.9.26 (ruby-2.7.2).
|
315
304
|
</div>
|
316
305
|
|
317
306
|
</div>
|