env_parser 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4a74430339836ddc96fb9cc9a263cda76cd56e8
4
- data.tar.gz: 54a8006eafc16759eaf77d5b599f3d971ceaa1cc
3
+ metadata.gz: dc268898780679cefa38eabfd71a3b40277289b8
4
+ data.tar.gz: f2f248a37d15f2bcc68d1a52f24f27a82d331fca
5
5
  SHA512:
6
- metadata.gz: 1fb0f1411eed85ebb78ef6d60954252a1625bffb4a7018c2273eaee3412cf932fa48e3d09028b9786054ecf4df683244f9ceaa9727d70254343107b2d14eb81d
7
- data.tar.gz: 855b7753725fdc5da43cb1d064fbf96717e50e0e4bb8190c40a58f2d73ad29eeff3b3156ed125ba215f45c83587d27d8cbdd0b34a0d715d69931d4b3ec50eb41
6
+ metadata.gz: f4ef8caacf69a8da90b1084cee02a85f74b8bb5ca6b073599d2d034e9ff8f6f2907b29ba1c542e845c96827010335ea19c16ce68b946f72c28d9ec7cd6a624de
7
+ data.tar.gz: 0f0812c3038e21463dc8f3fbf7ecc26aea7a9035a8cb594edcce8b861f498afbc8ee440d91a861cadc4d649dcdaa8eb1a10067fa35d65d4e94afa49234c8a75e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- env_parser (0.3.0)
4
+ env_parser (0.4.0)
5
5
  activesupport (>= 5.0.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -49,17 +49,32 @@ EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset: 250 ## => 250
49
49
 
50
50
  ## Note that "if_unset" values are used as-is, with no type conversion.
51
51
  ##
52
- EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset: 'oof!' ## => 'oof!'
52
+ EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset: 'Whoops!' ## => 'Whoops!'
53
53
 
54
- ## You can also restrict the set of allowed values.
55
- ## (Sometimes setting the type alone is a bit too open-ended.)
54
+ ## Sometimes setting the type alone is a bit too open-ended.
55
+ ## The "from_set" option lets you restrict the set of allowed values.
56
56
  ##
57
57
  EnvParser.parse :API_TO_USE, as: :symbol, from_set: %i[internal external]
58
58
  EnvParser.parse :SOME_CUSTOM_NETWORK_PORT, as: :integer, from_set: (1..65535), if_unset: 80
59
59
 
60
60
  ## And if the value is not allowed...
61
61
  ##
62
- EnvParser.parse :SOME_NEGATIVE_NUMBER, as: :integer, from_set: (1..5) ## => raises EnvParser::ValueNotAllowed
62
+ EnvParser.parse :NEGATIVE_NUMBER, as: :integer, from_set: (1..5) ## => raises EnvParser::ValueNotAllowed
63
+
64
+ ## You can also SET CONSTANTS DIRECTLY FROM YOUR ENV VARIABLES.
65
+
66
+ ## Global constants...
67
+ ##
68
+ ENV['API_KEY'] = 'unbreakable p4$$w0rd'
69
+ EnvParser.register :API_KEY, as: :string
70
+ API_KEY ## => 'unbreakable p4$$w0rd' (registered within the Kernel module, so it's available everywhere)
71
+
72
+ ## ... and class/module constants!
73
+ ##
74
+ ENV['ULTIMATE_LINK'] = 'https://youtu.be/L_jWHffIx5E'
75
+ EnvParser.register :ULTIMATE_LINK, as: :string, within: URI
76
+ URI::ULTIMATE_LINK ## => That sweet, sweet link we just set.
77
+ ULTIMATE_LINK ## => raises NameError (the un-namespaced constant is only in scope within the URI module)
63
78
  ```
64
79
 
65
80
  ---
@@ -87,9 +102,10 @@ Note JSON is parsed using *quirks-mode* (meaning 'true', '25', and 'null' are al
87
102
  ## Feature Roadmap / Future Development
88
103
 
89
104
  Additional features/options coming in the future:
90
- - An `EnvParser.load` method that will not only parse the given value, but will set a constant, easily converting environment variables into constants in your code.
91
- - An `EnvParser.load_all` method to shortcut multiple `.load` calls.
92
- - A means to **optionally** bind `#parse`, `#load`, and `#load_all` methods onto `ENV` itself (not all hashes!). Because `ENV.parse ...` reads better than `EnvParser.parse ...`.
105
+ - An `EnvParser.register_all` method to shortcut multiple `.register` calls.
106
+ - A means to **optionally** bind `#parse`, `#regiter`, and `#register_all` methods onto `ENV`. Because `ENV.parse ...` reads better than `EnvParser.parse ...`.
107
+ - A `validator` option that lets you pass in a validator lambda/block for things more complex than what a simple `from_set` can enforce.
108
+ - A means to register validation blocks as new "as" types. This will allow for custom "as" types like `:url`, `:email`, etc.
93
109
  - ... ?
94
110
 
95
111
 
@@ -126,7 +126,7 @@ option.</p>
126
126
  </div>
127
127
 
128
128
  <div id="footer">
129
- Generated on Wed Nov 29 18:34:06 2017 by
129
+ Generated on Thu Nov 30 22:44:16 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.3.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.4.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
 
@@ -169,6 +169,31 @@ different data types.</p>
169
169
  <p>Interprets the given value as the specified type.</p>
170
170
  </div></span>
171
171
 
172
+ </li>
173
+
174
+
175
+ <li class="public ">
176
+ <span class="summary_signature">
177
+
178
+ <a href="#register-class_method" title="register (class method)">.<strong>register</strong>(name, options = {}) &#x21d2; Object </a>
179
+
180
+
181
+
182
+ </span>
183
+
184
+
185
+
186
+
187
+
188
+
189
+
190
+
191
+
192
+ <span class="summary_desc"><div class='inline'>
193
+ <p>Parses the referenced value and creates a matching constant in the
194
+ requested context.</p>
195
+ </div></span>
196
+
172
197
  </li>
173
198
 
174
199
 
@@ -396,6 +421,224 @@ value will raise an ArgumentError exception.</p>
396
421
  </td>
397
422
  </tr>
398
423
  </table>
424
+ </div>
425
+
426
+ <div class="method_details ">
427
+ <h3 class="signature " id="register-class_method">
428
+
429
+ .<strong>register</strong>(name, options = {}) &#x21d2; <tt>Object</tt>
430
+
431
+
432
+
433
+
434
+
435
+ </h3><div class="docstring">
436
+ <div class="discussion">
437
+
438
+ <p>Parses the referenced value and creates a matching constant in the
439
+ requested context.</p>
440
+
441
+
442
+ </div>
443
+ </div>
444
+ <div class="tags">
445
+ <p class="tag_title">Parameters:</p>
446
+ <ul class="param">
447
+
448
+ <li>
449
+
450
+ <span class='name'>name</span>
451
+
452
+
453
+ <span class='type'></span>
454
+
455
+
456
+
457
+ &mdash;
458
+ <div class='inline'>
459
+ <p>The name of the value to parse/interpret from the “from” Hash. If the
460
+ “from” value is ENV, you may give a Symbol and the corresponding String key
461
+ will be used instead.</p>
462
+ </div>
463
+
464
+ </li>
465
+
466
+ <li>
467
+
468
+ <span class='name'>options</span>
469
+
470
+
471
+ <span class='type'>(<tt>Hash</tt>)</span>
472
+
473
+
474
+ <em class="default">(defaults to: <tt>{}</tt>)</em>
475
+
476
+
477
+ &mdash;
478
+ <div class='inline'>
479
+ <p>a customizable set of options</p>
480
+ </div>
481
+
482
+ </li>
483
+
484
+ </ul>
485
+
486
+
487
+
488
+
489
+
490
+
491
+ <p class="tag_title">Options Hash (<tt>options</tt>):</p>
492
+ <ul class="option">
493
+
494
+ <li>
495
+ <span class="name">from</span>
496
+ <span class="type">(<tt>Hash</tt>)</span>
497
+ <span class="default">
498
+
499
+ </span>
500
+
501
+ &mdash; <div class='inline'>
502
+ <p>The source Hash from which to pull the value referenced by the “name” key.
503
+ Defaults to ENV.</p>
504
+ </div>
505
+
506
+ </li>
507
+
508
+ <li>
509
+ <span class="name">within</span>
510
+ <span class="type">(<tt>Module</tt>, <tt>Class</tt>)</span>
511
+ <span class="default">
512
+
513
+ </span>
514
+
515
+ &mdash; <div class='inline'>
516
+ <p>The module or class in which the constant should be created. Defaults to
517
+ Kernel (making it a global constant).</p>
518
+ </div>
519
+
520
+ </li>
521
+
522
+ <li>
523
+ <span class="name">as</span>
524
+ <span class="type">(<tt>Symbol</tt>)</span>
525
+ <span class="default">
526
+
527
+ &mdash; default:
528
+ <tt>See `.parse`</tt>
529
+
530
+ </span>
531
+
532
+ </li>
533
+
534
+ <li>
535
+ <span class="name">if_unset</span>
536
+ <span class="type">(<tt>Object</tt>)</span>
537
+ <span class="default">
538
+
539
+ &mdash; default:
540
+ <tt>See `.parse`</tt>
541
+
542
+ </span>
543
+
544
+ </li>
545
+
546
+ <li>
547
+ <span class="name">from_set</span>
548
+ <span class="type">(<tt>Array</tt>, <tt>Range</tt>)</span>
549
+ <span class="default">
550
+
551
+ &mdash; default:
552
+ <tt>See `.parse`</tt>
553
+
554
+ </span>
555
+
556
+ </li>
557
+
558
+ </ul>
559
+
560
+
561
+ <p class="tag_title">Raises:</p>
562
+ <ul class="raise">
563
+
564
+ <li>
565
+
566
+
567
+ <span class='type'>(<tt>ArgumentError</tt>)</span>
568
+
569
+
570
+
571
+ </li>
572
+
573
+ </ul>
574
+
575
+ </div><table class="source_code">
576
+ <tr>
577
+ <td>
578
+ <pre class="lines">
579
+
580
+
581
+ 101
582
+ 102
583
+ 103
584
+ 104
585
+ 105
586
+ 106
587
+ 107
588
+ 108
589
+ 109
590
+ 110
591
+ 111
592
+ 112
593
+ 113
594
+ 114
595
+ 115
596
+ 116
597
+ 117
598
+ 118
599
+ 119
600
+ 120
601
+ 121
602
+ 122
603
+ 123
604
+ 124
605
+ 125
606
+ 126
607
+ 127</pre>
608
+ </td>
609
+ <td>
610
+ <pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 101</span>
611
+
612
+ <span class='kw'>def</span> <span class='id identifier rubyid_register'>register</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>
613
+ <span class='id identifier rubyid_from'>from</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:from</span><span class='comma'>,</span> <span class='const'>ENV</span><span class='rparen'>)</span>
614
+ <span class='id identifier rubyid_within'>within</span> <span class='op'>=</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='symbol'>:within</span><span class='comma'>,</span> <span class='const'>Kernel</span><span class='rparen'>)</span>
615
+
616
+ <span class='comment'>## ENV *seems* like a Hash and it does *some* Hash-y things, but it is NOT a Hash and that can
617
+ </span> <span class='comment'>## bite you in some cases. Making sure we&#39;re working with a straight-up Hash saves a lot of
618
+ </span> <span class='comment'>## sanity checks later on. This is also a good place to make sure we&#39;re working with a String
619
+ </span> <span class='comment'>## key.
620
+ </span> <span class='kw'>if</span> <span class='id identifier rubyid_from'>from</span> <span class='op'>==</span> <span class='const'>ENV</span>
621
+ <span class='id identifier rubyid_from'>from</span> <span class='op'>=</span> <span class='id identifier rubyid_from'>from</span><span class='period'>.</span><span class='id identifier rubyid_to_h'>to_h</span>
622
+ <span class='id identifier rubyid_name'>name</span> <span class='op'>=</span> <span class='id identifier rubyid_name'>name</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span>
623
+ <span class='kw'>end</span>
624
+
625
+ <span class='kw'>unless</span> <span class='id identifier rubyid_from'>from</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Hash</span><span class='rparen'>)</span>
626
+ <span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>invalid `from` parameter: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_from'>from</span><span class='period'>.</span><span class='id identifier rubyid_class'>class</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span>
627
+ <span class='kw'>end</span>
628
+
629
+ <span class='kw'>unless</span> <span class='id identifier rubyid_within'>within</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Module</span><span class='rparen'>)</span> <span class='op'>||</span> <span class='id identifier rubyid_within'>within</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span><span class='lparen'>(</span><span class='const'>Class</span><span class='rparen'>)</span>
630
+ <span class='id identifier rubyid_raise'>raise</span> <span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>invalid `within` parameter: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_within'>within</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>&quot;</span></span>
631
+ <span class='kw'>end</span>
632
+
633
+ <span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='id identifier rubyid_from'>from</span><span class='lbracket'>[</span><span class='id identifier rubyid_name'>name</span><span class='rbracket'>]</span>
634
+ <span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='id identifier rubyid_parse'>parse</span><span class='lparen'>(</span><span class='id identifier rubyid_value'>value</span><span class='comma'>,</span> <span class='id identifier rubyid_options'>options</span><span class='rparen'>)</span>
635
+ <span class='id identifier rubyid_within'>within</span><span class='period'>.</span><span class='id identifier rubyid_const_set'>const_set</span><span class='lparen'>(</span><span class='id identifier rubyid_name'>name</span><span class='period'>.</span><span class='id identifier rubyid_upcase'>upcase</span><span class='period'>.</span><span class='id identifier rubyid_to_sym'>to_sym</span><span class='comma'>,</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_dup'>dup</span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span><span class='rparen'>)</span>
636
+
637
+ <span class='id identifier rubyid_value'>value</span>
638
+ <span class='kw'>end</span></pre>
639
+ </td>
640
+ </tr>
641
+ </table>
399
642
  </div>
400
643
 
401
644
  </div>
@@ -403,7 +646,7 @@ value will raise an ArgumentError exception.</p>
403
646
  </div>
404
647
 
405
648
  <div id="footer">
406
- Generated on Wed Nov 29 18:34:06 2017 by
649
+ Generated on Thu Nov 30 22:44:16 2017 by
407
650
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
408
651
  0.9.11 (ruby-2.4.2).
409
652
  </div>
data/docs/_index.html CHANGED
@@ -112,7 +112,7 @@
112
112
  </div>
113
113
 
114
114
  <div id="footer">
115
- Generated on Wed Nov 29 18:34:06 2017 by
115
+ Generated on Thu Nov 30 22:44:16 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>
@@ -125,11 +125,11 @@ EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset: 250 ## =&gt;
125
125
  <h2 id="label-Note+that+-22if_unset-22+values+are+used+as-is-2C+with+no+type+conversion.">Note that “if_unset” values are used as-is, with no type conversion.</h2>
126
126
 
127
127
  <p>## EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset:
128
- &#39;oof!&#39; ## =&gt; &#39;oof!&#39;</p>
128
+ &#39;Whoops!&#39; ## =&gt; &#39;Whoops!&#39;</p>
129
129
 
130
- <h2 id="label-You+can+also+restrict+the+set+of+allowed+values.">You can also restrict the set of allowed values.</h2>
130
+ <h2 id="label-Sometimes+setting+the+type+alone+is+a+bit+too+open-ended.">Sometimes setting the type alone is a bit too open-ended.</h2>
131
131
 
132
- <h2 id="label-28Sometimes+setting+the+type+alone+is+a+bit+too+open-ended.-29">(Sometimes setting the type alone is a bit too open-ended.)</h2>
132
+ <h2 id="label-The+-22from_set-22+option+lets+you+restrict+the+set+of+allowed+values.">The “from_set” option lets you restrict the set of allowed values.</h2>
133
133
 
134
134
  <p>## EnvParser.parse :API_TO_USE, as: :symbol, from_set: %i[internal
135
135
  external] EnvParser.parse :SOME_CUSTOM_NETWORK_PORT, as: :integer,
