env_parser 1.7.1 → 1.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +6 -4
  3. data/Gemfile.lock +34 -3
  4. data/env_parser.gemspec +19 -12
  5. data/lib/env_parser/types/chronology_types.rb +1 -1
  6. data/lib/env_parser/types/internet_types.rb +1 -0
  7. data/lib/env_parser/version.rb +1 -1
  8. data/lib/env_parser.rb +17 -15
  9. metadata +4 -113
  10. data/.circleci/config.yml +0 -64
  11. data/.gitignore +0 -14
  12. data/.rspec +0 -3
  13. data/.rubocop.yml +0 -64
  14. data/.ruby-version +0 -1
  15. data/.yardopts +0 -1
  16. data/bin/console +0 -14
  17. data/bin/setup +0 -8
  18. data/docs/EnvParser/AutoregisterFileNotFound.html +0 -146
  19. data/docs/EnvParser/Error.html +0 -140
  20. data/docs/EnvParser/TypeAlreadyDefined.html +0 -143
  21. data/docs/EnvParser/TypeAlreadyDefinedError.html +0 -145
  22. data/docs/EnvParser/Types/BaseTypes.html +0 -179
  23. data/docs/EnvParser/Types/ChronologyTypes.html +0 -161
  24. data/docs/EnvParser/Types/InternetTypes.html +0 -173
  25. data/docs/EnvParser/Types.html +0 -130
  26. data/docs/EnvParser/UnknownTypeError.html +0 -145
  27. data/docs/EnvParser/UnparseableAutoregisterSpec.html +0 -146
  28. data/docs/EnvParser/ValueNotAllowed.html +0 -144
  29. data/docs/EnvParser/ValueNotAllowedError.html +0 -147
  30. data/docs/EnvParser/ValueNotConvertibleError.html +0 -145
  31. data/docs/EnvParser.html +0 -1194
  32. data/docs/EnvParserTypes/BaseTypes.html +0 -178
  33. data/docs/EnvParserTypes/ChronologyTypes.html +0 -159
  34. data/docs/EnvParserTypes/InternetTypes.html +0 -159
  35. data/docs/EnvParserTypes/TimeTypes.html +0 -158
  36. data/docs/EnvParserTypes.html +0 -129
  37. data/docs/_index.html +0 -245
  38. data/docs/class_list.html +0 -54
  39. data/docs/css/common.css +0 -1
  40. data/docs/css/full_list.css +0 -206
  41. data/docs/css/style.css +0 -1089
  42. data/docs/file.README.html +0 -334
  43. data/docs/file_list.html +0 -59
  44. data/docs/frames.html +0 -22
  45. data/docs/index.html +0 -334
  46. data/docs/js/app.js +0 -801
  47. data/docs/js/full_list.js +0 -334
  48. data/docs/js/jquery.js +0 -4
  49. data/docs/method_list.html +0 -94
  50. data/docs/top-level-namespace.html +0 -112
