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.
Files changed (43) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +7 -6
  3. data/.rubocop.yml +56 -57
  4. data/.ruby-version +1 -1
  5. data/Gemfile.lock +58 -28
  6. data/README.md +252 -176
  7. data/docs/EnvParser.html +305 -169
  8. data/docs/EnvParser/AutoregisterFileNotFound.html +143 -0
  9. data/docs/EnvParser/Error.html +9 -9
  10. data/docs/EnvParser/TypeAlreadyDefinedError.html +143 -0
  11. data/docs/EnvParser/Types.html +128 -0
  12. data/docs/EnvParser/Types/BaseTypes.html +177 -0
  13. data/docs/EnvParser/Types/ChronologyTypes.html +159 -0
  14. data/docs/EnvParser/Types/InternetTypes.html +159 -0
  15. data/docs/EnvParser/UnknownTypeError.html +143 -0
  16. data/docs/EnvParser/UnparseableAutoregisterSpec.html +143 -0
  17. data/docs/EnvParser/ValueNotAllowedError.html +143 -0
  18. data/docs/EnvParser/ValueNotConvertibleError.html +143 -0
  19. data/docs/EnvParserTypes.html +129 -0
  20. data/docs/EnvParserTypes/BaseTypes.html +178 -0
  21. data/docs/EnvParserTypes/ChronologyTypes.html +159 -0
  22. data/docs/EnvParserTypes/InternetTypes.html +159 -0
  23. data/docs/EnvParserTypes/TimeTypes.html +158 -0
  24. data/docs/_index.html +108 -9
  25. data/docs/class_list.html +3 -3
  26. data/docs/css/style.css +7 -9
  27. data/docs/file.README.html +215 -226
  28. data/docs/file_list.html +2 -2
  29. data/docs/frames.html +2 -2
  30. data/docs/index.html +215 -226
  31. data/docs/js/app.js +69 -3
  32. data/docs/method_list.html +13 -5
  33. data/docs/top-level-namespace.html +9 -9
  34. data/env_parser.gemspec +6 -2
  35. data/lib/env_parser.rb +111 -83
  36. data/lib/env_parser/autoregister.rb +3 -0
  37. data/lib/env_parser/errors.rb +40 -0
  38. data/lib/env_parser/types.rb +3 -0
  39. data/lib/env_parser/types/base_types.rb +99 -26
  40. data/lib/env_parser/types/chronology_types.rb +104 -0
  41. data/lib/env_parser/types/internet_types.rb +99 -0
  42. data/lib/env_parser/version.rb +1 -1
  43. metadata +85 -10
@@ -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" charset="utf-8" />
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" charset="utf-8" />
9
+ <link rel="stylesheet" href="css/common.css" type="text/css" media="screen" />
10
10
 
11
11
 
12
12
 
@@ -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> &lt; Object<small class='search_info'>Top Level Namespace</small></div><ul><li id='object_EnvParser::Error' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="EnvParser/Error.html" title="EnvParser::Error (class)">Error</a></span> &lt; StandardError<small class='search_info'>EnvParser</small></div></li><li id='object_EnvParser::TypeAlreadyDefined' class='collapsed even'><div class='item' style='padding-left:45px'><span class='object_link'><a href="EnvParser/TypeAlreadyDefined.html" title="EnvParser::TypeAlreadyDefined (class)">TypeAlreadyDefined</a></span> &lt; Error<small class='search_info'>EnvParser</small></div></li><li id='object_EnvParser::ValueNotAllowed' class='collapsed odd'><div class='item' style='padding-left:45px'><span class='object_link'><a href="EnvParser/ValueNotAllowed.html" title="EnvParser::ValueNotAllowed (class)">ValueNotAllowed</a></span> &lt; Error<small class='search_info'>EnvParser</small></div></li></ul></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> &lt; 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> &lt; 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> &lt; 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> &lt; 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> &lt; 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> &lt; 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> &lt; 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> &lt; Error<small class='search_info'>EnvParser</small></div></li></ul></li>
47
47
 
48
48
  </ul>
49
49
  </div>
@@ -52,6 +52,7 @@ body {
52
52
  background: #fff;
53
53
  padding: 1.2em;
54
54
  padding-top: 0.2em;
55
+ box-sizing: border-box;
55
56
  }