@@ -137,8 +137,26 @@ from_set: (1..65535), if_unset: 80</p>
137
137
 
138
138
  <h2 id="label-And+if+the+value+is+not+allowed...">And if the value is not allowed…</h2>
139
139
 
140
- <p>## EnvParser.parse :SOME_NEGATIVE_NUMBER, as: :integer, from_set: (1..5) ##
141
- =&gt; raises EnvParser::ValueNotAllowed “`</p>
140
+ <p>## EnvParser.parse :NEGATIVE_NUMBER, as: :integer, from_set: (1..5) ##
141
+ =&gt; raises EnvParser::ValueNotAllowed</p>
142
+
143
+ <h2 id="label-You+can+also+SET+CONSTANTS+DIRECTLY+FROM+YOUR+ENV+VARIABLES.">You can also SET CONSTANTS DIRECTLY FROM YOUR ENV VARIABLES.</h2>
144
+
145
+ <h2 id="label-Global+constants...">Global constants…</h2>
146
+
147
+ <p>## <a href="'API_KEY'">ENV</a> = &#39;unbreakable p4$$w0rd&#39;
148
+ EnvParser.register :API_KEY, as: :string API_KEY ## =&gt; &#39;unbreakable
149
+ p4$$w0rd&#39; (registered within the Kernel module, so it&#39;s available
150
+ everywhere)</p>
151
+
152
+ <h2 id="label-...+and+class-2Fmodule+constants-21">… and class/module constants!</h2>
153
+
154
+ <p>## <a href="'ULTIMATE_LINK'">ENV</a> = &#39;<a
155
+ href="https://youtu.be/L_jWHffIx5E">youtu.be/L_jWHffIx5E</a>&#39;
156
+ EnvParser.register :ULTIMATE_LINK, as: :string, within: URI
157
+ URI::ULTIMATE_LINK ## =&gt; That sweet, sweet link we just set.
158
+ ULTIMATE_LINK ## =&gt; raises NameError (the un-namespaced constant is only
159
+ in scope within the URI module) “`</p>
142
160
  <hr>
