env_parser 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +48 -57
- data/Gemfile.lock +19 -1
- data/README.md +214 -125
- data/docs/EnvParser.html +214 -32
- data/docs/EnvParser/AutoregisterFileNotFound.html +144 -0
- data/docs/EnvParser/Error.html +2 -2
- data/docs/EnvParser/TypeAlreadyDefinedError.html +1 -1
- data/docs/EnvParser/Types.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 +1 -1
- data/docs/EnvParser/UnknownTypeError.html +1 -1
- data/docs/EnvParser/UnparseableAutoregisterSpec.html +144 -0
- data/docs/EnvParser/ValueNotAllowedError.html +1 -1
- data/docs/EnvParser/ValueNotConvertibleError.html +1 -1
- data/docs/_index.html +26 -1
- data/docs/class_list.html +1 -1
- data/docs/file.README.html +296 -169
- data/docs/index.html +296 -169
- data/docs/method_list.html +11 -3
- data/docs/top-level-namespace.html +1 -1
- data/env_parser.gemspec +2 -0
- data/lib/env_parser.rb +47 -3
- data/lib/env_parser/autoregister.rb +3 -0
- data/lib/env_parser/errors.rb +12 -0
- data/lib/env_parser/types/chronology_types.rb +1 -1
- data/lib/env_parser/types/internet_types.rb +2 -2
- data/lib/env_parser/version.rb +1 -1
- metadata +33 -2
data/docs/EnvParser/Error.html
CHANGED
@@ -114,7 +114,7 @@
|
|
114
114
|
|
115
115
|
</div><div id="subclasses">
|
116
116
|
<h2>Direct Known Subclasses</h2>
|
117
|
-
<p class="children"><span class='object_link'><a href="TypeAlreadyDefinedError.html" title="EnvParser::TypeAlreadyDefinedError (class)">TypeAlreadyDefinedError</a></span>, <span class='object_link'><a href="UnknownTypeError.html" title="EnvParser::UnknownTypeError (class)">UnknownTypeError</a></span>, <span class='object_link'><a href="ValueNotAllowedError.html" title="EnvParser::ValueNotAllowedError (class)">ValueNotAllowedError</a></span>, <span class='object_link'><a href="ValueNotConvertibleError.html" title="EnvParser::ValueNotConvertibleError (class)">ValueNotConvertibleError</a></span></p>
|
117
|
+
<p class="children"><span class='object_link'><a href="AutoregisterFileNotFound.html" title="EnvParser::AutoregisterFileNotFound (class)">AutoregisterFileNotFound</a></span>, <span class='object_link'><a href="TypeAlreadyDefinedError.html" title="EnvParser::TypeAlreadyDefinedError (class)">TypeAlreadyDefinedError</a></span>, <span class='object_link'><a href="UnknownTypeError.html" title="EnvParser::UnknownTypeError (class)">UnknownTypeError</a></span>, <span class='object_link'><a href="UnparseableAutoregisterSpec.html" title="EnvParser::UnparseableAutoregisterSpec (class)">UnparseableAutoregisterSpec</a></span>, <span class='object_link'><a href="ValueNotAllowedError.html" title="EnvParser::ValueNotAllowedError (class)">ValueNotAllowedError</a></span>, <span class='object_link'><a href="ValueNotConvertibleError.html" title="EnvParser::ValueNotConvertibleError (class)">ValueNotConvertibleError</a></span></p>
|
118
118
|
</div>
|
119
119
|
|
120
120
|
|
@@ -129,7 +129,7 @@
|
|
129
129
|
</div>
|
130
130
|
|
131
131
|
<div id="footer">
|
132
|
-
Generated on
|
132
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
133
133
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
134
134
|
0.9.20 (ruby-2.4.2).
|
135
135
|
</div>
|
@@ -133,7 +133,7 @@
|
|
133
133
|
</div>
|
134
134
|
|
135
135
|
<div id="footer">
|
136
|
-
Generated on
|
136
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
137
137
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
138
138
|
0.9.20 (ruby-2.4.2).
|
139
139
|
</div>
|
data/docs/EnvParser/Types.html
CHANGED
@@ -119,7 +119,7 @@ for documentation's sake.</p>
|
|
119
119
|
</div>
|
120
120
|
|
121
121
|
<div id="footer">
|
122
|
-
Generated on
|
122
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
123
123
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
124
124
|
0.9.20 (ruby-2.4.2).
|
125
125
|
</div>
|
@@ -168,7 +168,7 @@
|
|
168
168
|
</div>
|
169
169
|
|
170
170
|
<div id="footer">
|
171
|
-
Generated on
|
171
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
172
172
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
173
173
|
0.9.20 (ruby-2.4.2).
|
174
174
|
</div>
|
@@ -149,7 +149,7 @@
|
|
149
149
|
</div>
|
150
150
|
|
151
151
|
<div id="footer">
|
152
|
-
Generated on
|
152
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
153
153
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
154
154
|
0.9.20 (ruby-2.4.2).
|
155
155
|
</div>
|
@@ -149,7 +149,7 @@
|
|
149
149
|
</div>
|
150
150
|
|
151
151
|
<div id="footer">
|
152
|
-
Generated on
|
152
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
153
153
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
154
154
|
0.9.20 (ruby-2.4.2).
|
155
155
|
</div>
|
@@ -133,7 +133,7 @@
|
|
133
133
|
</div>
|
134
134
|
|
135
135
|
<div id="footer">
|
136
|
-
Generated on
|
136
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
137
137
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
138
138
|
0.9.20 (ruby-2.4.2).
|
139
139
|
</div>
|
@@ -0,0 +1,144 @@
|
|
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
|
+
Exception: EnvParser::UnparseableAutoregisterSpec
|
8
|
+
|
9
|
+
— Documentation by YARD 0.9.20
|
10
|
+
|
11
|
+
</title>
|
12
|
+
|
13
|
+
<link rel="stylesheet" href="../css/style.css" type="text/css" charset="utf-8" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8" />
|
16
|
+
|
17
|
+
<script type="text/javascript" charset="utf-8">
|
18
|
+
pathId = "EnvParser::UnparseableAutoregisterSpec";
|
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="../class_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 (U)</a> »
|
40
|
+
<span class='title'><span class='object_link'><a href="../EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span>
|
41
|
+
»
|
42
|
+
<span class="title">UnparseableAutoregisterSpec</span>
|
43
|
+
|
44
|
+
</div>
|
45
|
+
|
46
|
+
<div id="search">
|
47
|
+
|
48
|
+
<a class="full_list_link" id="class_list_link"
|
49
|
+
href="../class_list.html">
|
50
|
+
|
51
|
+
<svg width="24" height="24">
|
52
|
+
<rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
|
53
|
+
<rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
|
54
|
+
<rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
|
55
|
+
</svg>
|
56
|
+
</a>
|
57
|
+
|
58
|
+
</div>
|
59
|
+
<div class="clear"></div>
|
60
|
+
</div>
|
61
|
+
|
62
|
+
<div id="content"><h1>Exception: EnvParser::UnparseableAutoregisterSpec
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
</h1>
|
67
|
+
<div class="box_info">
|
68
|
+
|
69
|
+
<dl>
|
70
|
+
<dt>Inherits:</dt>
|
71
|
+
<dd>
|
72
|
+
<span class="inheritName"><span class='object_link'><a href="Error.html" title="EnvParser::Error (class)">Error</a></span></span>
|
73
|
+
|
74
|
+
<ul class="fullTree">
|
75
|
+
<li>Object</li>
|
76
|
+
|
77
|
+
<li class="next">StandardError</li>
|
78
|
+
|
79
|
+
<li class="next"><span class='object_link'><a href="Error.html" title="EnvParser::Error (class)">Error</a></span></li>
|
80
|
+
|
81
|
+
<li class="next">EnvParser::UnparseableAutoregisterSpec</li>
|
82
|
+
|
83
|
+
</ul>
|
84
|
+
<a href="#" class="inheritanceTree">show all</a>
|
85
|
+
|
86
|
+
</dd>
|
87
|
+
</dl>
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
<dl>
|
100
|
+
<dt>Defined in:</dt>
|
101
|
+
<dd>lib/env_parser/errors.rb</dd>
|
102
|
+
</dl>
|
103
|
+
|
104
|
+
</div>
|
105
|
+
|
106
|
+
<h2>Overview</h2><div class="docstring">
|
107
|
+
<div class="discussion">
|
108
|
+
|
109
|
+
<p>Error class used to indicate an unparseable auto-registration spec (used by
|
110
|
+
the “autoregister” feature).</p>
|
111
|
+
|
112
|
+
|
113
|
+
</div>
|
114
|
+
</div>
|
115
|
+
<div class="tags">
|
116
|
+
|
117
|
+
|
118
|
+
</div>
|
119
|
+
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
</div>
|
135
|
+
|
136
|
+
<div id="footer">
|
137
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
138
|
+
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
139
|
+
0.9.20 (ruby-2.4.2).
|
140
|
+
</div>
|
141
|
+
|
142
|
+
</div>
|
143
|
+
</body>
|
144
|
+
</html>
|
@@ -135,7 +135,7 @@ by failing the “validated_by” Proc or yield-block check.</p>
|
|
135
135
|
</div>
|
136
136
|
|
137
137
|
<div id="footer">
|
138
|
-
Generated on
|
138
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
139
139
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
140
140
|
0.9.20 (ruby-2.4.2).
|
141
141
|
</div>
|
@@ -134,7 +134,7 @@ requested type.</p>
|
|
134
134
|
</div>
|
135
135
|
|
136
136
|
<div id="footer">
|
137
|
-
Generated on
|
137
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
138
138
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
139
139
|
0.9.20 (ruby-2.4.2).
|
140
140
|
</div>
|
data/docs/_index.html
CHANGED
@@ -78,6 +78,21 @@
|
|
78
78
|
<td valign='top' width="33%">
|
79
79
|
|
80
80
|
|
81
|
+
<ul id="alpha_A" class="alpha">
|
82
|
+
<li class="letter">A</li>
|
83
|
+
<ul>
|
84
|
+
|
85
|
+
<li>
|
86
|
+
<span class='object_link'><a href="EnvParser/AutoregisterFileNotFound.html" title="EnvParser::AutoregisterFileNotFound (class)">AutoregisterFileNotFound</a></span>
|
87
|
+
|
88
|
+
<small>(EnvParser)</small>
|
89
|
+
|
90
|
+
</li>
|
91
|
+
|
92
|
+
</ul>
|
93
|
+
</ul>
|
94
|
+
|
95
|
+
|
81
96
|
<ul id="alpha_B" class="alpha">
|
82
97
|
<li class="letter">B</li>
|
83
98
|
<ul>
|
@@ -176,10 +191,20 @@
|
|
176
191
|
|
177
192
|
</li>
|
178
193
|
|
194
|
+
<li>
|
195
|
+
<span class='object_link'><a href="EnvParser/UnparseableAutoregisterSpec.html" title="EnvParser::UnparseableAutoregisterSpec (class)">UnparseableAutoregisterSpec</a></span>
|
196
|
+
|
197
|
+
<small>(EnvParser)</small>
|
198
|
+
|
199
|
+
</li>
|
200
|
+
|
179
201
|
</ul>
|
180
202
|
</ul>
|
181
203
|
|
182
204
|
|
205
|
+
</td><td valign='top' width="33%">
|
206
|
+
|
207
|
+
|
183
208
|
<ul id="alpha_V" class="alpha">
|
184
209
|
<li class="letter">V</li>
|
185
210
|
<ul>
|
@@ -210,7 +235,7 @@
|
|
210
235
|
</div>
|
211
236
|
|
212
237
|
<div id="footer">
|
213
|
-
Generated on
|
238
|
+
Generated on Sun Nov 3 21:30:34 2019 by
|
214
239
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
215
240
|
0.9.20 (ruby-2.4.2).
|
216
241
|
</div>
|
data/docs/class_list.html
CHANGED
@@ -43,7 +43,7 @@
|
|
43
43
|
|
44
44
|
<ul id="full_list" class="class">
|
45
45
|
<li id="object_" class="odd"><div class="item" style="padding-left:30px"><span class='object_link'><a href="top-level-namespace.html" title="Top Level Namespace (root)">Top Level Namespace</a></span></div></li>
|
46
|
-
<li id='object_EnvParser' class='even'><div class='item' style='padding-left:30px'><a class='toggle'></a> <span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span> < Object<small class='search_info'>Top Level Namespace</small></div><ul><li id='object_EnvParser::
|
46
|
+
<li id='object_EnvParser' class='even'><div class='item' style='padding-left:30px'><a class='toggle'></a> <span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span> < Object<small class='search_info'>Top Level Namespace</small></div><ul><li id='object_EnvParser::AutoregisterFileNotFound' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="EnvParser/AutoregisterFileNotFound.html" title="EnvParser::AutoregisterFileNotFound (class)">AutoregisterFileNotFound</a></span> < Error<small class='search_info'>EnvParser</small></div></li><li id='object_EnvParser::Error' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="EnvParser/Error.html" title="EnvParser::Error (class)">Error</a></span> < StandardError<small class='search_info'>EnvParser</small></div></li><li id='object_EnvParser::TypeAlreadyDefinedError' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="EnvParser/TypeAlreadyDefinedError.html" title="EnvParser::TypeAlreadyDefinedError (class)">TypeAlreadyDefinedError</a></span> < Error<small class='search_info'>EnvParser</small></div></li><li id='object_EnvParser::Types' class='collapsed even'><div class='item' style='padding-left:45px'><a class='toggle'></a> <span class='object_link'><a href="EnvParser/Types.html" title="EnvParser::Types (module)">Types</a></span><small class='search_info'>EnvParser</small></div><ul><li id='object_EnvParser::Types::BaseTypes' class='collapsed'><div class='item' style='padding-left:60px'><span class='object_link'><a href="EnvParser/Types/BaseTypes.html" title="EnvParser::Types::BaseTypes (module)">BaseTypes</a></span><small class='search_info'>EnvParser::Types</small></div></li><li id='object_EnvParser::Types::ChronologyTypes' class='collapsed'><div class='item' style='padding-left:60px'><span class='object_link'><a href="EnvParser/Types/ChronologyTypes.html" title="EnvParser::Types::ChronologyTypes (module)">ChronologyTypes</a></span><small class='search_info'>EnvParser::Types</small></div></li><li id='object_EnvParser::Types::InternetTypes' class='collapsed'><div class='item' style='padding-left:60px'><span class='object_link'><a href="EnvParser/Types/InternetTypes.html" title="EnvParser::Types::InternetTypes (module)">InternetTypes</a></span><small class='search_info'>EnvParser::Types</small></div></li></ul></li><li id='object_EnvParser::UnknownTypeError' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="EnvParser/UnknownTypeError.html" title="EnvParser::UnknownTypeError (class)">UnknownTypeError</a></span> < Error<small class='search_info'>EnvParser</small></div></li><li id='object_EnvParser::UnparseableAutoregisterSpec' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="EnvParser/UnparseableAutoregisterSpec.html" title="EnvParser::UnparseableAutoregisterSpec (class)">UnparseableAutoregisterSpec</a></span> < Error<small class='search_info'>EnvParser</small></div></li><li id='object_EnvParser::ValueNotAllowedError' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="EnvParser/ValueNotAllowedError.html" title="EnvParser::ValueNotAllowedError (class)">ValueNotAllowedError</a></span> < Error<small class='search_info'>EnvParser</small></div></li><li id='object_EnvParser::ValueNotConvertibleError' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="EnvParser/ValueNotConvertibleError.html" title="EnvParser::ValueNotConvertibleError (class)">ValueNotConvertibleError</a></span> < Error<small class='search_info'>EnvParser</small></div></li></ul></li>
|
47
47
|
|
48
48
|
</ul>
|
49
49
|
</div>
|
data/docs/file.README.html
CHANGED
@@ -58,7 +58,13 @@
|
|
58
58
|
</div>
|
59
59
|
|
60
60
|
<div id="content"><div id='filecontents'>
|
61
|
-
<
|
61
|
+
<p><a href="https://rubygems.org/gems/env_parser"><img
|
62
|
+
src="https://img.shields.io/github/v/release/nestor-custodio/env_parser?color=green&label=gem%20version"></a>
|
63
|
+
<a
|
64
|
+
href="https://github.com/nestor-custodio/env_parser/blob/master/LICENSE.txt"><img
|
65
|
+
src="https://img.shields.io/github/license/nestor-custodio/env_parser"></a></p>
|
66
|
+
|
67
|
+
<h1 id="label-EnvParser">EnvParser</h1>
|
62
68
|
|
63
69
|
<p>If your code uses environment variables, you know that <code>ENV</code>
|
64
70
|
will always surface these as strings. Interpreting these strings as the
|
@@ -74,232 +80,353 @@ environment variables in play grows. Tools like <a
|
|
74
80
|
href="https://github.com/bkeepers/dotenv">dotenv</a> help to make sure
|
75
81
|
you're loading the correct <strong>set</strong> of variables, but <a
|
76
82
|
href="https://github.com/nestor-custodio/env_parser">EnvParser</a> makes
|
77
|
-
<strong
|
78
|
-
|
83
|
+
<strong>the values themselves</strong> usable with a minimum of effort.</p>
|
84
|
+
|
85
|
+
<p><a href="http://nestor-custodio.github.io/env_parser/EnvParser.html">Full
|
86
|
+
documentation is available here</a>, but do read below for a crash course
|
87
|
+
on availble featues!</p>
|
79
88
|
|
80
89
|
<h2 id="label-Installation">Installation</h2>
|
90
|
+
<ul><li>
|
91
|
+
<p>If your project uses <a
|
92
|
+
href="https://github.com/bundler/bundler">Bundler</a>:</p>
|
93
|
+
</li><li>
|
94
|
+
<p>Add one of the following to your application's Gemfile: "`ruby</p>
|
81
95
|
|
82
|
-
<
|
96
|
+
<h2 id="label-For+on-demand+usage+...">For on-demand usage ...</h2>
|
83
97
|
|
84
|
-
<
|
98
|
+
<p>## gem 'env_parser'</p>
|
99
|
+
|
100
|
+
<h2 id="label-To+automatically+register+ENV">To automatically register ENV</h2>
|
101
|
+
|
102
|
+
<h2 id="label-constants+per+-22.env_parser.yml-22+...">constants per ".env_parser.yml" ...</h2>
|
103
|
+
|
104
|
+
<p>## gem 'env_parser', require: 'env_parser/autoregister' "`</p>
|
105
|
+
</li><li>
|
106
|
+
<p>And then run a: <code>shell $ bundle install </code></p>
|
107
|
+
</li><li>
|
108
|
+
<p>Or, you can keep things simple with a manual install: <code>shell $ gem
|
109
|
+
install env_parser </code></p>
|
110
|
+
</li></ul>
|
111
|
+
|
112
|
+
<h2 id="label-Syntax+Cheat+Sheet">Syntax Cheat Sheet</h2>
|
113
|
+
|
114
|
+
<pre class="code ruby"><code class="ruby">## Returns an ENV value parsed "as" a specific type:
|
115
|
+
##
|
116
|
+
EnvParser.parse env_key_as_a_symbol
|
117
|
+
as: … ## ➜ required
|
118
|
+
if_unset: … ## ➜ optional; default value
|
119
|
+
from_set: … ## ➜ optional; an Array or Range
|
120
|
+
validated_by: ->(value) { … } ## ➜ optional; may also be given as a block
|
121
|
+
|
122
|
+
## Parse an ENV value and register it as a constant:
|
123
|
+
##
|
124
|
+
EnvParser.register env_key_as_a_symbol
|
125
|
+
as: … ## ➜ required
|
126
|
+
within: … ## ➜ optional; Class or Module
|
127
|
+
if_unset: … ## ➜ optional; default value
|
128
|
+
from_set: … ## ➜ optional; an Array or Range
|
129
|
+
validated_by: ->(value) { … } ## ➜ optional; may also be given as a block
|
130
|
+
|
131
|
+
## Registers all ENV variables as spec'ed in ".env_parser.yml":
|
132
|
+
##
|
133
|
+
EnvParser.autoregister ## Note this is automatically called if your
|
134
|
+
## Gemfile included the "env_parser" gem with
|
135
|
+
## the "require: 'env_parser/autoregister'" option.
|
136
|
+
|
137
|
+
## Lets you call "parse" and "register" on ENV itself:
|
138
|
+
##
|
139
|
+
EnvParser.add_env_bindings ## ENV.parse will now be a proxy for EnvParser.parse
|
140
|
+
## and ENV.register will now be a proxy for EnvParser.register
|
85
141
|
</code></pre>
|
86
142
|
|
87
|
-
<
|
143
|
+
<h2 id="label-Extended+How-To-Use">Extended How-To-Use</h2>
|
144
|
+
|
145
|
+
<h4 id="label-Basic+Usage">Basic Usage</h4>
|
146
|
+
<ul><li>
|
147
|
+
<p><strong>Parsing <code>ENV</code> Values</strong></p>
|
148
|
+
</li></ul>
|
149
|
+
|
150
|
+
<p>At its core, EnvParser is a straight-forward parser for string values
|
151
|
+
(since that's all <code>ENV</code> ever gives you), allowing you to
|
152
|
+
read a given string <strong><em>as</em></strong> a variety of types.</p>
|
88
153
|
|
89
|
-
<
|
154
|
+
<p><code>ruby ## Returns ENV['TIMEOUT_MS'] as an Integer, ## or a sensible
|
155
|
+
default (0) if ENV['TIMEOUT_MS'] is unset. ## timeout_ms =
|
156
|
+
EnvParser.parse ENV['TIMEOUT_MS'], as: :integer </code></p>
|
90
157
|
|
91
|
-
<p>
|
158
|
+
<p>You can check the full documentation for <a
|
159
|
+
href="http://nestor-custodio.github.io/env_parser/EnvParser/Types.html">a
|
160
|
+
list of all as types available right out of the box</a>.</p>
|
161
|
+
<ul><li>
|
162
|
+
<p><strong>How About Less Typing?</strong></p>
|
163
|
+
</li></ul>
|
92
164
|
|
93
|
-
<
|
165
|
+
<p>EnvParser is all about ~~simplification~~ ~~less typing~~
|
166
|
+
<em>laziness</em>. If you pass in a symbol instead of a string, EnvParser
|
167
|
+
will look to <code>ENV</code> and use the value from the corresponding
|
168
|
+
(string) key.</p>
|
94
169
|
|
95
|
-
<
|
170
|
+
<p><code>ruby ## YAY, LESS TYPING! 😃 ## These two are the same: ##
|
171
|
+
more_typing = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer less_typing
|
172
|
+
= EnvParser.parse :TIMEOUT_MS, as: :integer </code></p>
|
173
|
+
<ul><li>
|
174
|
+
<p><strong>Registering Constants From <code>ENV</code> Values</strong></p>
|
175
|
+
</li></ul>
|
96
176
|
|
97
|
-
<
|
177
|
+
<p>The <code>EnvParser.register</code> method lets you “promote”
|
178
|
+
<code>ENV</code> variables into their own constants, already parsed into
|
179
|
+
the correct type.</p>
|
98
180
|
|
99
|
-
<
|
181
|
+
<p>“`ruby <a href="'API_KEY'">ENV</a> ## => 'unbreakable p4$$w0rd'</p>
|
100
182
|
|
101
|
-
<
|
102
|
-
|
103
|
-
</span>
|
104
|
-
<span class='id identifier rubyid_timeout_ms'>timeout_ms</span> <span class='op'>=</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='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>TIMEOUT_MS</span><span class='tstring_end'>'</span></span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span>
|
183
|
+
<p>EnvParser.register :API_KEY, as: :string API_KEY ## => 'unbreakable
|
184
|
+
p4$$w0rd' “`</p>
|
105
185
|
|
186
|
+
<p>By default, <code>EnvParser.register</code> will create the requested
|
187
|
+
constant within the Kernel module (making it available everywhere), but you
|
188
|
+
can specify any class or module you like.</p>
|
106
189
|
|
107
|
-
<
|
108
|
-
</
|
109
|
-
</span><span class='comment'>## will use the value behind the matching String key in ENV.
|
110
|
-
</span><span class='comment'>## (i.e. passing in ENV['X'] is equivalent to passing in :X)
|
111
|
-
</span>
|
112
|
-
<span class='id identifier rubyid_timeout_ms'>timeout_ms</span> <span class='op'>=</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'>:TIMEOUT_MS</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span>
|
113
|
-
</code></pre>
|
190
|
+
<p>“`ruby <a href="'BEST_VIDEO'">ENV</a> ## => '<a
|
191
|
+
href="https://youtu.be/L_jWHffIx5E">youtu.be/L_jWHffIx5E</a>'</p>
|
114
192
|
|
115
|
-
<p>
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
<h4 id="label-Setting+Non-Trivial+Defaults">Setting Non-Trivial Defaults</h4>
|
121
|
-
|
122
|
-
<pre class="code ruby"><code class="ruby"><span class='comment'>## If the ENV variable you want is unset (nil) or blank (''),
|
123
|
-
</span><span class='comment'>## the return value is a sensible default for the given "as" type
|
124
|
-
</span><span class='comment'>## (0 or 0.0 for numbers, an empty tring, an empty Array or Hash, etc).
|
125
|
-
</span><span class='comment'>## Sometimes you want a non-trivial default, however.
|
126
|
-
</span>
|
127
|
-
<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
|
128
|
-
</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
|
129
|
-
</span>
|
130
|
-
|
131
|
-
<span class='comment'>## Note that "if_unset" values are used as-is, with no type conversion.
|
132
|
-
</span>
|
133
|
-
<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'>Careful!</span><span class='tstring_end'>'</span></span> <span class='comment'>## => 'Careful!'
|
134
|
-
</span></code></pre>
|
135
|
-
<hr>
|
136
|
-
|
137
|
-
<h4 id="label-Setting+Constants+From+ENV+Values">Setting Constants From ENV Values</h4>
|
138
|
-
|
139
|
-
<pre class="code ruby"><code class="ruby"><span class='comment'>## Global constants...
|
140
|
-
</span>
|
141
|
-
<span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>API_KEY</span><span class='tstring_end'>'</span></span><span class='rbracket'>]</span> <span class='comment'>## => 'unbreakable p4$$w0rd'
|
142
|
-
</span>
|
143
|
-
<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_register'><span class='object_link'><a href="EnvParser.html#register-class_method" title="EnvParser.register (method)">register</a></span></span> <span class='symbol'>:API_KEY</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:string</span>
|
144
|
-
<span class='const'>API_KEY</span> <span class='comment'>## => 'unbreakable p4$$w0rd' (registered within the Kernel module, so it's available everywhere)
|
145
|
-
</span>
|
146
|
-
|
147
|
-
<span class='comment'>## ... and class/module-level constants!
|
148
|
-
</span>
|
149
|
-
<span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>ULTIMATE_LINK</span><span class='tstring_end'>'</span></span><span class='rbracket'>]</span> <span class='comment'>## => 'https://youtu.be/L_jWHffIx5E'
|
150
|
-
</span>
|
151
|
-
<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_register'><span class='object_link'><a href="EnvParser.html#register-class_method" title="EnvParser.register (method)">register</a></span></span> <span class='symbol'>:ULTIMATE_LINK</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:string</span><span class='comma'>,</span> <span class='label'>within:</span> <span class='const'>URI</span>
|
152
|
-
<span class='const'>URI</span><span class='op'>::</span><span class='const'>ULTIMATE_LINK</span> <span class='comment'>## => 'https://youtu.be/L_jWHffIx5E'
|
153
|
-
</span>
|
154
|
-
<span class='const'>ULTIMATE_LINK</span> <span class='comment'>## => raises NameError (the un-namespaced constant is only in scope within the URI module)
|
155
|
-
</span>
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
<span class='comment'>## You can also set multiple constants in one call, which is considerably cleaner to read:
|
160
|
-
</span>
|
161
|
-
<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_register'><span class='object_link'><a href="EnvParser.html#register-class_method" title="EnvParser.register (method)">register</a></span></span> <span class='symbol'>:A</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:string</span>
|
162
|
-
<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_register'><span class='object_link'><a href="EnvParser.html#register-class_method" title="EnvParser.register (method)">register</a></span></span> <span class='symbol'>:B</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'>25</span>
|
163
|
-
<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_register'><span class='object_link'><a href="EnvParser.html#register-class_method" title="EnvParser.register (method)">register</a></span></span> <span class='symbol'>:C</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:boolean</span><span class='comma'>,</span> <span class='label'>if_unset:</span> <span class='kw'>true</span>
|
164
|
-
|
165
|
-
|
166
|
-
<span class='comment'>## ... is equivalent to ...
|
167
|
-
</span>
|
168
|
-
<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_register'><span class='object_link'><a href="EnvParser.html#register-class_method" title="EnvParser.register (method)">register</a></span></span><span class='lparen'>(</span>
|
169
|
-
<span class='label'>A:</span> <span class='lbrace'>{</span> <span class='label'>as:</span> <span class='symbol'>:string</span> <span class='rbrace'>}</span><span class='comma'>,</span>
|
170
|
-
<span class='label'>B:</span> <span class='lbrace'>{</span> <span class='label'>as:</span> <span class='symbol'>:integer</span><span class='comma'>,</span> <span class='label'>if_unset:</span> <span class='int'>25</span> <span class='rbrace'>}</span><span class='comma'>,</span>
|
171
|
-
<span class='label'>C:</span> <span class='lbrace'>{</span> <span class='label'>as:</span> <span class='symbol'>:boolean</span><span class='comma'>,</span> <span class='label'>if_unset:</span> <span class='kw'>true</span> <span class='rbrace'>}</span>
|
172
|
-
<span class='rparen'>)</span>
|
173
|
-
</code></pre>
|
174
|
-
<hr>
|
193
|
+
<p>EnvParser.register :BEST_VIDEO, as: :string, within: URI URI::BEST_VIDEO
|
194
|
+
## => '<a
|
195
|
+
href="https://youtu.be/L_jWHffIx5E">youtu.be/L_jWHffIx5E</a>'
|
196
|
+
BEST_VIDEO ## => raises NameError “`</p>
|
175
197
|
|
176
|
-
<
|
198
|
+
<p>You can also register multiple constants with a single call, which is a bit
|
199
|
+
cleaner.</p>
|
177
200
|
|
178
|
-
<
|
179
|
-
|
180
|
-
</
|
181
|
-
<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_add_env_bindings'><span class='object_link'><a href="EnvParser.html#add_env_bindings-class_method" title="EnvParser.add_env_bindings (method)">add_env_bindings</a></span></span> <span class='comment'>## Sets up the proxy methods.
|
182
|
-
</span>
|
201
|
+
<p>“`ruby EnvParser.register :USERNAME, as: :string EnvParser.register
|
202
|
+
:PASSWORD, as: :string EnvParser.register :MOCK_API, as: :boolean, within:
|
203
|
+
MyClassOrModule }</p>
|
183
204
|
|
184
|
-
<
|
185
|
-
</span><span class='comment'>## which is more legible and feels more straight-forward.
|
186
|
-
</span>
|
187
|
-
<span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>SHORT_PI</span><span class='tstring_end'>'</span></span><span class='rbracket'>]</span> <span class='comment'>## => '3.1415926'
|
188
|
-
</span>
|
189
|
-
<span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span> <span class='symbol'>:SHORT_PI</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:float</span> <span class='comment'>## => 3.1415926
|
190
|
-
</span><span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_register'>register</span> <span class='symbol'>:SHORT_PI</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:float</span> <span class='comment'>## Your constant is set, my man!
|
191
|
-
</span>
|
205
|
+
<p>## … is equivalent to … ##</p>
|
192
206
|
|
193
|
-
<
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
</
|
207
|
+
<p>EnvParser.register USERNAME: { as: :string }, PASSWORD: { as: :string },
|
208
|
+
MOCK_API: { as: :boolean, within: MyClassOrModule } “`</p>
|
209
|
+
<ul><li>
|
210
|
+
<p><strong>Okay, But… How About Even Less Typing?</strong></p>
|
211
|
+
</li></ul>
|
198
212
|
|
199
|
-
<
|
213
|
+
<p>Calling <code>EnvParser.add_env_bindings</code> binds proxy
|
214
|
+
<code>parse</code> and <code>register</code> methods onto <code>ENV</code>.
|
215
|
+
With these bindings in place, you can call <code>parse</code> or
|
216
|
+
<code>register</code> on <code>ENV</code> itself, which is more legible and
|
217
|
+
feels more straight-forward.</p>
|
200
218
|
|
201
|
-
<
|
219
|
+
<p>“`ruby <a href="'SHORT_PI'">ENV</a> ## => '3.1415926' <a
|
220
|
+
href="'BETTER_PI'">ENV</a> ## => '[“flaky crust”, “strawberry
|
221
|
+
filling”]'</p>
|
202
222
|
|
203
|
-
<
|
204
|
-
</span><span class='comment'>## The "from_set" option lets you restrict the set of allowed values.
|
205
|
-
</span>
|
206
|
-
<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>
|
207
|
-
<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>
|
223
|
+
<p>## Bind the proxy methods. ## EnvParser.add_env_bindings</p>
|
208
224
|
|
225
|
+
<p>ENV.parse :SHORT_PI, as: :float ## => 3.1415926 ENV.register
|
226
|
+
:BETTER_PI, as: :array ## Your constant is set! “`</p>
|
209
227
|
|
210
|
-
<
|
211
|
-
</
|
212
|
-
|
213
|
-
</
|
228
|
+
<p>Note that the proxy <code>ENV.parse</code> method will (naturally)
|
229
|
+
<em>always</em> interpret the value given as an <code>ENV</code> key
|
230
|
+
(converting it to a string, if necessary), which is slightly different from
|
231
|
+
the original <code>EnvParser.parse</code> method.</p>
|
214
232
|
|
233
|
+
<p>“`ruby <a href="'SHORT_PI'">ENV</a> ## => '3.1415926'</p>
|
215
234
|
|
235
|
+
<p>EnvParser.parse 'SHORT_PI', as: :float ## => 'SHORT_PI'
|
236
|
+
as a float: 0.0 EnvParser.parse :SHORT_PI , as: :float ## => <a
|
237
|
+
href="'SHORT_PI'">ENV</a> as a float: 3.1415926</p>
|
238
|
+
|
239
|
+
<p>## Bind the proxy methods. ## EnvParser.add_env_bindings</p>
|
240
|
+
|
241
|
+
<p>ENV.parse 'SHORT_PI', as: :float ## => <a
|
242
|
+
href="'SHORT_PI'">ENV</a> as a float: 3.1415926 ENV.parse :SHORT_PI , as:
|
243
|
+
:float ## => <a href="'SHORT_PI'">ENV</a> as a float: 3.1415926 “`</p>
|
244
|
+
|
245
|
+
<p>Note also that the <code>ENV.parse</code> and <code>ENV.register</code>
|
246
|
+
binding is done safely and without polluting the method space for other
|
247
|
+
objects.</p>
|
248
|
+
|
249
|
+
<p><strong>All additional examples below will assume that <code>ENV</code>
|
250
|
+
bindings are already in place, for brevity's sake.</strong></p>
|
251
|
+
|
252
|
+
<h4 id="label-Ensuring+Usable+Values">Ensuring Usable Values</h4>
|
253
|
+
<ul><li>
|
254
|
+
<p><strong>Sensible Defaults</strong></p>
|
255
|
+
</li></ul>
|
216
256
|
|
217
|
-
<
|
218
|
-
|
219
|
-
|
257
|
+
<p>If the <code>ENV</code> variable you want is unset (<code>nil</code>) or
|
258
|
+
blank (<code>''</code>), the return value is a sensible default for
|
259
|
+
the given <strong><em>as</em></strong> type: 0 or 0.0 for numbers, an empty
|
260
|
+
string/array/hash, etc. Sometimes you want a non-trivial default, however.
|
261
|
+
The <strong><em>if_unset</em></strong> option lets you specify a default
|
262
|
+
that better meets your needs.</p>
|
220
263
|
|
264
|
+
<p><code>ruby ENV.parse :MISSING_VAR, as: :integer ## => 0 ENV.parse
|
265
|
+
:MISSING_VAR, as: :integer, if_unset: 250 ## => 250 </code></p>
|
221
266
|
|
222
|
-
<
|
223
|
-
|
224
|
-
|
225
|
-
<
|
267
|
+
<p>Note these default values are used as-is with no type conversion, so
|
268
|
+
exercise caution.</p>
|
269
|
+
|
270
|
+
<p><code>ruby ENV.parse :MISSING_VAR, as: :integer, if_unset:
|
271
|
+
'Careful!' ## => 'Careful!' (NOT AN INTEGER) </code></p>
|
272
|
+
<ul><li>
|
273
|
+
<p><strong>Selecting From A Set</strong></p>
|
274
|
+
</li></ul>
|
275
|
+
|
276
|
+
<p>Sometimes setting the <strong><em>as</em></strong> type is a bit too
|
277
|
+
open-ended. The <strong><em>from_set</em></strong> option lets you restrict
|
278
|
+
the domain of allowed values.</p>
|
279
|
+
|
280
|
+
<p>“`ruby 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
|
+
<p>## And if the value is not in the allowed set … ## ENV.parse :TWELVE, as:
|
284
|
+
:integer, from_set: (1..5) ## => raises EnvParser::ValueNotAllowedError
|
285
|
+
“`</p>
|
286
|
+
<ul><li>
|
287
|
+
<p><strong>Custom Validation Of Parsed Values</strong></p>
|
288
|
+
</li></ul>
|
289
|
+
|
290
|
+
<p>You can write your own, more complex validations by passing in a
|
291
|
+
<strong><em>validated_by</em></strong> lambda or an equivalent block. The
|
292
|
+
lambda/block should take one value and return true if the given value
|
293
|
+
passes the custom validation.</p>
|
294
|
+
|
295
|
+
<p>“`ruby ## Via a “validated_by” lambda … ## ENV.parse :MUST_BE_LOWERCASE,
|
296
|
+
as: :string, validated_by: ->(value) { value == value.downcase }</p>
|
297
|
+
|
298
|
+
<p>## … or with a block! ## ENV.parse(:MUST_BE_LOWERCASE, as: :string) {
|
299
|
+
|value| value == value.downcase } ENV.parse(:CONNECTION_RETRIES, as:
|
300
|
+
:integer, &:positive?) “`</p>
|
301
|
+
<ul><li>
|
302
|
+
<p><strong>Defining Your Own EnvParser “<em>as</em>” Types</strong></p>
|
303
|
+
</li></ul>
|
304
|
+
|
305
|
+
<p>If you use a particular validation many times or are often manipulating
|
306
|
+
values in the same way after EnvParser has done its thing, you may want to
|
307
|
+
register a new type altogether. Defining a new type makes your code both
|
308
|
+
more maintainable (all the logic for your special type is only defined
|
309
|
+
once) and more readable (your <code>parse</code> calls aren't littered
|
310
|
+
with type-checking cruft).</p>
|
311
|
+
|
312
|
+
<p>Something as repetitive as:</p>
|
313
|
+
|
314
|
+
<p>“`ruby a = ENV.parse :A, as: :int, if_unset: 6 raise unless
|
315
|
+
passes_all_my_checks?(a)</p>
|
316
|
+
|
317
|
+
<p>b = ENV.parse :B, as: :int, if_unset: 6 raise unless
|
318
|
+
passes_all_my_checks?(b) “`</p>
|
319
|
+
|
320
|
+
<p>… is perhaps best handled by defining a new type:</p>
|
321
|
+
|
322
|
+
<p>“`ruby EnvParser.define_type(:my_special_type_of_number, if_unset: 6) do
|
323
|
+
|value| value = value.to_i unless passes_all_my_checks?(value)
|
324
|
+
raise(EnvParser::ValueNotConvertibleError, 'cannot parse as a “special
|
325
|
+
type number”') end</p>
|
326
|
+
|
327
|
+
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_value'>value</span>
|
226
328
|
</code></pre>
|
227
|
-
<hr>
|
228
329
|
|
229
|
-
<
|
330
|
+
<p>end</p>
|
331
|
+
|
332
|
+
<p>a = ENV.parse :A, as: :my_special_type_of_number b = ENV.parse :B, as:
|
333
|
+
:my_special_type_of_number “`</p>
|
334
|
+
|
335
|
+
<h4 id="label-Auto-Registering+Constants">Auto-Registering Constants</h4>
|
336
|
+
<ul><li>
|
337
|
+
<p><strong>The <code>autoregister</code> Call</strong></p>
|
338
|
+
</li></ul>
|
230
339
|
|
231
|
-
<
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
<
|
340
|
+
<p>Consolidating all of your <code>EnvParser.register</code> calls into a
|
341
|
+
single place only makes sense. A single <code>EnvParser.autoregister</code>
|
342
|
+
call take a filename to read and process as a series of constant
|
343
|
+
registration requests. If no filename is given, the default
|
344
|
+
<code>".env_parser.yml"</code> is assumed.</p>
|
345
|
+
|
346
|
+
<p>You'll normally want to call <code>EnvParser.autoregister</code> as
|
347
|
+
early in your application as possible. For Rails applications (and other
|
348
|
+
frameworks that call <code>require 'bundler/setup'</code>),
|
349
|
+
requiring the EnvParser gem via …</p>
|
350
|
+
|
351
|
+
<p><code>ruby gem 'env_parser', require:
|
352
|
+
'env_parser/autoregister' </code></p>
|
353
|
+
|
354
|
+
<p>… will automatically make the autoregistration call for you as soon as the
|
355
|
+
gem is loaded (which should be early enough for most uses). If this is
|
356
|
+
<em>still</em> not early enough for your needs, you can always
|
357
|
+
<code>require 'env_parser/autoregister'</code> yourself even before
|
358
|
+
<code>bundler/setup</code> is invoked.</p>
|
359
|
+
<ul><li>
|
360
|
+
<p><strong>The “.env_parser.yml” File</strong></p>
|
361
|
+
</li></ul>
|
238
362
|
|
239
|
-
<
|
240
|
-
<
|
363
|
+
<p>If you recall, multiple constants can be registered via a single
|
364
|
+
<code>EnvParser.register</code> call:</p>
|
241
365
|
|
366
|
+
<p>“`ruby EnvParser.register :USERNAME, as: :string EnvParser.register
|
367
|
+
:PASSWORD, as: :string EnvParser.register :MOCK_API, as: :boolean, within:
|
368
|
+
MyClassOrModule }</p>
|
242
369
|
|
243
|
-
<
|
244
|
-
</span>
|
245
|
-
<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_define_type'><span class='object_link'><a href="EnvParser.html#define_type-class_method" title="EnvParser.define_type (method)">define_type</a></span></span><span class='lparen'>(</span><span class='symbol'>:my_special_type_of_number</span><span class='comma'>,</span> <span class='label'>if_unset:</span> <span class='kw'>nil</span><span class='rparen'>)</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_value'>value</span><span class='op'>|</span>
|
246
|
-
<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_to_i'>to_i</span>
|
247
|
-
<span class='kw'>unless</span> <span class='id identifier rubyid_passes_all_my_checks?'>passes_all_my_checks?</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='rparen'>)</span>
|
248
|
-
<span class='id identifier rubyid_raise'>raise</span><span class='lparen'>(</span><span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="EnvParser/ValueNotConvertibleError.html" title="EnvParser::ValueNotConvertibleError (class)">ValueNotConvertibleError</a></span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>cannot parse as a "special type number"</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span>
|
249
|
-
<span class='kw'>end</span>
|
370
|
+
<p>## … is equivalent to … ##</p>
|
250
371
|
|
251
|
-
|
252
|
-
|
372
|
+
<p>EnvParser.register USERNAME: { as: :string }, PASSWORD: { as: :string },
|
373
|
+
MOCK_API: { as: :boolean, within: MyClassOrModule } “`</p>
|
253
374
|
|
254
|
-
<
|
255
|
-
|
375
|
+
<p>The autoregistraton file is intended to read as a YAML version of what
|
376
|
+
you'd pass to the single-call version of
|
377
|
+
<code>EnvParser.register</code>: a single hash with keys for each of the
|
378
|
+
constants you'd like to register, with each value being the set of
|
379
|
+
options to parse that constant.</p>
|
256
380
|
|
381
|
+
<p>The equivalent autoregistration file for the above would be:</p>
|
257
382
|
|
258
|
-
<
|
259
|
-
</span><span class='comment'>## (all the logic for your special type is only defined once)
|
260
|
-
</span><span class='comment'>## and more readable (your "parse" calls aren't littered with
|
261
|
-
</span><span class='comment'>## type-checking cruft).
|
262
|
-
</span></code></pre>
|
263
|
-
<hr>
|
383
|
+
<p>“`yaml USERNAME: as: :string</p>
|
264
384
|
|
265
|
-
<p
|
266
|
-
|
267
|
-
|
385
|
+
<p>PASSWORD: as: :string</p>
|
386
|
+
|
387
|
+
<p>MOCK_API: as: :boolean within: MyClassOrModule “`</p>
|
388
|
+
|
389
|
+
<p>Because no Ruby <em>statements</em> can be safely represented via YAML, the
|
390
|
+
set of <code>EnvParser.register</code> options available via
|
391
|
+
autoregistration is limited to <strong><em>as</em></strong>,
|
392
|
+
<strong><em>within</em></strong>, <strong><em>if_unset</em></strong>, and
|
393
|
+
<strong><em>from_set</em></strong>. As an additional restriction,
|
394
|
+
<strong><em>from_set</em></strong> (if given) must be an array, as ranges
|
395
|
+
cannot be represented in YAML.</p>
|
268
396
|
|
269
397
|
<h2 id="label-Feature+Roadmap+-2F+Future+Development">Feature Roadmap / Future Development</h2>
|
270
398
|
|
271
|
-
<p>Additional features
|
399
|
+
<p>Additional features coming in the future:</p>
|
272
400
|
<ul><li>
|
273
|
-
<p>
|
274
|
-
|
275
|
-
</li><li>
|
276
|
-
<p>Continue to round out the “as” type selection as ideas come to mind,
|
277
|
-
suggestions are made, and pull requests are submitted.</p>
|
401
|
+
<p>Continue to round out the <strong><em>as</em></strong> type selection as
|
402
|
+
ideas come to mind, suggestions are made, and pull requests are submitted.</p>
|
278
403
|
</li></ul>
|
279
404
|
|
280
405
|
<h2 id="label-Contribution+-2F+Development">Contribution / Development</h2>
|
281
406
|
|
282
|
-
<p>Bug reports and pull requests are welcome
|
283
|
-
href="https://github.com/nestor-custodio/env_parser">github.com/nestor-custodio/env_parser</a
|
407
|
+
<p>Bug reports and pull requests are welcome at: <a
|
408
|
+
href="https://github.com/nestor-custodio/env_parser">github.com/nestor-custodio/env_parser</a></p>
|
284
409
|
|
285
410
|
<p>After checking out the repo, run <code>bin/setup</code> to install
|
286
|
-
dependencies. Then, run <code>
|
287
|
-
also run <code>bin/console</code> for an interactive prompt that
|
288
|
-
you to experiment.</p>
|
411
|
+
dependencies. Then, run <code>bundle exec rspec</code> to run the tests.
|
412
|
+
You can also run <code>bin/console</code> for an interactive prompt that
|
413
|
+
will allow you to experiment.</p>
|
289
414
|
|
290
|
-
<p>Linting is courtesy of <a
|
291
|
-
|
292
|
-
|
293
|
-
|
415
|
+
<p>Linting is courtesy of <a href="https://docs.rubocop.org/">Rubocop</a>
|
416
|
+
(<code>bundle exec rubocop</code>) and documentation is built using <a
|
417
|
+
href="https://yardoc.org/">Yard</a> (<code>bundle exec yard</code>). Please
|
418
|
+
ensure you have a clean bill of health from Rubocop and that any new
|
419
|
+
features and/or changes to behaviour are reflected in the documentation
|
420
|
+
before submitting a pull request.</p>
|
294
421
|
|
295
422
|
<h2 id="label-License">License</h2>
|
296
423
|
|
297
|
-
<p>
|
298
|
-
href="https://
|
424
|
+
<p>EnvParser is available as open source under the terms of the <a
|
425
|
+
href="https://tldrlegal.com/license/mit-license">MIT License</a>.</p>
|
299
426
|
</div></div>
|
300
427
|
|
301
428
|
<div id="footer">
|
302
|
-
Generated on
|
429
|
+
Generated on Sun Nov 3 21:30:37 2019 by
|
303
430
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
304
431
|
0.9.20 (ruby-2.4.2).
|
305
432
|
</div>
|