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.
@@ -58,7 +58,13 @@
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>
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&#39;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><em>the values themselves</em></strong> usable with a minimum of
78
- effort.</p>
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&#39;s Gemfile: "`ruby</p>
81
95
 
82
- <p>Add this line to your application&#39;s Gemfile:</p>
96
+ <h2 id="label-For+on-demand+usage+...">For on-demand usage ...</h2>
83
97
 
84
- <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>
98
+ <p>## gem &#39;env_parser&#39;</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 &#39;env_parser&#39;, require: &#39;env_parser/autoregister&#39; "`</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 &quot;as&quot; 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: -&gt;(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: -&gt;(value) { … } ## ➜ optional; may also be given as a block
130
+
131
+ ## Registers all ENV variables as spec&#39;ed in &quot;.env_parser.yml&quot;:
132
+ ##
133
+ EnvParser.autoregister ## Note this is automatically called if your
134
+ ## Gemfile included the &quot;env_parser&quot; gem with
135
+ ## the &quot;require: &#39;env_parser/autoregister&#39;&quot; option.
136
+
137
+ ## Lets you call &quot;parse&quot; and &quot;register&quot; 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
- <p>And then execute:</p>
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&#39;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
- <pre class="code ruby"><code class="ruby">$ bundle</code></pre>
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>Or install it yourself as:</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
- <pre class="code ruby"><code class="ruby">$ gem install env_parser</code></pre>
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
- <h2 id="label-Using+EnvParser">Using EnvParser</h2>
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
- <h3 id="label-Basic+Usage">Basic Usage</h3>
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
- <h4 id="label-Parsing+ENV+Values">Parsing ENV Values</h4>
181
+ <p>“`ruby <a href="'API_KEY'">ENV</a> ## =&gt; &#39;unbreakable p4$$w0rd&#39;</p>
100
182
 
101
- <pre class="code ruby"><code class="ruby"><span class='comment'>## Returns ENV[&#39;TIMEOUT_MS&#39;] as an Integer,
102
- </span><span class='comment'>## or 0 if ENV[&#39;TIMEOUT_MS&#39;] is unset or nil.
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'>&#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>
183
+ <p>EnvParser.register :API_KEY, as: :string API_KEY ## =&gt; &#39;unbreakable
184
+ p4$$w0rd&#39; “`</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
- <span class='comment'>## LESS TYPING, PLZ! :(
108
- </span><span class='comment'>## If you pass in a Symbol instead of a String, EnvParser
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[&#39;X&#39;] 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> ## =&gt; &#39;<a
191
+ href="https://youtu.be/L_jWHffIx5E">youtu.be/L_jWHffIx5E</a>&#39;</p>
114
192
 
115
- <p>For a full list of all “as types available out-of-the-box, <a
116
- href="http://nestor-custodio.github.io/env_parser/EnvParserTypes.html">see
117
- the documentation for modules listed under EnvParserTypes</a>.</p>
118
- <hr>
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 (&#39;&#39;),
123
- </span><span class='comment'>## the return value is a sensible default for the given &quot;as&quot; 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'>## =&gt; 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'>## =&gt; 250
129
- </span>
130
-
131
- <span class='comment'>## Note that &quot;if_unset&quot; 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'>&#39;</span><span class='tstring_content'>Careful!</span><span class='tstring_end'>&#39;</span></span> <span class='comment'>## =&gt; &#39;Careful!&#39;
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'>&#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;
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'>## =&gt; &#39;unbreakable p4$$w0rd&#39; (registered within the Kernel module, so it&#39;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'>&#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;
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'>## =&gt; &#39;https://youtu.be/L_jWHffIx5E&#39;
153
- </span>
154
- <span class='const'>ULTIMATE_LINK</span> <span class='comment'>## =&gt; 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
+ ## =&gt; &#39;<a
195
+ href="https://youtu.be/L_jWHffIx5E">youtu.be/L_jWHffIx5E</a>&#39;
196
+ BEST_VIDEO ## =&gt; raises NameError “`</p>
175
197
 
176
- <h4 id="label-Binding+EnvParser+Proxies+Onto+ENV">Binding EnvParser Proxies Onto ENV</h4>
198
+ <p>You can also register multiple constants with a single call, which is a bit
199
+ cleaner.</p>
177
200
 
178
- <pre class="code ruby"><code class="ruby"><span class='comment'>## You can bind proxy &quot;parse&quot; and &quot;register&quot; methods onto ENV.
179
- </span><span class='comment'>## This is done without polluting the method space for other objects.
180
- </span>
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
- <span class='comment'>## Now you can call &quot;parse&quot; and &quot;register&quot; on ENV itself,
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'>&#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;
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'>## =&gt; 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
- <span class='comment'>## Note that ENV&#39;s proxy &quot;parse&quot; method will *always* interpret the
194
- </span><span class='comment'>## value given as an ENV key (converting to a String, if necessary).
195
- </span><span class='comment'>## This is different from the non-proxy &quot;parse&quot; method, which will use
196
- </span><span class='comment'>## String values as-is and only looks up ENV values when given a Symbol.
197
- </span></code></pre>
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
- <h3 id="label-Advanced+Usage">Advanced Usage</h3>
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
- <h4 id="label-Custom+Validation+Of+Parsed+Values">Custom Validation Of Parsed Values</h4>
219
+ <p>“`ruby <a href="'SHORT_PI'">ENV</a> ## =&gt; &#39;3.1415926&#39; <a
220
+ href="'BETTER_PI'">ENV</a> ## =&gt; &#39;[“flaky crust”, “strawberry
221
+ filling”]&#39;</p>
202
222
 
203
- <pre class="code ruby"><code class="ruby"><span class='comment'>## Sometimes setting the type alone is a bit too open-ended.
204
- </span><span class='comment'>## The &quot;from_set&quot; 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 ## =&gt; 3.1415926 ENV.register
226
+ :BETTER_PI, as: :array ## Your constant is set! “`</p>
209
227
 
210
- <span class='comment'>## And if the value is not allowed...
211
- </span>
212
- <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::ValueNotAllowedError
213
- </span>
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> ## =&gt; &#39;3.1415926&#39;</p>
215
234
 
235
+ <p>EnvParser.parse &#39;SHORT_PI&#39;, as: :float ## =&gt; &#39;SHORT_PI&#39;
236
+ as a float: 0.0 EnvParser.parse :SHORT_PI , as: :float ## =&gt; <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 &#39;SHORT_PI&#39;, as: :float ## =&gt; <a
242
+ href="'SHORT_PI'">ENV</a> as a float: 3.1415926 ENV.parse :SHORT_PI , as:
243
+ :float ## =&gt; <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&#39;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
- <span class='comment'>## The &quot;validated_by&quot; option allows for more complex validation.
218
- </span>
219
- <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>
257
+ <p>If the <code>ENV</code> variable you want is unset (<code>nil</code>) or
258
+ blank (<code>&#39;&#39;</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 ## =&gt; 0 ENV.parse
265
+ :MISSING_VAR, as: :integer, if_unset: 250 ## =&gt; 250 </code></p>
221
266
 
222
- <span class='comment'>## ... but a block will also do the trick!
223
- </span>
224
- <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>
225
- <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'>:positive?</span><span class='rparen'>)</span>
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
+ &#39;Careful!&#39; ## =&gt; &#39;Careful!&#39; (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) ## =&gt; 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: -&gt;(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, &amp;: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&#39;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, &#39;cannot parse as a “special
325
+ type number”&#39;) 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
- <h4 id="label-Defining+Your+Own+EnvParser+-22as-22+Types">Defining Your Own EnvParser “as” Types</h4>
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
- <pre class="code ruby"><code class="ruby"><span class='comment'>## If you use a particular validation many times,
232
- </span><span class='comment'>## or are often manipulating values in the same way
233
- </span><span class='comment'>## after EnvParser has done its thing, you may want
234
- </span><span class='comment'>## to register a new type altogether.
235
- </span>
236
- <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>
237
- <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>
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>&quot;.env_parser.yml&quot;</code> is assumed.</p>
345
+
346
+ <p>You&#39;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 &#39;bundler/setup&#39;</code>),
349
+ requiring the EnvParser gem via …</p>
350
+
351
+ <p><code>ruby gem &#39;env_parser&#39;, require:
352
+ &#39;env_parser/autoregister&#39; </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 &#39;env_parser/autoregister&#39;</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
- <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>
240
- <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>
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
- <span class='comment'>## ... is perhaps best handled by defining a new type:
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'>&#39;</span><span class='tstring_content'>cannot parse as a &quot;special type number&quot;</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
249
- <span class='kw'>end</span>
370
+ <p>## is equivalent to ##</p>
250
371
 
251
- <span class='id identifier rubyid_value'>value</span>
252
- <span class='kw'>end</span>
372
+ <p>EnvParser.register USERNAME: { as: :string }, PASSWORD: { as: :string },
373
+ MOCK_API: { as: :boolean, within: MyClassOrModule } “`</p>
253
374
 
254
- <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>
255
- <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>
375
+ <p>The autoregistraton file is intended to read as a YAML version of what
376
+ you&#39;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&#39;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
- <span class='comment'>## Defining a new type makes your code both more maintainable
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 &quot;parse&quot; calls aren&#39;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><a
266
- href="http://nestor-custodio.github.io/env_parser/EnvParser.html">Consult
267
- the repo docs for the full EnvParser documentation.</a></p>
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/options coming in the future:</p>
399
+ <p>Additional features coming in the future:</p>
272
400
  <ul><li>
273
- <p>Allow for a “.env_parser” file where you can easily type-check and/or
274
- register (as constants) any ENV variables you like.</p>
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 on GitHub at <a
283
- href="https://github.com/nestor-custodio/env_parser">github.com/nestor-custodio/env_parser</a>.</p>
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>rake spec</code> to run the tests. You can
287
- also run <code>bin/console</code> for an interactive prompt that will allow
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
- href="https://github.com/bbatsov/rubocop">Rubocop</a> and documentation is
292
- built using <a href="https://yardoc.org/">Yard</a>. Neither is included in
293
- the Gemspec; you&#39;ll need to install these locally to take advantage.</p>
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>The gem is available as open source under the terms of the <a
298
- href="https://opensource.org/licenses/MIT">MIT License</a>.</p>
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 Mon Oct 21 00:33:12 2019 by
429
+ Generated on Sun Nov 3 21:30:35 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>