143
161
 
144
162
  <p>The named <code>:as</code> value is required. Allowed values are:</p>
@@ -160,14 +178,15 @@ the repo docs</a> for the full EnvParser documentation.</p>
160
178
  <h2 id="label-Feature+Roadmap+-2F+Future+Development">Feature Roadmap / Future Development</h2>
161
179
 
162
180
  <p>Additional features/options coming in the future: - An
163
- <code>EnvParser.load</code> method that will not only parse the given
164
- value, but will set a constant, easily converting environment variables
165
- into constants in your code. - An <code>EnvParser.load_all</code> method to
166
- shortcut multiple <code>.load</code> calls. - A means to
167
- <strong>optionally</strong> bind <code>#parse</code>, <code>#load</code>,
168
- and <code>#load_all</code> methods onto <code>ENV</code> itself (not all
169
- hashes!). Because <code>ENV.parse ...</code> reads better than
170
- <code>EnvParser.parse ...</code>. - ?</p>
181
+ <code>EnvParser.register_all</code> method to shortcut multiple
182
+ <code>.register</code> calls. - A means to <strong>optionally</strong> bind
183
+ <code>#parse</code>, <code>#regiter</code>, and <code>#register_all</code>
184
+ methods onto <code>ENV</code>. Because <code>ENV.parse ...</code> reads
185
+ better than <code>EnvParser.parse ...</code>. - A <code>validator</code>
186
+ option that lets you pass in a validator lambda/block for things more
187
+ complex than what a simple <code>from_set</code> can enforce. - A means to
188
+ register validation blocks as new “as” types. This will allow for custom
189
+ “as” types like <code>:url</code>, <code>:email</code>, etc. - … ?</p>
171
190
 