data/docs/index.html DELETED
@@ -1,334 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>
7
- File: README
8
-
9
- &mdash; Documentation by YARD 0.9.44
10
-
11
- </title>
12
-
13
- <link rel="stylesheet" href="css/style.css" type="text/css">
14
-
15
- <link rel="stylesheet" href="css/common.css" type="text/css">
16
-
17
- <script type="text/javascript">
18
- pathId = "README";
19
- relpath = '';
20
- </script>
21
-
22
-
23
- <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
-
25
- <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
-
27
-
28
- </head>
29
- <body>
30
- <div id="main_progress" aria-hidden="true"></div>
31
-
32
- <div class="nav_wrap">
33
- <iframe id="nav" src="class_list.html?1"></iframe>
34
- <div id="resizer"></div>
35
- </div>
36
-
37
- <div id="main" tabindex="-1">
38
- <div id="header">
39
- <div id="menu">
40
-
41
- <a href="_index.html">Index</a> &raquo;
42
- <span class="title">File: README</span>
43
-
44
- </div>
45
-
46
- <div id="search">
47
-
48
- <a class="full_list_link" id="class_list_link"
49
- href="class_list.html">
50
-
51
- <svg width="24" height="24">
52
- <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
- <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
- <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
- </svg>
56
- </a>
57
-
58
- </div>
59
- <div class="clear"></div>
60
- </div>
61
-
62
- <div id="content"><div id='filecontents'><p><a href="https://rubygems.org/gems/env_parser"><img src="https://img.shields.io/github/v/release/nestor-custodio/env_parser?color=green&amp;label=gem%20version" alt="Gem Version" /></a>
63
- <a href="https://tldrlegal.com/license/mit-license"><img src="https://img.shields.io/github/license/nestor-custodio/env_parser" alt="MIT License" /></a></p>
64
- <h1 id="EnvParser">EnvParser</h1>
65
- <p>If your code uses environment variables, you know that <code>ENV</code> will always surface these as strings. Interpreting these strings as the value you <em>actually</em> want to see/use takes some work, however: for numbers you need to cast with <code>to_i</code> or <code>to_f</code> ... for booleans you need to check for a specific value (<code>ENV['SOME_VAR'] == 'true'</code>) ... maybe you want to set non-trivial defaults (something other than <code>0</code> or <code>''</code>)? ... maybe you only want to allow values from a limited set? ...</p>
66
- <p>Things can get out of control pretty fast, especially as the number of environment variables in play grows. Tools like <a href="https://github.com/bkeepers/dotenv">dotenv</a> help to make sure you're loading the correct <strong>set</strong> of variables, but <a href="https://github.com/nestor-custodio/env_parser">EnvParser</a> makes <strong>the values themselves</strong> usable with a minimum of effort.</p>
67
- <p><a href="https://nestor-custodio.github.io/env_parser/EnvParser.html">Full documentation is available here</a>, but do read below for a crash course on availble featues!</p>
68
- <h2 id="Installation">Installation</h2>
69
- <ul>
70
- <li>
71
- <p>If your project uses <a href="https://github.com/bundler/bundler">Bundler</a>:</p>
72
- <ul>
73
- <li>Add one of the following to your application's Gemfile:
74
- <pre class="code ruby"><code class="ruby"><span class='comment'># For on-demand usage ...
75
- </span><span class='comment'>#
76
- </span><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>
77
-
78
- <span class='comment'># To automatically register ENV
79
- </span><span class='comment'># constants per &quot;.env_parser.yml&quot; ...
80
- </span><span class='comment'>#
81
- </span><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><span class='comma'>,</span> <span class='label'>require:</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>env_parser/autoregister</span><span class='tstring_end'>&#39;</span></span>
82
- </code></pre>
83
- </li>
84
- <li>And then run a:
85
- <pre class="code shell"><code class="shell">$ bundle install
86
- </code></pre>
87
- </li>
88
- </ul>
89
- </li>
90
- <li>
91
- <p>Or, you can keep things simple with a manual install:</p>
92
- <pre class="code shell"><code class="shell">$ gem install env_parser
93
- </code></pre>
94
- </li>
95
- </ul>
96
- <h2 id="Syntax_Cheat_Sheet">Syntax Cheat Sheet</h2>
97
- <pre class="code ruby"><code class="ruby"><span class='comment'># Returns an ENV value parsed &quot;as&quot; a specific type:
98
- </span><span class='comment'>#
99
- </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='id identifier rubyid_env_key_as_a_symbol'>env_key_as_a_symbol</span><span class='comma'>,</span>
100
- <span class='label'>as:</span> <span class='id identifier rubyid_…'>…</span><span class='comma'>,</span> <span class='comment'># ➜ required; Symbol
101
- </span> <span class='label'>if_unset:</span> <span class='id identifier rubyid_…'>…</span><span class='comma'>,</span> <span class='comment'># ➜ optional; default value (of any type)
102
- </span> <span class='label'>from_set:</span> <span class='id identifier rubyid_…'>…</span><span class='comma'>,</span> <span class='comment'># ➜ optional; Array or Range
103
- </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_…'>…</span> <span class='rbrace'>}</span> <span class='comment'># ➜ optional; may also be given as a block
104
- </span>
105
- <span class='comment'># Parse an ENV value and register it as a constant:
106
- </span><span class='comment'>#
107
- </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='id identifier rubyid_env_key_as_a_symbol'>env_key_as_a_symbol</span><span class='comma'>,</span>
108
- <span class='label'>as:</span> <span class='id identifier rubyid_…'>…</span><span class='comma'>,</span> <span class='comment'># ➜ required; Symbol
109
- </span> <span class='label'>named:</span> <span class='id identifier rubyid_…'>…</span><span class='comma'>,</span> <span class='comment'># ➜ optional; String or Symbol; available only if `within` is also given
110
- </span> <span class='label'>within:</span> <span class='id identifier rubyid_…'>…</span><span class='comma'>,</span> <span class='comment'># ➜ optional; Class or Module
111
- </span> <span class='label'>if_unset:</span> <span class='id identifier rubyid_…'>…</span><span class='comma'>,</span> <span class='comment'># ➜ optional; default value (of any type)
112
- </span> <span class='label'>from_set:</span> <span class='id identifier rubyid_…'>…</span><span class='comma'>,</span> <span class='comment'># ➜ optional; Array or Range
113
- </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_…'>…</span> <span class='rbrace'>}</span> <span class='comment'># ➜ optional; may also be given as a block
114
- </span>
115
- <span class='comment'># Registers all ENV variables as spec&#39;ed in &quot;.env_parser.yml&quot;:
116
- </span><span class='comment'>#
117
- </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_autoregister'><span class='object_link'><a href="EnvParser.html#autoregister-class_method" title="EnvParser.autoregister (method)">autoregister</a></span></span> <span class='comment'># Note this is automatically called if your
118
- </span> <span class='comment'># Gemfile included the &quot;env_parser&quot; gem with
119
- </span> <span class='comment'># the &quot;require: &#39;env_parser/autoregister&#39;&quot; option.
120
- </span>
121
- <span class='comment'># Lets you call &quot;parse&quot; and &quot;register&quot; on ENV itself:
122
- </span><span class='comment'>#
123
- </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'># ENV.parse will now be a proxy for EnvParser.parse
124
- </span> <span class='comment'># and ENV.register will now be a proxy for EnvParser.register
125
- </span></code></pre>
126
- <h2 id="Extended_How_To_Use">Extended How-To-Use</h2>
127
- <h4 id="Basic_Usage">Basic Usage</h4>
128
- <ul>
129
- <li>
130
- <p><strong>Parsing <code>ENV</code> Values</strong></p>
131
- <p>At its core, EnvParser is a straight-forward parser for string values (since that's all <code>ENV</code> ever gives you), allowing you to read a given string <strong><em>as</em></strong> a variety of types.</p>
132
- <pre class="code ruby"><code class="ruby"><span class='comment'># Returns ENV[&#39;TIMEOUT_MS&#39;] as an Integer,
133
- </span><span class='comment'># or a sensible default (0) if ENV[&#39;TIMEOUT_MS&#39;] is unset.
134
- </span><span class='comment'>#
135
- </span><span class='id identifier rubyid_timeout_ms'>timeout_ms</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_parse'><span class='object_link'><a href="EnvParser.html#parse-class_method" title="EnvParser.parse (method)">parse</a></span></span> <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>TIMEOUT_MS</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span>
136
- </code></pre>
137
- <p>You can check the full documentation for <a href="https://nestor-custodio.github.io/env_parser/EnvParser/Types.html">a list of all <strong><em>as</em></strong> types available right out of the box</a>.</p>
138
- </li>
139
- <li>
140
- <p><strong>How About Less Typing?</strong></p>
141
- <p>EnvParser is all about <del>simplification</del> <del>less typing</del> <em>laziness</em>. If you pass in a symbol instead of a string, EnvParser will look to <code>ENV</code> and use the value from the corresponding (string) key.</p>
142
- <pre class="code ruby"><code class="ruby"><span class='comment'># YAY, LESS TYPING! 😃
143
- </span><span class='comment'># These two are the same:
144
- </span><span class='comment'>#
145
- </span><span class='id identifier rubyid_more_typing'>more_typing</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>
146
- <span class='id identifier rubyid_less_typing'>less_typing</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>
147
- </code></pre>
148
- </li>
149
- <li>
150
- <p><strong>Registering Constants From <code>ENV</code> Values</strong></p>
151
- <p>The <code>EnvParser.register</code> method lets you &quot;promote&quot; <code>ENV</code> variables into their own constants, already parsed into the correct type.</p>
152
- <pre class="code ruby"><code class="ruby"><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;
153
- </span>
154
- <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>
155
- <span class='const'>API_KEY</span> <span class='comment'># =&gt; &#39;unbreakable p4$$w0rd&#39;
156
- </span></code></pre>
157
- <p>By default, <code>EnvParser.register</code> will create the requested constant within the Kernel module (making it available everywhere), but you can specify any class or module you like.</p>
158
- <pre class="code ruby"><code class="ruby"><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>BEST_VIDEO</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='comment'># =&gt; &#39;https://youtu.be/L_jWHffIx5E&#39;
159
- </span>
160
- <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'>:BEST_VIDEO</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>
161
- <span class='const'>URI</span><span class='op'>::</span><span class='const'>BEST_VIDEO</span> <span class='comment'># =&gt; &#39;https://youtu.be/L_jWHffIx5E&#39;
162
- </span><span class='const'>BEST_VIDEO</span> <span class='comment'># =&gt; raises NameError
163
- </span></code></pre>
164
- <p><code>EnvParser.register</code>'s <strong><em>within</em></strong> option also allows for specifying what you would like the registered constant to be <strong><em>named</em></strong>, since related ENV variables will tend to have redundant names once namespaced within a single class or module. Note that <code>named</code> is only available when used alongside <code>within</code>, as it exists solely as a namespacing aid; registering ENV variables as <em>global</em> constants with different names would be a debugging nightmare.</p>
165
- <pre class="code ruby"><code class="ruby"><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>CUSTOM_CLIENT_DEFAULT_HOSTNAME</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='comment'># =&gt; &#39;localhost&#39;
166
- </span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>CUSTOM_CLIENT_DEFAULT_PORT</span><span class='tstring_end'>&#39;</span></span> <span class='rbracket'>]</span> <span class='comment'># =&gt; &#39;3000&#39;
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='symbol'>:CUSTOM_CLIENT_DEFAULT_HOSTNAME</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:string</span> <span class='comma'>,</span> <span class='label'>named:</span> <span class='symbol'>:DEFAULT_HOSTNAME</span><span class='comma'>,</span> <span class='label'>within:</span> <span class='const'>CustomClient</span>
169
- <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'>:CUSTOM_CLIENT_DEFAULT_PORT</span> <span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span><span class='comma'>,</span> <span class='label'>named:</span> <span class='symbol'>:DEFAULT_PORT</span> <span class='comma'>,</span> <span class='label'>within:</span> <span class='const'>CustomClient</span>
170
- <span class='const'>CustomClient</span><span class='op'>::</span><span class='const'>DEFAULT_HOSTNAME</span> <span class='comment'># =&gt; &#39;localhost&#39;
171
- </span><span class='const'>CustomClient</span><span class='op'>::</span><span class='const'>DEFAULT_PORT</span> <span class='comment'># =&gt; 3000
172
- </span></code></pre>
173
- <p>You can also register multiple constants with a single call, which is a bit cleaner.</p>
174
- <pre class="code ruby"><code class="ruby">EnvParser.register :USERNAME, as: :string
175
- EnvParser.register :PASSWORD, as: :string
176
- EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }
177
-
178
- # ... is equivalent to ... #
179
-
180
- EnvParser.register USERNAME: { as: :string },
181
- PASSWORD: { as: :string },
182
- MOCK_API: { as: :boolean, within: MyClassOrModule }
183
- </code></pre>
184
- </li>
185
- <li>
186
- <p><strong>Okay, But... How About Even Less Typing?</strong></p>
187
- <p>Calling <code>EnvParser.add_env_bindings</code> binds proxy <code>parse</code> and <code>register</code> methods onto <code>ENV</code>. With these bindings in place, you can call <code>parse</code> or <code>register</code> on <code>ENV</code> itself, which is more legible and feels more straight-forward.</p>
188
- <pre class="code ruby"><code class="ruby"><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;
189
- </span><span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>BETTER_PI</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='comment'># =&gt; &#39;[&quot;flaky crust&quot;, &quot;strawberry filling&quot;]&#39;
190
- </span>
191
- <span class='comment'># Bind the proxy methods.
192
- </span><span class='comment'>#
193
- </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>
194
-
195
- <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
196
- </span><span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_register'>register</span> <span class='symbol'>:BETTER_PI</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:array</span> <span class='comment'># Your constant is set!
197
- </span></code></pre>
198
- <p>Note that the proxy <code>ENV.parse</code> method will (naturally) <em>always</em> interpret the value given as an <code>ENV</code> key (converting it to a string, if necessary), which is slightly different from the original <code>EnvParser.parse</code> method.</p>
199
- <pre class="code ruby"><code class="ruby"><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;
200
- </span>
201
- <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='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>SHORT_PI</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:float</span> <span class='comment'># =&gt; &#39;SHORT_PI&#39; as a float: 0.0
202
- </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'>:SHORT_PI</span> <span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:float</span> <span class='comment'># =&gt; ENV[&#39;SHORT_PI&#39;] as a float: 3.1415926
203
- </span>
204
- <span class='comment'># Bind the proxy methods.
205
- </span><span class='comment'>#
206
- </span><span class='const'><span class='object_link'><a href="EnvParser.html" title="EnvParser (class)">EnvParser</a></span></span><span class='period'>.</span><span class='id identifier rubyid_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>
207
-
208
- <span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</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='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:float</span> <span class='comment'># =&gt; ENV[&#39;SHORT_PI&#39;] as a float: 3.1415926
209
- </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; ENV[&#39;SHORT_PI&#39;] as a float: 3.1415926
210
- </span></code></pre>
211
- <p>Note also that the <code>ENV.parse</code> and <code>ENV.register</code> binding is done safely and without polluting the method space for other objects.</p>
212
- <p><strong>All additional examples below will assume that <code>ENV</code> bindings are already in place, for brevity's sake.</strong></p>
213
- </li>
214
- </ul>
215
- <h4 id="Ensuring_Usable_Values">Ensuring Usable Values</h4>
216
- <ul>
217
- <li>
218
- <p><strong>Sensible Defaults</strong></p>
219
- <p>If the <code>ENV</code> variable you want is unset (<code>nil</code>) or blank (<code>''</code>), the return value is a sensible default for the given <strong><em>as</em></strong> type: 0 or 0.0 for numbers, an empty string/array/hash, etc. Sometimes you want a non-trivial default, however. The <strong><em>if_unset</em></strong> option lets you specify a default that better meets your needs.</p>
220
- <pre class="code ruby"><code class="ruby"><span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span> <span class='symbol'>:MISSING_VAR</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:integer</span> <span class='comment'># =&gt; 0
221
- </span><span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span> <span class='symbol'>:MISSING_VAR</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
222
- </span></code></pre>
223
- <p>Note these default values are used as-is, with no type conversion (because sometimes you just want <code>nil</code> 🤷), so exercise caution.</p>
224
- <pre class="code ruby"><code class="ruby"><span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span> <span class='symbol'>:MISSING_VAR</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; (NOT AN INTEGER)
225
- </span></code></pre>
226
- </li>
227
- <li>
228
- <p><strong>Selecting From A Set</strong></p>
229
- <p>Sometimes setting the <strong><em>as</em></strong> type is a bit too open-ended. The <strong><em>from_set</em></strong> option lets you restrict the domain of allowed values.</p>
230
- <pre class="code ruby"><code class="ruby"><span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</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='tstring_end'>]</span></span>
231
- <span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span> <span class='symbol'>: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>
232
-
233
- <span class='comment'># And if the value is not in the allowed set ...
234
- </span><span class='comment'>#
235
- </span><span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span> <span class='symbol'>:TWELVE</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
236
- </span></code></pre>
237
- </li>
238
- <li>
239
- <p><strong>Custom Validation Of Parsed Values</strong></p>
240
- <p>You can write your own, more complex validations by passing in a <strong><em>validated_by</em></strong> lambda or an equivalent block. The lambda/block should expect one value (of the requested <strong><em>as</em></strong> type) and return true if the given value passes the custom validation.</p>
241
- <pre class="code ruby"><code class="ruby"><span class='comment'># Via a &quot;validated_by&quot; lambda ...
242
- </span><span class='comment'>#
243
- </span><span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</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>
244
-
245
- <span class='comment'># ... or with a block!
246
- </span><span class='comment'>#
247
- </span><span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</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>
248
- <span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</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>
249
- </code></pre>
250
- </li>
251
- <li>
252
- <p><strong>Defining Your Own EnvParser &quot;<em>as</em>&quot; Types</strong></p>
253
- <p>If you use a particular validation many times or are often manipulating values in the same way after EnvParser has done its thing, you may want to register a new type altogether. Defining a new type makes your code both more maintainable (all the logic for your special type is only defined once) and more readable (your <code>parse</code> calls aren't littered with type-checking cruft).</p>
254
- <p>Something as repetitive as:</p>
255
- <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</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='int'>6</span>
256
- <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>
257
-
258
- <span class='id identifier rubyid_b'>b</span> <span class='op'>=</span> <span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</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='int'>6</span>
259
- <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>
260
- </code></pre>
261
- <p>... is perhaps best handled by defining a new type:</p>
262
- <pre class="code ruby"><code class="ruby"><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='int'>6</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>
263
- <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>
264
- <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>
265
- <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>
266
- <span class='kw'>end</span>
267
-
268
- <span class='id identifier rubyid_value'>value</span>
269
- <span class='kw'>end</span>
270
-
271
- <span class='id identifier rubyid_a'>a</span> <span class='op'>=</span> <span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span> <span class='symbol'>:A</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:my_special_type_of_number</span>
272
- <span class='id identifier rubyid_b'>b</span> <span class='op'>=</span> <span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_parse'>parse</span> <span class='symbol'>:B</span><span class='comma'>,</span> <span class='label'>as:</span> <span class='symbol'>:my_special_type_of_number</span>
273
- </code></pre>
274
- </li>
275
- </ul>
276
- <h4 id="Auto_Registering_Constants">Auto-Registering Constants</h4>
277
- <ul>
278
- <li>
279
- <p><strong>The <code>autoregister</code> Call</strong></p>
280
- <p>Consolidating all of your <code>EnvParser.register</code> calls into a single place only makes sense. A single <code>EnvParser.autoregister</code> call takes a filename to read and process as a series of constant registration requests. If no filename is given, the default <code>&quot;.env_parser.yml&quot;</code> is assumed.</p>
281
- <p>You'll normally want to call <code>EnvParser.autoregister</code> as early in your application as possible. For Rails applications (and other frameworks that call <code>require 'bundler/setup'</code>), requiring the EnvParser gem via ...</p>
282
- <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><span class='comma'>,</span> <span class='label'>require:</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>env_parser/autoregister</span><span class='tstring_end'>&#39;</span></span>
283
- </code></pre>
284
- <p>... will automatically make the autoregistration call for you as soon as the gem is loaded (which should be early enough for most uses). If this is <em>still</em> not early enough for your needs, you can always <code>require 'env_parser/autoregister'</code> yourself even before <code>bundler/setup</code> is invoked.</p>
285
- </li>
286
- <li>
287
- <p><strong>The &quot;.env_parser.yml&quot; File</strong></p>
288
- <p>If you recall, multiple constants can be registered via a single <code>EnvParser.register</code> call:</p>
289
- <pre class="code ruby"><code class="ruby">EnvParser.register :USERNAME, as: :string
290
- EnvParser.register :PASSWORD, as: :string
291
- EnvParser.register :MOCK_API, as: :boolean, within: MyClassOrModule }
292
-
293
- # ... is equivalent to ... #
294
-
295
- EnvParser.register USERNAME: { as: :string },
296
- PASSWORD: { as: :string },
297
- MOCK_API: { as: :boolean, within: MyClassOrModule }
298
- </code></pre>
299
- <p>The autoregistraton file is intended to read as a YAML version of what you'd pass to the single-call version of <code>EnvParser.register</code>: a single hash with keys for each of the constants you'd like to register, with each value being the set of options to parse that constant.</p>
300
- <p>The equivalent autoregistration file for the above would be:</p>
301
- <pre class="code yaml"><code class="yaml">USERNAME:
302
- as: :string
303
-
304
- PASSWORD:
305
- as: :string
306
-
307
- MOCK_API:
308
- as: :boolean
309
- within: MyClassOrModule
310
- </code></pre>
311
- <p>Because no Ruby <em>statements</em> can be safely represented via YAML, the set of <code>EnvParser.register</code> options available via autoregistration is limited to <strong><em>as</em></strong>, <strong><em>named</em></strong>, <strong><em>within</em></strong>, <strong><em>if_unset</em></strong>, and <strong><em>from_set</em></strong>. As an additional restriction, <strong><em>from_set</em></strong> (if given) must be an array, as ranges cannot be represented in YAML.</p>
312
- </li>
313
- </ul>
314
- <h2 id="Feature_Roadmap___Future_Development">Feature Roadmap / Future Development</h2>
315
- <p>Additional features coming in the future:</p>
316
- <ul>
317
- <li>Continue to round out the <strong><em>as</em></strong> type selection as ideas come to mind, suggestions are made, and pull requests are submitted.</li>
318
- </ul>
319
- <h2 id="Contribution___Development">Contribution / Development</h2>
320
- <p>Bug reports and pull requests are welcome at: <a href="https://github.com/nestor-custodio/env_parser"><a href="https://github.com/nestor-custodio/env_parser">https://github.com/nestor-custodio/env_parser</a></a></p>
321
- <p>After checking out the repo, run <code>bin/setup</code> to install dependencies. Then, run <code>bundle exec rspec</code> to run the tests. You can also run <code>bin/console</code> for an interactive prompt that will allow you to experiment.</p>
322
- <p>Linting is courtesy of <a href="https://docs.rubocop.org/">Rubocop</a> (<code>bundle exec rubocop</code>) and documentation is built using <a href="https://yardoc.org/">Yard</a> (<code>bundle exec yard</code>). Please ensure you have a clean bill of health from Rubocop and that any new features and/or changes to behaviour are reflected in the documentation before submitting a pull request.</p>
323
- <h2 id="License">License</h2>
324
- <p>EnvParser is available as open source under the terms of the <a href="https://tldrlegal.com/license/mit-license">MIT License</a>.</p></div></div>
325
-
326
- <div id="footer">
327
- Generated on Sun Jun 21 01:17:51 2026 by
328
- <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
329
- 0.9.44 (ruby-3.4.8).
330
- </div>
331
-
332
- </div>
333
- </body>
334
- </html>