56
57
 
57
58
  @media (max-width: 920px) {
@@ -245,6 +246,7 @@ ul.toplevel { list-style: none; padding-left: 0; font-size: 1.1em; }
245
246
 
246
247
  dl.constants { margin-left: 10px; }
247
248
  dl.constants dt { font-weight: bold; font-size: 1.1em; margin-bottom: 5px; }
249
+ dl.constants.compact dt { display: inline-block; font-weight: normal }
248
250
  dl.constants dd { width: 75%; white-space: pre; font-family: monospace; margin-bottom: 18px; }
249
251
  dl.constants .docstring .note:first-child { margin-top: 5px; }
250
252
 
@@ -326,13 +328,9 @@ ul.summary a, ul.summary a:visited {
326
328
  text-decoration: none; font-size: 1.1em;
327
329
  }
328
330
  ul.summary li { margin-bottom: 5px; }
329
- .summary .summary_signature {
330
- padding: 4px 8px;
331
- background: #f8f8f8;
332
- border: 1px solid #f0f0f0;
333
- border-radius: 5px;
334
- }
331
+ .summary_signature { padding: 4px 8px; background: #f8f8f8; border: 1px solid #f0f0f0; border-radius: 5px; }
335
332
  .summary_signature:hover { background: #CFEBFF; border-color: #A4CCDA; cursor: pointer; }
333
+ .summary_signature.deprecated { background: #ffe5e5; border-color: #e9dada; }
336
334
  ul.summary.compact li { display: inline-block; margin: 0px 5px 0px 0px; line-height: 2.6em;}
337
335
  ul.summary.compact .summary_signature { padding: 5px 7px; padding-right: 4px; }
338
336
  #content .summary_signature:hover a,
@@ -425,8 +423,8 @@ li.r2 { background: #fafafa; }
425
423
  #toc ol { padding-left: 1.8em; }
426
424
  #toc li { font-size: 1.1em; line-height: 1.7em; }
427
425
  #toc > ol > li { font-size: 1.1em; font-weight: bold; }
428
- #toc ol > ol { font-size: 0.9em; }
429
- #toc ol ol > ol { padding-left: 2.3em; }
426
+ #toc ol > li > ol { font-size: 0.9em; }
427
+ #toc ol ol > li > ol { padding-left: 2.3em; }
430
428
  #toc ol + li { margin-top: 0.3em; }
431
429
  #toc.hidden { padding: 10px; background: #fefefe; box-shadow: none; }
432
430
  #toc.hidden:hover { background: #fafafa; }
@@ -485,7 +483,7 @@ pre.code .rubyid_nth_ref { color: #6D79DE; }
485
483
  pre.code .regexp, .dregexp { color: #036A07; }
486
484
  pre.code a { border-bottom: 1px dotted #bbf; }
487
485
  /* inline code */
488
- p > code {
486
+ *:not(pre) > code {
489
487
  padding: 1px 3px 1px 3px;
490
488
  border: 1px solid #E1E1E8;
491
489
  background: #F7F7F9;
@@ -6,15 +6,15 @@
6
6
  <title>
7
7
  File: README
8
8
 
9
- &mdash; Documentation by YARD 0.9.11
9
+ &mdash; Documentation by YARD 0.9.26
10
10
 
11
11
  </title>
12
12
 
13
- <link rel="stylesheet" href="css/style.css" type="text/css" charset="utf-8" />
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" />
14
14
 
15
- <link rel="stylesheet" href="css/common.css" type="text/css" charset="utf-8" />
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" />
16
16
 
17
- <script type="text/javascript" charset="utf-8">
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
- <h1 id="label-EnvParser+rdoc-image-3Ahttps-3A-2F-2Fbadge.fury.io-2Frb-2Fenv_parser.svg">EnvParser <a href="https://badge.fury.io/rb/env_parser"><img src="https://badge.fury.io/rb/env_parser.svg"></a></h1>
62
-
63
- <p>If your code uses environment variables, you know that <code>ENV</code>
64
- will always surface these as strings. Interpreting these strings as the
65
- value you <em>actually</em> want to see/use takes some work, however: for
66
- numbers you need to cast with <code>#to_i</code>/<code>#to_f</code>, for
67
- booleans you need to check for a specific value (<code>ENV['SOME_VAR'] ==
68
- &#39;true&#39;</code>), etc. Maybe you want to set non-trivial defaults
69
- (something other than <code>0</code> or <code>&#39;&#39;</code>)? Maybe you
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&#39;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'] == &#39;true&#39;</code>) … maybe you want to set non-trivial defaults (something other than <code>0</code> or <code>&#39;&#39;</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&#39;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&#39;s Gemfile: "`ruby</p>
79
76
 
80
- <p>Add this line to your application&#39;s Gemfile:</p>
77
+ <h2 id="label-For+on-demand+usage+...">For on-demand usage ...</h2>
81
78
 
82
- <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_gem'>gem</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>env_parser</span><span class='tstring_end'>&#39;</span></span>
83
- </code></pre>
79
+ <p>## gem &#39;env_parser&#39;</p>
84
80
 
85
- <p>And then execute:</p>
81
+ <h2 id="label-To+automatically+register+ENV">To automatically register ENV</h2>
86
82
 
87
- <pre class="code ruby"><code class="ruby">$ bundle</code></pre>
83
+ <h2 id="label-constants+per+-22.env_parser.yml-22+...">constants per ".env_parser.yml" ...</h2>
88
84
 
89
- <p>Or install it yourself as:</p>
85
+ <p>## gem &#39;env_parser&#39;, require: &#39;env_parser/autoregister&#39; "`</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
- <pre class="code ruby"><code class="ruby">$ gem install env_parser</code></pre>
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 &quot;as&quot; 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: -&gt;(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: -&gt;(value) { … } ## ➜ optional; may also be given as a block
110
+
111
+ ## Registers all ENV variables as spec&#39;ed in &quot;.env_parser.yml&quot;:
112
+ ##
113
+ EnvParser.autoregister ## Note this is automatically called if your
114
+ ## Gemfile included the &quot;env_parser&quot; gem with
115
+ ## the &quot;require: &#39;env_parser/autoregister&#39;&quot; option.
116
+
117
+ ## Lets you call &quot;parse&quot; and &quot;register&quot; 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-Using+EnvParser">Using EnvParser</h2>
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
- <pre class="code ruby"><code class="ruby"><span class='comment'>## Returns ENV[&#39;TIMEOUT_MS&#39;] as an Integer.
98
- </span><span class='comment'>## Yields 0 if ENV[&#39;TIMEOUT_MS&#39;] is unset or nil.
99
- </span><span class='comment'>##
100
- </span><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'>&#39;</span><span class='tstring_content'>TIMEOUT_MS</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span>
101
-
102
- <span class='comment'>## LESS TYPING, PLZ! :(
103
- </span><span class='comment'>## If you pass in a Symbol instead of a String, EnvParser
104
- </span><span class='comment'>## will use the value behind the matching String key in ENV.
105
- </span><span class='comment'>## (i.e. passing in ENV[&#39;X&#39;] is equivalent to passing in :X)
106
- </span><span class='comment'>##
107
- </span><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>
108
- </code></pre>
109
- <hr>
110
-
111
- <p>The named <code>:as</code> parameter is required. The list of allowed
112
- values is user-expandable, but allowed values out-of-the-box are:</p>
113
- <table>
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>&lt; depends on JSON given &gt;</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 &#39;true&#39;,
156
- &#39;25&#39;, and &#39;null&#39; 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 (&#39;&#39;),
161
- </span><span class='comment'>## the return value is a sensible default for the given &quot;as&quot; 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'>## =&gt; 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'>## =&gt; 250
167
- </span>
168
- <span class='comment'>## Note that &quot;if_unset&quot; 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'>&#39;</span><span class='tstring_content'>Whoops!</span><span class='tstring_end'>&#39;</span></span> <span class='comment'>## =&gt; &#39;Whoops!&#39;
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 &quot;from_set&quot; 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'>## =&gt; raises EnvParser::ValueNotAllowed
184
- </span>
185
-
186
- <span class='comment'>## The &quot;validated_by&quot; 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'>-&gt;</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'>&amp;</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&#39;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~ &lt;s&gt;less typing&lt;/s&gt; <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
- <h4 id="label-Setting+Constants+From+ENV+Values">Setting Constants From ENV Values</h4>
196
-
197
- <pre class="code ruby"><code class="ruby"><span class='comment'>## Global constants...
198
- </span><span class='comment'>##
199
- </span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>API_KEY</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='comment'>## =&gt; &#39;unbreakable p4$$w0rd&#39; (Set elsewhere, like a &quot;.env&quot; file.)
200
- </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_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>
201
- <span class='const'>API_KEY</span> <span class='comment'>## =&gt; &#39;unbreakable p4$$w0rd&#39; (registered within the Kernel module, so it&#39;s available everywhere)
202
- </span>
203
- <span class='comment'>## ... and class/module constants!
204
- </span><span class='comment'>##
205
- </span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>ULTIMATE_LINK</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='comment'>## =&gt; &#39;https://youtu.be/L_jWHffIx5E&#39; (Set elsewhere, like a &quot;.env&quot; file.)
206
- </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_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>
207
- <span class='const'>URI</span><span class='op'>::</span><span class='const'>ULTIMATE_LINK</span> <span class='comment'>## =&gt; &#39;https://youtu.be/L_jWHffIx5E&#39; (You know you want to check it out!)
208
- </span><span class='const'>ULTIMATE_LINK</span> <span class='comment'>## =&gt; raises NameError (the un-namespaced constant is only in scope within the URI module)
209
- </span>
210
-
211
- <span class='comment'>## You can also set multiple constants in one call, which is considerably cleaner to read:
212
- </span><span class='comment'>##
213
- </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_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>
214
- <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>
215
- <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>
216
-
217
- <span class='comment'>## ... is equivalent to ...
218
- </span><span class='comment'>##
219
- </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_register'><span class='object_link'><a href="EnvParser.html#register-class_method" title="EnvParser.register (method)">register</a></span></span><span class='lparen'>(</span>
220
- <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>
221
- <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>
222
- <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>
223
- <span class='rparen'>)</span>
148
+ <p>“`ruby <a href="'API_KEY'">ENV</a> ## =&gt; &#39;unbreakable p4$$w0rd&#39;</p>
149
+
150
+ <p>EnvParser.register :API_KEY, as: :string API_KEY ## =&gt; &#39;unbreakable p4$$w0rd&#39; “`</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> ## =&gt; &#39;<a href="https://youtu.be/L_jWHffIx5E">youtu.be/L_jWHffIx5E</a>&#39;</p>
155
+
156
+ <p>EnvParser.register :BEST_VIDEO, as: :string, within: URI URI::BEST_VIDEO ## =&gt; &#39;<a href="https://youtu.be/L_jWHffIx5E">youtu.be/L_jWHffIx5E</a>&#39; BEST_VIDEO ## =&gt; 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> ## =&gt; &#39;3.1415926&#39; <a href="'BETTER_PI'">ENV</a> ## =&gt; &#39;[“flaky crust”, “strawberry filling”]&#39;</p>
172
+
173
+ <p>## Bind the proxy methods. ## EnvParser.add_env_bindings</p>
174
+
175
+ <p>ENV.parse :SHORT_PI, as: :float ## =&gt; 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> ## =&gt; &#39;3.1415926&#39;</p>
180
+
181
+ <p>EnvParser.parse &#39;SHORT_PI&#39;, as: :float ## =&gt; &#39;SHORT_PI&#39; as a float: 0.0 EnvParser.parse :SHORT_PI , as: :float ## =&gt; <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 &#39;SHORT_PI&#39;, as: :float ## =&gt; <a href="'SHORT_PI'">ENV</a> as a float: 3.1415926 ENV.parse :SHORT_PI , as: :float ## =&gt; <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&#39;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>&#39;&#39;</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 ## =&gt; 0 ENV.parse :MISSING_VAR, as: :integer, if_unset: 250 ## =&gt; 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: &#39;Careful!&#39; ## =&gt; &#39;Careful!&#39; (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) ## =&gt; 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: -&gt;(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, &amp;: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&#39;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, &#39;cannot parse as a “special type number”&#39;) 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
- <h4 id="label-Binding+EnvParser+Proxies+Onto+ENV">Binding EnvParser Proxies Onto ENV</h4>
227
-
228
- <pre class="code ruby"><code class="ruby"><span class='comment'>## To allow for even cleaner usage, you can bind proxy &quot;parse&quot; and &quot;register&quot; methods onto ENV.
229
- </span><span class='comment'>## This is done cleanly and without polluting the method space for any other objects.
230
- </span><span class='comment'>##
231
- </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_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.
232
- </span>
233
- <span class='comment'>## Now you can call &quot;parse&quot; and &quot;register&quot; on ENV itself. Note that ENV&#39;s proxy &quot;parse&quot; method will
234
- </span><span class='comment'>## attempt to interpret any value given as an ENV key (converting to a String, if necessary).
235
- </span><span class='comment'>##
236
- </span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>SHORT_PI</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='comment'>## =&gt; &#39;3.1415926&#39;
237
- </span><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'>## =&gt; 3.1415926
238
- </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!
239
- </span></code></pre>
240
-
241
- <h4 id="label-Defining+your+own+types+for+use+with+EnvParser">Defining your own types for use with EnvParser</h4>
242
-
243
- <pre class="code ruby"><code class="ruby"><span class='comment'>## If you use a particular validation many times, or are often manipulating values in the same way
244
- </span><span class='comment'>## after EnvParser has done its thing, you may want to register a new type altogether.
245
- </span><span class='comment'>##
246
- </span><span class='id identifier rubyid_a'>a</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'>:A</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:int</span><span class='comma'>,</span> <span class='label'>if_unset:</span> <span class='kw'>nil</span>
247
- <span class='id identifier rubyid_raise'>raise</span> <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_a'>a</span><span class='rparen'>)</span>
248
-
249
- <span class='id identifier rubyid_b'>b</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'>:B</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:int</span><span class='comma'>,</span> <span class='label'>if_unset:</span> <span class='kw'>nil</span>
250
- <span class='id identifier rubyid_raise'>raise</span> <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_b'>b</span><span class='rparen'>)</span>
251
-
252
- <span class='comment'>## ... is perhaps best handled by defining a new type:
253
- </span><span class='comment'>##
254
- </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_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>
255
- <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>
256
- <span class='id identifier rubyid_raise'>raise</span><span class='lparen'>(</span><span class='const'>StandardError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>this is not a &quot;special type&quot; number</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span> <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>
257
-
258
- <span class='id identifier rubyid_value'>value</span>
259
- <span class='kw'>end</span>
260
-
261
- <span class='id identifier rubyid_a'>a</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'>:A</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:my_special_type_of_number</span>
262
- <span class='id identifier rubyid_b'>b</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'>:B</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:my_special_type_of_number</span>
263
-
264
- <span class='comment'>## Defining a new type makes your code both more maintainable (all the logic for your special type
265
- </span><span class='comment'>## is only defined once) and more readable (your &quot;parse&quot; calls aren&#39;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>&quot;.env_parser.yml&quot;</code> is assumed.</p>
250
+
251
+ <p>You&#39;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 &#39;bundler/setup&#39;</code>), requiring the EnvParser gem via …</p>
252
+
253
+ <p><code>ruby gem &#39;env_parser&#39;, require: &#39;env_parser/autoregister&#39; </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 &#39;env_parser/autoregister&#39;</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&#39;d pass to the single-call version of <code>EnvParser.register</code>: a single hash with keys for each of the constants you&#39;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/options coming in the future:</p>
282
+ <p>Additional features coming in the future:</p>
276
283
  <ul><li>
277
- <p>Round out the as type selection with things like <code>:url</code>,
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 on GitHub at <a
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>Linting is courtesy of <a
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&#39;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>To install this gem onto your local machine, run <code>bundle exec rake
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>The gem is available as open source under the terms of the <a
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 Sun Dec 24 19:33:35 2017 by
301
+ Generated on Sat Jan 2 17:31:17 2021 by
313
302
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
314
- 0.9.11 (ruby-2.4.2).
303
+ 0.9.26 (ruby-2.7.2).
315
304
  </div>
316
305
 
317
306
  </div>