172
191
  <h2 id="label-Contribution+-2F+Development">Contribution / Development</h2>
173
192
 
@@ -198,7 +217,7 @@ href="https://opensource.org/licenses/MIT">MIT License</a>.</p>
198
217
  </div></div>
199
218
 
200
219
  <div id="footer">
201
- Generated on Wed Nov 29 18:34:06 2017 by
220
+ Generated on Thu Nov 30 22:44:16 2017 by
202
221
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
203
222
  0.9.11 (ruby-2.4.2).
204
223
  </div>
data/docs/index.html CHANGED
@@ -125,11 +125,11 @@ EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset: 250 ## =&gt;
125
125
  <h2 id="label-Note+that+-22if_unset-22+values+are+used+as-is-2C+with+no+type+conversion.">Note that “if_unset” values are used as-is, with no type conversion.</h2>
126
126
 
127
127
  <p>## EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset:
128
- &#39;oof!&#39; ## =&gt; &#39;oof!&#39;</p>
128
+ &#39;Whoops!&#39; ## =&gt; &#39;Whoops!&#39;</p>
129
129
 
130
- <h2 id="label-You+can+also+restrict+the+set+of+allowed+values.">You can also restrict the set of allowed values.</h2>
130
+ <h2 id="label-Sometimes+setting+the+type+alone+is+a+bit+too+open-ended.">Sometimes setting the type alone is a bit too open-ended.</h2>
131
131
 
