env_parser 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 824a4026a34c07f38be413d43c95c1a58b65794f
4
- data.tar.gz: 4a94816516c1925f887aaba58ff670af1189b3f8
3
+ metadata.gz: 1074b534666060176bf44c5b5d96c91ebf87f13e
4
+ data.tar.gz: a4bbaf7ec568cf6fb1a3c8adb75b0ba36e0abfcc
5
5
  SHA512:
6
- metadata.gz: 9ed47eb04f852943956de4fb37cf0a866c0ad9647405734e572c9994f2ff800cb18f57423a7db90766583eeaffd7394d61b5a5b5996d9a9833eec1cb2c54cb1f
7
- data.tar.gz: 1e0cbd84c815baf0d9359825279379b8abdc61e8aebadc7aadbb7739bad1e14b335822fec2217ad7fb3c80832a62e562dc9276fa255c6c8e695f43641c7d3756
6
+ metadata.gz: '0778ff18e226b11676ac3ef7b2c0d4ec939ce6ca5a272e5d2ddbfc29827ff29bb1fe96d5028190fa4deec42858a1235de02ca783fa04902c4eca140e62d0ff12'
7
+ data.tar.gz: 4e11313dca4ee78652b0dbe34af9d47b8bf7a61aa5ed7ebc7d119d8ada62a5b2d4d977caf194e6f88f0d4591e2fc902eaed9bd6ea665faa12fcfc07347cb69ee
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- env_parser (0.5.0)
4
+ env_parser (0.6.0)
5
5
  activesupport (>= 5.0.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -24,9 +24,9 @@ Or install it yourself as:
24
24
  $ gem install env_parser
25
25
 
26
26
 
27
- ## Usage
27
+ ## Using EnvParser
28
28
 
29
- #### Basic EnvParser usage:
29
+ #### Basic Usage
30
30
 
31
31
  ```ruby
32
32
  ## Returns ENV['TIMEOUT_MS'] as an Integer.
@@ -93,7 +93,7 @@ The named `:as` parameter is required. Allowed values are:
93
93
  Note JSON is parsed using *quirks-mode* (meaning 'true', '25', and 'null' are all considered valid, parseable JSON).
94
94
 
95
95
 
96
- #### Setting non-trivial defaults:
96
+ #### Setting Non-Trivial Defaults
97
97
 
98
98
  ```ruby
99
99
  ## If the ENV variable you want is unset (nil) or blank (''),
@@ -110,7 +110,7 @@ EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset: 'Whoops!' ## => '
110
110
  ```
111
111
 
112
112
 
113
- #### Validation of parsed ENV values:
113
+ #### Validating Parsed ENV Values
114
114
 
115
115
  ```ruby
116
116
  ## Sometimes setting the type alone is a bit too open-ended.
@@ -124,7 +124,8 @@ EnvParser.parse :SOME_CUSTOM_NETWORK_PORT, as: :integer, from_set: (1..65535), i
124
124
  EnvParser.parse :NEGATIVE_NUMBER, as: :integer, from_set: (1..5) ## => raises EnvParser::ValueNotAllowed
125
125
  ```
126
126
 
127
- #### Turning ENV values into constants:
127
+
128
+ #### Setting Constants From ENV Values
128
129
 
129
130
  ```ruby
130
131
  ## Global constants...
@@ -139,17 +140,14 @@ ENV['ULTIMATE_LINK'] ## => 'https://youtu.be/L_jWHffIx5E' (Set elsewhere, like a
139
140
  EnvParser.register :ULTIMATE_LINK, as: :string, within: URI
140
141
  URI::ULTIMATE_LINK ## => 'https://youtu.be/L_jWHffIx5E' (You know you want to check it out!)
141
142
  ULTIMATE_LINK ## => raises NameError (the un-namespaced constant is only in scope within the URI module)
142
- ```
143
143
 
144
- #### Quickly registering multiple ENV-derived constants:
145
144
 
146
- ```ruby
147
145
  ## You can also set multiple constants in one call, which is considerably cleaner to read:
148
146
  ##
149
147
  EnvParser.register :A, as: :string
150
148
  EnvParser.register :B, as: :integer, if_unset: 25
151
149
  EnvParser.register :C, as: :boolean, if_unset: true
152
- ##
150
+
153
151
  ## ... is equivalent to ...
154
152
  ##
155
153
  EnvParser.register(
@@ -159,6 +157,23 @@ EnvParser.register(
159
157
  )
160
158
  ```
161
159
 
160
+
161
+ #### Binding EnvParser Proxies Onto ENV
162
+
163
+ ```ruby
164
+ ## To allow for even cleaner usage, you can bind proxy "parse" and "register" methods onto ENV.
165
+ ## This is done cleanly and without polluting the method space for any other objects.
166
+ ##
167
+ EnvParser.add_env_bindings ## Sets up the proxy methods.
168
+
169
+ ## Now you can call "parse" and "register" on ENV itself. Note that ENV's proxy "parse" method will
170
+ ## attempt to interpret any value given as an ENV key (converting to a String, if necessary).
171
+ ##
172
+ ENV['SHORT_PI'] ## => '3.1415926'
173
+ ENV.parse :SHORT_PI, as: :float ## => 3.1415926
174
+ ENV.register :SHORT_PI, as: :float ## Your constant is set, my man!
175
+ ```
176
+
162
177
  ---
163
178
 
164
179
  [Consult the repo docs](http://nestor-custodio.github.io/env_parser) for the full EnvParser documentation.
@@ -167,7 +182,6 @@ EnvParser.register(
167
182
  ## Feature Roadmap / Future Development
168
183
 
169
184
  Additional features/options coming in the future:
170
- - A means to **optionally** bind `#parse`, `#regiter`, and `#register_all` methods onto `ENV`. Because `ENV.parse ...` reads better than `EnvParser.parse ...`.
171
185
  - A `validator` option that lets you pass in a validator lambda/block for things more complex than what a simple `from_set` can enforce.
172
186
  - A means to register validation blocks as new "as" types. This will allow for custom "as" types like `:url`, `:email`, etc.
173
187
  - ... ?
@@ -126,7 +126,7 @@ option.</p>
126
126
  </div>
127
127
 
128
128
  <div id="footer">
129
- Generated on Sun Dec 3 14:43:03 2017 by
129
+ Generated on Sun Dec 3 16:33:44 2017 by
130
130
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
131
131
  0.9.11 (ruby-2.4.2).
132
132
  </div>
data/docs/EnvParser.html CHANGED
@@ -130,7 +130,7 @@ different data types.</p>
130
130
  <dt id="VERSION-constant" class="">VERSION =
131
131
 
132
132
  </dt>
133
- <dd><pre class="code"><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.5.0</span><span class='tstring_end'>&#39;</span></span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span></pre></dd>
133
+ <dd><pre class="code"><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>0.6.0</span><span class='tstring_end'>&#39;</span></span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span></pre></dd>
134
134
 
135
135
  </dl>
136
136
 
@@ -151,6 +151,31 @@ different data types.</p>
151
151
  <li class="public ">
152
152
  <span class="summary_signature">
153
153
 
154
+ <a href="#add_env_bindings-class_method" title="add_env_bindings (class method)">.<strong>add_env_bindings</strong> &#x21d2; ENV </a>
155
+
156
+
157
+
158
+ </span>
159
+
160
+
161
+
162
+
163
+
164
+
165
+
166
+
167
+
168
+ <span class="summary_desc"><div class='inline'>
169
+ <p>Creates ENV bindings for EnvParser.parse and EnvParser.register proxy
170
+ methods.</p>
171
+ </div></span>
172
+
173
+ </li>
174
+
175
+
176
+ <li class="public ">
177
+ <span class="summary_signature">
178
+
154
179
  <a href="#parse-class_method" title="parse (class method)">.<strong>parse</strong>(value, options = {}) &#x21d2; Object </a>
155
180
 
156
181
 
@@ -207,7 +232,92 @@ requested context.</p>
207
232
 
208
233
 
209
234
  <div class="method_details first">
210
- <h3 class="signature first" id="parse-class_method">
235
+ <h3 class="signature first" id="add_env_bindings-class_method">
236
+
237
+ .<strong>add_env_bindings</strong> &#x21d2; <tt>ENV</tt>
238
+
239
+
240
+
241
+
242
+
243
+ </h3><div class="docstring">
244
+ <div class="discussion">
245
+
246
+ <p>Creates ENV bindings for EnvParser.parse and EnvParser.register proxy
247
+ methods.</p>
248
+
249
+ <p>The sole difference between these proxy methods and their EnvParser
250
+ counterparts is that ENV.parse will interpret any value given as an ENV key
251
+ (as a String), not the given value itself. i.e. ENV.parse(&#39;XYZ&#39;, …)
252
+ is equivalent to <a href="'XYZ'">EnvParser.parse(ENV</a>, …)</p>
253
+
254
+
255
+ </div>
256
+ </div>
257
+ <div class="tags">
258
+
259
+ <p class="tag_title">Returns:</p>
260
+ <ul class="return">
261
+
262
+ <li>
263
+
264
+
265
+ <span class='type'>(<tt>ENV</tt>)</span>
266
+
267
+
268
+
269
+ &mdash;
270
+ <div class='inline'>
271
+ <p>This generates no usable value, so we may as well return ENV for chaining?</p>
272
+ </div>
273
+
274
+ </li>
275
+
276
+ </ul>
277
+
278
+ </div><table class="source_code">
279
+ <tr>
280
+ <td>
281
+ <pre class="lines">
282
+
283
+
284
+ 160
285
+ 161
286
+ 162
287
+ 163
288
+ 164
289
+ 165
290
+ 166
291
+ 167
292
+ 168
293
+ 169
294
+ 170
295
+ 171
296
+ 172</pre>
297
+ </td>
298
+ <td>
299
+ <pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 160</span>
300
+
301
+ <span class='kw'>def</span> <span class='id identifier rubyid_add_env_bindings'>add_env_bindings</span>
302
+ <span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_instance_eval'>instance_eval</span> <span class='kw'>do</span>
303
+ <span class='kw'>def</span> <span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='id identifier rubyid_name'>name</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span> <span class='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='rparen'>)</span>
304
+ <span class='const'><span class='object_link'><a href="" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span><span class='lparen'>(</span><span class='kw'>self</span><span class='lbracket'>[</span><span class='id identifier rubyid_name'>name</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
305
+ <span class='kw'>end</span>
306
+
307
+ <span class='kw'>def</span> <span class='id identifier rubyid_register'>register</span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_args'>args</span><span class='rparen'>)</span>
308
+ <span class='const'><span class='object_link'><a href="" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_register'><span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">register</a></span></span><span class='lparen'>(</span><span class='op'>*</span><span class='id identifier rubyid_args'>args</span><span class='rparen'>)</span>
309
+ <span class='kw'>end</span>
310
+ <span class='kw'>end</span>
311
+
312
+ <span class='const'>ENV</span>
313
+ <span class='kw'>end</span></pre>
314
+ </td>
315
+ </tr>
316
+ </table>
317
+ </div>
318
+
319
+ <div class="method_details ">
320
+ <h3 class="signature " id="parse-class_method">
211
321
 
212
322
  .<strong>parse</strong>(value, options = {}) &#x21d2; <tt>Object</tt>
213
323
 
@@ -450,8 +560,8 @@ the same keys as those in the “from” Hash and each value being the
450
560
  ## ... is equivalent to ...
451
561
 
452
562
  EnvParser.register(
453
- A: { from: ENV, as: :integer }
454
- B: { from: other_hash, as: :string, if_unset: &#39;none&#39; }
563
+ A: { from: one_hash, as: :integer }
564
+ B: { from: another_hash, as: :string, if_unset: &#39;none&#39; }
455
565
  )
456
566
  </code></pre>
457
567
 
@@ -670,7 +780,7 @@ Kernel (making it a global constant).</p>
670
780
  </div>
671
781
 
672
782
  <div id="footer">
673
- Generated on Sun Dec 3 14:43:03 2017 by
783
+ Generated on Sun Dec 3 16:33:44 2017 by
674
784
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
675
785
  0.9.11 (ruby-2.4.2).
676
786
  </div>
data/docs/_index.html CHANGED
@@ -112,7 +112,7 @@
112
112
  </div>
113
113
 
114
114
  <div id="footer">
115
- Generated on Sun Dec 3 14:43:03 2017 by
115
+ Generated on Sun Dec 3 16:33:43 2017 by
116
116
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
117
117
  0.9.11 (ruby-2.4.2).
118
118
  </div>
@@ -91,9 +91,9 @@ simple.</p>
91
91
 
92
92
  <pre class="code ruby"><code class="ruby">$ gem install env_parser</code></pre>
93
93
 
94
- <h2 id="label-Usage">Usage</h2>
94
+ <h2 id="label-Using+EnvParser">Using EnvParser</h2>
95
95
 
96
- <h4 id="label-Basic+EnvParser+usage-3A">Basic EnvParser usage:</h4>
96
+ <h4 id="label-Basic+Usage">Basic Usage</h4>
97
97
 
98
98
  <pre class="code ruby"><code class="ruby"><span class='comment'>## Returns ENV[&#39;TIMEOUT_MS&#39;] as an Integer.
99
99
  </span><span class='comment'>## Yields 0 if ENV[&#39;TIMEOUT_MS&#39;] is unset or nil.
@@ -155,7 +155,7 @@ simple.</p>
155
155
  <p>Note JSON is parsed using <em>quirks-mode</em> (meaning &#39;true&#39;,
156
156
  &#39;25&#39;, and &#39;null&#39; are all considered valid, parseable JSON).</p>
157
157
 
158
- <h4 id="label-Setting+non-trivial+defaults-3A">Setting non-trivial defaults:</h4>
158
+ <h4 id="label-Setting+Non-Trivial+Defaults">Setting Non-Trivial Defaults</h4>
159
159
 
160
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
161
  </span><span class='comment'>## the return value is a sensible default for the given &quot;as&quot; type
@@ -170,7 +170,7 @@ simple.</p>
170
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
171
  </span></code></pre>
172
172
 
173
- <h4 id="label-Validation+of+parsed+ENV+values-3A">Validation of parsed ENV values:</h4>
173
+ <h4 id="label-Validating+Parsed+ENV+Values">Validating Parsed ENV Values</h4>
174
174
 
175
175
  <pre class="code ruby"><code class="ruby"><span class='comment'>## Sometimes setting the type alone is a bit too open-ended.
176
176
  </span><span class='comment'>## The &quot;from_set&quot; option lets you restrict the set of allowed values.
@@ -183,7 +183,7 @@ simple.</p>
183
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
184
  </span></code></pre>
185
185
 
186
- <h4 id="label-Turning+ENV+values+into+constants-3A">Turning ENV values into constants:</h4>
186
+ <h4 id="label-Setting+Constants+From+ENV+Values">Setting Constants From ENV Values</h4>
187
187
 
188
188
  <pre class="code ruby"><code class="ruby"><span class='comment'>## Global constants...
189
189
  </span><span class='comment'>##
@@ -197,17 +197,15 @@ simple.</p>
197
197
  </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>
198
198
  <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!)
199
199
  </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)
200
- </span></code></pre>
201
-
202
- <h4 id="label-Quickly+registering+multiple+ENV-derived+constants-3A">Quickly registering multiple ENV-derived constants:</h4>
200
+ </span>
203
201
 
204
- <pre class="code ruby"><code class="ruby"><span class='comment'>## You can also set multiple constants in one call, which is considerably cleaner to read:
202
+ <span class='comment'>## You can also set multiple constants in one call, which is considerably cleaner to read:
205
203
  </span><span class='comment'>##
206
204
  </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>
207
205
  <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>
208
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_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>
209
- <span class='comment'>##
210
- </span><span class='comment'>## ... is equivalent to ...
207
+
208
+ <span class='comment'>## ... is equivalent to ...
211
209
  </span><span class='comment'>##
212
210
  </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>
213
211
  <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>
@@ -215,6 +213,21 @@ simple.</p>
215
213
  <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>
216
214
  <span class='rparen'>)</span>
217
215
  </code></pre>
216
+
217
+ <h4 id="label-Binding+EnvParser+Proxies+Onto+ENV">Binding EnvParser Proxies Onto ENV</h4>
218
+
219
+ <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.
220
+ </span><span class='comment'>## This is done cleanly and without polluting the method space for any other objects.
221
+ </span><span class='comment'>##
222
+ </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.
223
+ </span>
224
+ <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
225
+ </span><span class='comment'>## attempt to interpret any value given as an ENV key (converting to a String, if necessary).
226
+ </span><span class='comment'>##
227
+ </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;
228
+ </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
229
+ </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!
230
+ </span></code></pre>
218
231
  <hr>
219
232
 
220
233
  <p><a href="http://nestor-custodio.github.io/env_parser">Consult the repo
@@ -222,15 +235,12 @@ docs</a> for the full EnvParser documentation.</p>
222
235
 
223
236
  <h2 id="label-Feature+Roadmap+-2F+Future+Development">Feature Roadmap / Future Development</h2>
224
237
 
225
- <p>Additional features/options coming in the future: - A means to
226
- <strong>optionally</strong> bind <code>#parse</code>,
227
- <code>#regiter</code>, and <code>#register_all</code> methods onto
228
- <code>ENV</code>. Because <code>ENV.parse ...</code> reads better than
229
- <code>EnvParser.parse ...</code>. - A <code>validator</code> option that
230
- lets you pass in a validator lambda/block for things more complex than what
231
- a simple <code>from_set</code> can enforce. - A means to register
232
- validation blocks as new “as” types. This will allow for custom “as” types
233
- like <code>:url</code>, <code>:email</code>, etc. - … ?</p>
238
+ <p>Additional features/options coming in the future: - A
239
+ <code>validator</code> option that lets you pass in a validator
240
+ lambda/block for things more complex than what a simple
241
+ <code>from_set</code> can enforce. - A means to register validation blocks
242
+ as new “as” types. This will allow for custom “as” types like
243
+ <code>:url</code>, <code>:email</code>, etc. - ?</p>
234
244
 
235
245
  <h2 id="label-Contribution+-2F+Development">Contribution / Development</h2>
236
246
 
@@ -261,7 +271,7 @@ href="https://opensource.org/licenses/MIT">MIT License</a>.</p>
261
271
  </div></div>
262
272
 
263
273
  <div id="footer">
264
- Generated on Sun Dec 3 14:43:03 2017 by
274
+ Generated on Sun Dec 3 16:33:43 2017 by
265
275
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
266
276
  0.9.11 (ruby-2.4.2).
267
277
  </div>
data/docs/index.html CHANGED
@@ -91,9 +91,9 @@ simple.</p>
91
91
 
92
92
  <pre class="code ruby"><code class="ruby">$ gem install env_parser</code></pre>
93
93
 
94
- <h2 id="label-Usage">Usage</h2>
94
+ <h2 id="label-Using+EnvParser">Using EnvParser</h2>
95
95
 
96
- <h4 id="label-Basic+EnvParser+usage-3A">Basic EnvParser usage:</h4>
96
+ <h4 id="label-Basic+Usage">Basic Usage</h4>
97
97
 
98
98
  <pre class="code ruby"><code class="ruby"><span class='comment'>## Returns ENV[&#39;TIMEOUT_MS&#39;] as an Integer.
99
99
  </span><span class='comment'>## Yields 0 if ENV[&#39;TIMEOUT_MS&#39;] is unset or nil.
@@ -155,7 +155,7 @@ simple.</p>
155
155
  <p>Note JSON is parsed using <em>quirks-mode</em> (meaning &#39;true&#39;,
156
156
  &#39;25&#39;, and &#39;null&#39; are all considered valid, parseable JSON).</p>
157
157
 
158
- <h4 id="label-Setting+non-trivial+defaults-3A">Setting non-trivial defaults:</h4>
158
+ <h4 id="label-Setting+Non-Trivial+Defaults">Setting Non-Trivial Defaults</h4>
159
159
 
160
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
161
  </span><span class='comment'>## the return value is a sensible default for the given &quot;as&quot; type
@@ -170,7 +170,7 @@ simple.</p>
170
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
171
  </span></code></pre>
172
172
 
173
- <h4 id="label-Validation+of+parsed+ENV+values-3A">Validation of parsed ENV values:</h4>
173
+ <h4 id="label-Validating+Parsed+ENV+Values">Validating Parsed ENV Values</h4>
174
174
 
175
175
  <pre class="code ruby"><code class="ruby"><span class='comment'>## Sometimes setting the type alone is a bit too open-ended.
176
176
  </span><span class='comment'>## The &quot;from_set&quot; option lets you restrict the set of allowed values.
@@ -183,7 +183,7 @@ simple.</p>
183
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
184
  </span></code></pre>
185
185
 
186
- <h4 id="label-Turning+ENV+values+into+constants-3A">Turning ENV values into constants:</h4>
186
+ <h4 id="label-Setting+Constants+From+ENV+Values">Setting Constants From ENV Values</h4>
187
187
 
188
188
  <pre class="code ruby"><code class="ruby"><span class='comment'>## Global constants...
189
189
  </span><span class='comment'>##
@@ -197,17 +197,15 @@ simple.</p>
197
197
  </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>
198
198
  <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!)
199
199
  </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)
200
- </span></code></pre>
201
-
202
- <h4 id="label-Quickly+registering+multiple+ENV-derived+constants-3A">Quickly registering multiple ENV-derived constants:</h4>
200
+ </span>
203
201
 
204
- <pre class="code ruby"><code class="ruby"><span class='comment'>## You can also set multiple constants in one call, which is considerably cleaner to read:
202
+ <span class='comment'>## You can also set multiple constants in one call, which is considerably cleaner to read:
205
203
  </span><span class='comment'>##
206
204
  </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>
207
205
  <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>
208
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_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>
209
- <span class='comment'>##
210
- </span><span class='comment'>## ... is equivalent to ...
207
+
208
+ <span class='comment'>## ... is equivalent to ...
211
209
  </span><span class='comment'>##
212
210
  </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>
213
211
  <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>
@@ -215,6 +213,21 @@ simple.</p>
215
213
  <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>
216
214
  <span class='rparen'>)</span>
217
215
  </code></pre>
216
+
217
+ <h4 id="label-Binding+EnvParser+Proxies+Onto+ENV">Binding EnvParser Proxies Onto ENV</h4>
218
+
219
+ <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.
220
+ </span><span class='comment'>## This is done cleanly and without polluting the method space for any other objects.
221
+ </span><span class='comment'>##
222
+ </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.
223
+ </span>
224
+ <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
225
+ </span><span class='comment'>## attempt to interpret any value given as an ENV key (converting to a String, if necessary).
226
+ </span><span class='comment'>##
227
+ </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;
228
+ </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
229
+ </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!
230
+ </span></code></pre>
218
231
  <hr>
219
232
 
220
233
  <p><a href="http://nestor-custodio.github.io/env_parser">Consult the repo
@@ -222,15 +235,12 @@ docs</a> for the full EnvParser documentation.</p>
222
235
 
223
236
  <h2 id="label-Feature+Roadmap+-2F+Future+Development">Feature Roadmap / Future Development</h2>
224
237
 
225
- <p>Additional features/options coming in the future: - A means to
226
- <strong>optionally</strong> bind <code>#parse</code>,
227
- <code>#regiter</code>, and <code>#register_all</code> methods onto
228
- <code>ENV</code>. Because <code>ENV.parse ...</code> reads better than
229
- <code>EnvParser.parse ...</code>. - A <code>validator</code> option that
230
- lets you pass in a validator lambda/block for things more complex than what
231
- a simple <code>from_set</code> can enforce. - A means to register
232
- validation blocks as new “as” types. This will allow for custom “as” types
233
- like <code>:url</code>, <code>:email</code>, etc. - … ?</p>
238
+ <p>Additional features/options coming in the future: - A
239
+ <code>validator</code> option that lets you pass in a validator
240
+ lambda/block for things more complex than what a simple
241
+ <code>from_set</code> can enforce. - A means to register validation blocks
242
+ as new “as” types. This will allow for custom “as” types like
243
+ <code>:url</code>, <code>:email</code>, etc. - ?</p>
234
244
 
235
245
  <h2 id="label-Contribution+-2F+Development">Contribution / Development</h2>
236
246
 
@@ -261,7 +271,7 @@ href="https://opensource.org/licenses/MIT">MIT License</a>.</p>
261
271
  </div></div>
262
272
 
263
273
  <div id="footer">
264
- Generated on Sun Dec 3 14:43:03 2017 by
274
+ Generated on Sun Dec 3 16:33:43 2017 by
265
275
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
266
276
  0.9.11 (ruby-2.4.2).
267
277
  </div>
@@ -46,13 +46,21 @@
46
46
 
47
47
  <li class="odd ">
48
48
  <div class="item">
49
- <span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span>
49
+ <span class='object_link'><a href="EnvParser.html#add_env_bindings-class_method" title="EnvParser.add_env_bindings (method)">add_env_bindings</a></span>
50
50
  <small>EnvParser</small>
51
51
  </div>
52
52
  </li>
53
53
 
54
54
 
55
55
  <li class="even ">
56
+ <div class="item">
57
+ <span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span>
58
+ <small>EnvParser</small>
59
+ </div>
60
+ </li>
61
+
62
+
63
+ <li class="odd ">
56
64
  <div class="item">
57
65
  <span class='object_link'><a href="EnvParser.html#register-class_method" title="EnvParser.register (method)">register</a></span>
58
66
  <small>EnvParser</small>
@@ -100,7 +100,7 @@
100
100
  </div>
101
101
 
102
102
  <div id="footer">
103
- Generated on Sun Dec 3 14:43:03 2017 by
103
+ Generated on Sun Dec 3 16:33:43 2017 by
104
104
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
105
105
  0.9.11 (ruby-2.4.2).
106
106
  </div>
@@ -1,3 +1,3 @@
1
1
  class EnvParser
2
- VERSION = '0.5.0'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
  end
data/lib/env_parser.rb CHANGED
@@ -89,8 +89,8 @@ class EnvParser
89
89
  ## ## ... is equivalent to ...
90
90
  ##
91
91
  ## EnvParser.register(
92
- ## A: { from: ENV, as: :integer }
93
- ## B: { from: other_hash, as: :string, if_unset: 'none' }
92
+ ## A: { from: one_hash, as: :integer }
93
+ ## B: { from: another_hash, as: :string, if_unset: 'none' }
94
94
  ## )
95
95
  ## </pre>
96
96
  ##
@@ -148,6 +148,29 @@ class EnvParser
148
148
  value
149
149
  end
150
150
 
151
+ ## Creates ENV bindings for EnvParser.parse and EnvParser.register proxy methods.
152
+ ##
153
+ ## The sole difference between these proxy methods and their EnvParser counterparts is that
154
+ ## ENV.parse will interpret any value given as an ENV key (as a String), not the given value
155
+ ## itself. i.e. ENV.parse('XYZ', ...) is equivalent to EnvParser.parse(ENV['XYZ'], ...)
156
+ ##
157
+ ## @return [ENV]
158
+ ## This generates no usable value, so we may as well return ENV for chaining?
159
+ ##
160
+ def add_env_bindings
161
+ ENV.instance_eval do
162
+ def parse(name, options = {})
163
+ EnvParser.parse(self[name.to_s], options)
164
+ end
165
+
166
+ def register(*args)
167
+ EnvParser.register(*args)
168
+ end
169
+ end
170
+
171
+ ENV
172
+ end
173
+
151
174
  private
152
175
 
153
176
  def parse_string(value)
@@ -206,6 +229,9 @@ class EnvParser
206
229
  ##
207
230
  ## @param set [Array, Range]
208
231
  ##
232
+ ## @return [nil]
233
+ ## This generates no usable value.
234
+ ##
209
235
  ## @raise [ArgumentError, EnvParser::ValueNotAllowed]
210
236
  ##
211
237
  def check_for_set_inclusion(value, set: nil)
@@ -218,6 +244,8 @@ class EnvParser
218
244
  end
219
245
 
220
246
  raise ValueNotAllowed, 'parsed value not in allowed list/range' unless set.include?(value)
247
+
248
+ nil
221
249
  end
222
250
 
223
251
  ## Receives a list of "register" calls to make, as a Hash keyed with variable names and the
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nestor Custodio