132
- <h2 id="label-28Sometimes+setting+the+type+alone+is+a+bit+too+open-ended.-29">(Sometimes setting the type alone is a bit too open-ended.)</h2>
132
+ <h2 id="label-The+-22from_set-22+option+lets+you+restrict+the+set+of+allowed+values.">The “from_set” option lets you restrict the set of allowed values.</h2>
133
133
 
134
134
  <p>## EnvParser.parse :API_TO_USE, as: :symbol, from_set: %i[internal
135
135
  external] EnvParser.parse :SOME_CUSTOM_NETWORK_PORT, as: :integer,
@@ -137,8 +137,26 @@ from_set: (1..65535), if_unset: 80</p>
137
137
 
138
138
  <h2 id="label-And+if+the+value+is+not+allowed...">And if the value is not allowed…</h2>
139
139
 
140
- <p>## EnvParser.parse :SOME_NEGATIVE_NUMBER, as: :integer, from_set: (1..5) ##
141
- =&gt; raises EnvParser::ValueNotAllowed “`</p>
140
+ <p>## EnvParser.parse :NEGATIVE_NUMBER, as: :integer, from_set: (1..5) ##
141
+ =&gt; raises EnvParser::ValueNotAllowed</p>
142
+
143
+ <h2 id="label-You+can+also+SET+CONSTANTS+DIRECTLY+FROM+YOUR+ENV+VARIABLES.">You can also SET CONSTANTS DIRECTLY FROM YOUR ENV VARIABLES.</h2>
144
+
145
+ <h2 id="label-Global+constants...">Global constants…</h2>
146
+
147
+ <p>## <a href="'API_KEY'">ENV</a> = &#39;unbreakable p4$$w0rd&#39;
148
+ EnvParser.register :API_KEY, as: :string API_KEY ## =&gt; &#39;unbreakable
149
+ p4$$w0rd&#39; (registered within the Kernel module, so it&#39;s available
150
+ everywhere)</p>
151
+
152
+ <h2 id="label-...+and+class-2Fmodule+constants-21">… and class/module constants!</h2>
153
+
154
+ <p>## <a href="'ULTIMATE_LINK'">ENV</a> = &#39;<a
155
+ href="https://youtu.be/L_jWHffIx5E">youtu.be/L_jWHffIx5E</a>&#39;
156
+ EnvParser.register :ULTIMATE_LINK, as: :string, within: URI
157
+ URI::ULTIMATE_LINK ## =&gt; That sweet, sweet link we just set.
158
+ ULTIMATE_LINK ## =&gt; raises NameError (the un-namespaced constant is only
159
+ in scope within the URI module) “`</p>
142
160
  <hr>
143
161
 
144
162
  <p>The named <code>:as</code> value is required. Allowed values are:</p>
@@ -160,14 +178,15 @@ the repo docs</a> for the full EnvParser documentation.</p>
160
178
  <h2 id="label-Feature+Roadmap+-2F+Future+Development">Feature Roadmap / Future Development</h2>
161
179
 
162
180
  <p>Additional features/options coming in the future: - An
163
- <code>EnvParser.load</code> method that will not only parse the given
164
- value, but will set a constant, easily converting environment variables
165
- into constants in your code. - An <code>EnvParser.load_all</code> method to
166
- shortcut multiple <code>.load</code> calls. - A means to
167
- <strong>optionally</strong> bind <code>#parse</code>, <code>#load</code>,
168
- and <code>#load_all</code> methods onto <code>ENV</code> itself (not all
169
- hashes!). Because <code>ENV.parse ...</code> reads better than
170
- <code>EnvParser.parse ...</code>. - ?</p>
181
+ <code>EnvParser.register_all</code> method to shortcut multiple
182
+ <code>.register</code> calls. - A means to <strong>optionally</strong> bind
183
+ <code>#parse</code>, <code>#regiter</code>, and <code>#register_all</code>
184
+ methods onto <code>ENV</code>. Because <code>ENV.parse ...</code> reads
185
+ better than <code>EnvParser.parse ...</code>. - A <code>validator</code>
186
+ option that lets you pass in a validator lambda/block for things more
187
+ complex than what a simple <code>from_set</code> can enforce. - A means to
188
+ register validation blocks as new “as” types. This will allow for custom
189
+ “as” types like <code>:url</code>, <code>:email</code>, etc. - … ?</p>
171
190
 
172
191
  <h2 id="label-Contribution+-2F+Development">Contribution / Development</h2>
173
192
 
@@ -198,7 +217,7 @@ href="https://opensource.org/licenses/MIT">MIT License</a>.</p>
198
217
  </div></div>
199
218
 
200
219
  <div id="footer">
201
- Generated on Wed Nov 29 18:34:06 2017 by
220
+ Generated on Thu Nov 30 22:44:16 2017 by
202
221
  <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
203
222
  0.9.11 (ruby-2.4.2).
204
223
  </div>
@@ -52,6 +52,14 @@
52
52
  </li>
53
53
 
54
54
 
55
+ <li class="even ">
56
+ <div class="item">
57
+ <span class='object_link'><a href="EnvParser.html#register-class_method" title="EnvParser.register (method)">register</a></span>
58
+ <small>EnvParser</small>
59
+ </div>
60
+ </li>
61
+
62
+
55
63
 
56
64
  </ul>
57
65
  </div>
@@ -100,7 +100,7 @@
100
100
  </div>
101
101
 
102
102
  <div id="footer">
103
- Generated on Wed Nov 29 18:34:06 2017 by
103
+ Generated on Thu Nov 30 22:44:16 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.3.0'.freeze
2
+ VERSION = '0.4.0'.freeze
3
3
  end
data/lib/env_parser.rb CHANGED
@@ -74,6 +74,58 @@ class EnvParser
74
74
  value
75
75
  end
76
76
 
77
+ ## Parses the referenced value and creates a matching constant in the requested context.
78
+ ##
79
+ ## @param name
80
+ ## The name of the value to parse/interpret from the "from" Hash. If the "from" value is ENV,
81
+ ## you may give a Symbol and the corresponding String key will be used instead.
82
+ ##
83
+ ## @option options from [Hash]
84
+ ## The source Hash from which to pull the value referenced by the "name" key. Defaults to ENV.
85
+ ##
86
+ ## @option options within [Module, Class]
87
+ ## The module or class in which the constant should be created. Defaults to Kernel (making it
88
+ ## a global constant).
89
+ ##
90
+ ## @option options as [Symbol]
91
+ ## (See `.parse`)
92
+ ##
93
+ ## @option options if_unset
94
+ ## (See `.parse`)
95
+ ##
96
+ ## @option options from_set [Array, Range]
97
+ ## (See `.parse`)
98
+ ##
99
+ ## @raise [ArgumentError]
100
+ ##
101
+ def register(name, options = {})
102
+ from = options.fetch(:from, ENV)
103
+ within = options.fetch(:within, Kernel)
104
+
105
+ ## ENV *seems* like a Hash and it does *some* Hash-y things, but it is NOT a Hash and that can
106
+ ## bite you in some cases. Making sure we're working with a straight-up Hash saves a lot of
107
+ ## sanity checks later on. This is also a good place to make sure we're working with a String
108
+ ## key.
109
+ if from == ENV
110
+ from = from.to_h
111
+ name = name.to_s
112
+ end
113
+
114
+ unless from.is_a?(Hash)
115
+ raise ArgumentError, "invalid `from` parameter: #{from.class}"
116
+ end
117
+
118
+ unless within.is_a?(Module) || within.is_a?(Class)
119
+ raise ArgumentError, "invalid `within` parameter: #{within.inspect}"
120
+ end
121
+
122
+ value = from[name]
123
+ value = parse(value, options)
124
+ within.const_set(name.upcase.to_sym, value.dup.freeze)
125
+
126
+ value
127
+ end
128
+
77
129
  private
78
130
 
79
131
  def parse_string(value)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nestor Custodio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-30 00:00:00.000000000 Z
11
+ date: 2017-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler