env_parser 0.8.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +102 -115
- data/docs/EnvParser.html +128 -123
- data/docs/EnvParser/Error.html +4 -4
- data/docs/EnvParser/TypeAlreadyDefinedError.html +143 -0
- data/docs/EnvParser/UnknownTypeError.html +143 -0
- data/docs/EnvParser/ValueNotAllowedError.html +145 -0
- data/docs/EnvParser/ValueNotConvertibleError.html +144 -0
- data/docs/EnvParserTypes.html +127 -0
- data/docs/EnvParserTypes/BaseTypes.html +160 -0
- data/docs/_index.html +45 -3
- data/docs/class_list.html +1 -1
- data/docs/file.README.html +121 -132
- data/docs/index.html +121 -132
- data/docs/top-level-namespace.html +6 -4
- data/lib/env_parser.rb +66 -79
- data/lib/env_parser/errors.rb +28 -0
- data/lib/env_parser/types.rb +3 -0
- data/lib/env_parser/types/base_types.rb +80 -26
- data/lib/env_parser/version.rb +1 -1
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71db2fdf0d882840050bc0718e4695f3eeedfef4
|
4
|
+
data.tar.gz: e25fb8e4e50bc32c16f7479a26ca125818f35dc4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c27c985ef686b9c658648e833ee0cb1c65c191f38968ea3ad7114d2d4ed71ae737b4dd2cc951b168b7947ac61bbca4352585a503669934a075cbbf60ece9f0f7
|
7
|
+
data.tar.gz: f450e32ad5fdcbd32da05eb8ac49c984df30515262d63511e59d5a5093fc8fd418106f71b9e7740c43aed24a51abc4f8464ab675acad7e493be3de5b033e0934
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# EnvParser [![Gem Version](https://badge.fury.io/rb/env_parser.svg)](https://badge.fury.io/rb/env_parser)
|
2
2
|
|
3
|
-
If your code uses environment variables, you know that `ENV` will always surface these as strings. Interpreting these strings as the value you *actually* want to see/use takes some work, however: for numbers you need to cast with
|
3
|
+
If your code uses environment variables, you know that `ENV` will always surface these as strings. Interpreting these strings as the value you *actually* want to see/use takes some work, however: for numbers you need to cast with `to_i` or `to_f` ... for booleans you need to check for a specific value (`ENV['SOME_VAR'] == 'true'`) ... maybe you want to set non-trivial defaults (something other than `0` or `''`)? ... maybe you only want to allow values from a limited set? ...
|
4
4
|
|
5
|
-
Things can get out of control pretty fast, especially as the number of environment variables in play grows. Tools like [dotenv](https://github.com/bkeepers/dotenv) help to make sure you're loading the correct set of
|
5
|
+
Things can get out of control pretty fast, especially as the number of environment variables in play grows. Tools like [dotenv](https://github.com/bkeepers/dotenv) help to make sure you're loading the correct **set** of variables, but [EnvParser](https://github.com/nestor-custodio/env_parser) makes ***the values themselves*** usable with a minimum of effort.
|
6
6
|
|
7
7
|
|
8
8
|
## Installation
|
@@ -24,72 +24,28 @@ Or install it yourself as:
|
|
24
24
|
|
25
25
|
## Using EnvParser
|
26
26
|
|
27
|
-
|
27
|
+
### Basic Usage
|
28
|
+
|
29
|
+
#### Parsing ENV Values
|
28
30
|
|
29
31
|
```ruby
|
30
|
-
## Returns ENV['TIMEOUT_MS'] as an Integer
|
31
|
-
##
|
32
|
-
|
32
|
+
## Returns ENV['TIMEOUT_MS'] as an Integer,
|
33
|
+
## or 0 if ENV['TIMEOUT_MS'] is unset or nil.
|
34
|
+
|
33
35
|
timeout_ms = EnvParser.parse ENV['TIMEOUT_MS'], as: :integer
|
34
36
|
|
37
|
+
|
35
38
|
## LESS TYPING, PLZ! :(
|
36
39
|
## If you pass in a Symbol instead of a String, EnvParser
|
37
40
|
## will use the value behind the matching String key in ENV.
|
38
41
|
## (i.e. passing in ENV['X'] is equivalent to passing in :X)
|
39
|
-
|
42
|
+
|
40
43
|
timeout_ms = EnvParser.parse :TIMEOUT_MS, as: :integer
|
41
44
|
```
|
42
45
|
|
43
|
-
|
44
|
-
|
45
|
-
The named `:as` parameter is required. The list of allowed values is user-expandable, but allowed values out-of-the-box are:
|
46
|
-
|
47
|
-
<table>
|
48
|
-
<tbody>
|
49
|
-
<tr>
|
50
|
-
<th><code>:as</code> value</th>
|
51
|
-
<th>type returned</th>
|
52
|
-
</tr>
|
53
|
-
</tbody>
|
54
|
-
<tbody>
|
55
|
-
<tr>
|
56
|
-
<td>:string</td>
|
57
|
-
<td>String</td>
|
58
|
-
</tr>
|
59
|
-
<tr>
|
60
|
-
<td>:symbol</td>
|
61
|
-
<td>Symbol</td>
|
62
|
-
</tr>
|
63
|
-
<tr>
|
64
|
-
<td>:boolean</td>
|
65
|
-
<td>TrueValue / FalseValue</td>
|
66
|
-
</tr>
|
67
|
-
<tr>
|
68
|
-
<td>:int / :integer</td>
|
69
|
-
<td>Integer</td>
|
70
|
-
</tr>
|
71
|
-
<tr>
|
72
|
-
<td>:float / :decimal / :number</td>
|
73
|
-
<td>Float</td>
|
74
|
-
</tr>
|
75
|
-
<tr>
|
76
|
-
<td>:json</td>
|
77
|
-
<td>< depends on JSON given ></td>
|
78
|
-
</tr>
|
79
|
-
<tr>
|
80
|
-
<td>:array</td>
|
81
|
-
<td>Array</td>
|
82
|
-
</tr>
|
83
|
-
<tr>
|
84
|
-
<td>:hash</td>
|
85
|
-
<td>Hash</td>
|
86
|
-
</tr>
|
87
|
-
</tbody>
|
88
|
-
</table>
|
89
|
-
|
90
|
-
|
91
|
-
Note JSON is parsed using *quirks-mode* (meaning 'true', '25', and 'null' are all considered valid, parseable JSON).
|
46
|
+
For a full list of all "as" types available out-of-the-box, [see the documentation for modules listed under EnvParserTypes](http://nestor-custodio.github.io/env_parser/EnvParserTypes.html).
|
92
47
|
|
48
|
+
---
|
93
49
|
|
94
50
|
#### Setting Non-Trivial Defaults
|
95
51
|
|
@@ -98,65 +54,50 @@ Note JSON is parsed using *quirks-mode* (meaning 'true', '25', and 'null' are al
|
|
98
54
|
## the return value is a sensible default for the given "as" type
|
99
55
|
## (0 or 0.0 for numbers, an empty tring, an empty Array or Hash, etc).
|
100
56
|
## Sometimes you want a non-trivial default, however.
|
101
|
-
|
102
|
-
EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer
|
103
|
-
EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset: 250
|
57
|
+
|
58
|
+
EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer ## => 0
|
59
|
+
EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset: 250 ## => 250
|
60
|
+
|
104
61
|
|
105
62
|
## Note that "if_unset" values are used as-is, with no type conversion.
|
106
|
-
|
107
|
-
EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset: '
|
63
|
+
|
64
|
+
EnvParser.parse :MISSING_ENV_VARIABLE, as: :integer, if_unset: 'Careful!' ## => 'Careful!'
|
108
65
|
```
|
109
66
|
|
67
|
+
---
|
110
68
|
|
111
|
-
####
|
69
|
+
#### Setting Constants From ENV Values
|
112
70
|
|
113
71
|
```ruby
|
114
|
-
##
|
115
|
-
## The "from_set" option lets you restrict the set of allowed values.
|
116
|
-
##
|
117
|
-
EnvParser.parse :API_TO_USE, as: :symbol, from_set: %i[internal external]
|
118
|
-
EnvParser.parse :SOME_CUSTOM_NETWORK_PORT, as: :integer, from_set: (1..65535), if_unset: 80
|
72
|
+
## Global constants...
|
119
73
|
|
120
|
-
##
|
121
|
-
##
|
122
|
-
EnvParser.parse :NEGATIVE_NUMBER, as: :integer, from_set: (1..5) ## => raises EnvParser::ValueNotAllowed
|
74
|
+
ENV['API_KEY'] ## => 'unbreakable p4$$w0rd'
|
123
75
|
|
76
|
+
EnvParser.register :API_KEY, as: :string
|
77
|
+
API_KEY ## => 'unbreakable p4$$w0rd' (registered within the Kernel module, so it's available everywhere)
|
124
78
|
|
125
|
-
## The "validated_by" option allows for more complex validation.
|
126
|
-
##
|
127
|
-
EnvParser.parse :MUST_BE_LOWERCASE, as: :string, validated_by: ->(value) { value == value.downcase }
|
128
79
|
|
129
|
-
## ...
|
130
|
-
EnvParser.parse(:MUST_BE_LOWERCASE, as: :string) { |value| value == value.downcase }
|
131
|
-
EnvParser.parse(:CONNECTION_RETRIES, as: :integer, &:nonzero?)
|
132
|
-
```
|
80
|
+
## ... and class/module-level constants!
|
133
81
|
|
82
|
+
ENV['ULTIMATE_LINK'] ## => 'https://youtu.be/L_jWHffIx5E'
|
134
83
|
|
135
|
-
|
84
|
+
EnvParser.register :ULTIMATE_LINK, as: :string, within: URI
|
85
|
+
URI::ULTIMATE_LINK ## => 'https://youtu.be/L_jWHffIx5E'
|
86
|
+
|
87
|
+
ULTIMATE_LINK ## => raises NameError (the un-namespaced constant is only in scope within the URI module)
|
136
88
|
|
137
|
-
```ruby
|
138
|
-
## Global constants...
|
139
|
-
##
|
140
|
-
ENV['API_KEY'] ## => 'unbreakable p4$$w0rd' (Set elsewhere, like a ".env" file.)
|
141
|
-
EnvParser.register :API_KEY, as: :string
|
142
|
-
API_KEY ## => 'unbreakable p4$$w0rd' (registered within the Kernel module, so it's available everywhere)
|
143
89
|
|
144
|
-
## ... and class/module constants!
|
145
|
-
##
|
146
|
-
ENV['ULTIMATE_LINK'] ## => 'https://youtu.be/L_jWHffIx5E' (Set elsewhere, like a ".env" file.)
|
147
|
-
EnvParser.register :ULTIMATE_LINK, as: :string, within: URI
|
148
|
-
URI::ULTIMATE_LINK ## => 'https://youtu.be/L_jWHffIx5E' (You know you want to check it out!)
|
149
|
-
ULTIMATE_LINK ## => raises NameError (the un-namespaced constant is only in scope within the URI module)
|
150
90
|
|
151
91
|
|
152
92
|
## You can also set multiple constants in one call, which is considerably cleaner to read:
|
153
|
-
|
93
|
+
|
154
94
|
EnvParser.register :A, as: :string
|
155
95
|
EnvParser.register :B, as: :integer, if_unset: 25
|
156
96
|
EnvParser.register :C, as: :boolean, if_unset: true
|
157
97
|
|
98
|
+
|
158
99
|
## ... is equivalent to ...
|
159
|
-
|
100
|
+
|
160
101
|
EnvParser.register(
|
161
102
|
A: { as: :string },
|
162
103
|
B: { as: :integer, if_unset: 25 },
|
@@ -164,41 +105,87 @@ EnvParser.register(
|
|
164
105
|
)
|
165
106
|
```
|
166
107
|
|
108
|
+
---
|
167
109
|
|
168
110
|
#### Binding EnvParser Proxies Onto ENV
|
169
111
|
|
170
112
|
```ruby
|
171
|
-
##
|
172
|
-
## This is done
|
173
|
-
|
174
|
-
EnvParser.add_env_bindings
|
175
|
-
|
176
|
-
|
177
|
-
##
|
178
|
-
##
|
179
|
-
|
180
|
-
ENV
|
181
|
-
|
113
|
+
## You can bind proxy "parse" and "register" methods onto ENV.
|
114
|
+
## This is done without polluting the method space for other objects.
|
115
|
+
|
116
|
+
EnvParser.add_env_bindings ## Sets up the proxy methods.
|
117
|
+
|
118
|
+
|
119
|
+
## Now you can call "parse" and "register" on ENV itself,
|
120
|
+
## which is more legible and feels more straight-forward.
|
121
|
+
|
122
|
+
ENV['SHORT_PI'] ## => '3.1415926'
|
123
|
+
|
124
|
+
ENV.parse :SHORT_PI, as: :float ## => 3.1415926
|
125
|
+
ENV.register :SHORT_PI, as: :float ## Your constant is set, my man!
|
126
|
+
|
127
|
+
|
128
|
+
## Note that ENV's proxy "parse" method will *always* interpret the
|
129
|
+
## value given as an ENV key (converting to a String, if necessary).
|
130
|
+
## This is different from the non-proxy "parse" method, which will use
|
131
|
+
## String values as-is and only looks up ENV values when given a Symbol.
|
132
|
+
```
|
133
|
+
|
134
|
+
|
135
|
+
### Advanced Usage
|
136
|
+
|
137
|
+
#### Custom Validation Of Parsed Values
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
## Sometimes setting the type alone is a bit too open-ended.
|
141
|
+
## The "from_set" option lets you restrict the set of allowed values.
|
142
|
+
|
143
|
+
EnvParser.parse :API_TO_USE, as: :symbol, from_set: %i[internal external]
|
144
|
+
EnvParser.parse :SOME_CUSTOM_NETWORK_PORT, as: :integer, from_set: (1..65535), if_unset: 80
|
145
|
+
|
146
|
+
|
147
|
+
## And if the value is not allowed...
|
148
|
+
|
149
|
+
EnvParser.parse :NEGATIVE_NUMBER, as: :integer, from_set: (1..5) ## => raises EnvParser::ValueNotAllowed
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
|
154
|
+
## The "validated_by" option allows for more complex validation.
|
155
|
+
|
156
|
+
EnvParser.parse :MUST_BE_LOWERCASE, as: :string, validated_by: ->(value) { value == value.downcase }
|
157
|
+
|
158
|
+
|
159
|
+
## ... but a block will also do the trick!
|
160
|
+
|
161
|
+
EnvParser.parse(:MUST_BE_LOWERCASE, as: :string) { |value| value == value.downcase }
|
162
|
+
EnvParser.parse(:CONNECTION_RETRIES, as: :integer, &:nonzero?)
|
182
163
|
```
|
183
164
|
|
165
|
+
---
|
184
166
|
|
185
|
-
#### Defining
|
167
|
+
#### Defining Your Own EnvParser "as" Types
|
186
168
|
|
187
169
|
```ruby
|
188
|
-
## If you use a particular validation many times,
|
189
|
-
##
|
190
|
-
##
|
170
|
+
## If you use a particular validation many times,
|
171
|
+
## or are often manipulating values in the same way
|
172
|
+
## after EnvParser has done its thing, you may want
|
173
|
+
## to register a new type altogether.
|
174
|
+
|
191
175
|
a = EnvParser.parse :A, as: :int, if_unset: nil
|
192
176
|
raise unless passes_all_my_checks?(a)
|
193
177
|
|
194
178
|
b = EnvParser.parse :B, as: :int, if_unset: nil
|
195
179
|
raise unless passes_all_my_checks?(b)
|
196
180
|
|
181
|
+
|
197
182
|
## ... is perhaps best handled by defining a new type:
|
198
|
-
|
183
|
+
|
199
184
|
EnvParser.define_type(:my_special_type_of_number, if_unset: nil) do |value|
|
200
185
|
value = value.to_i
|
201
|
-
|
186
|
+
unless passes_all_my_checks?(value)
|
187
|
+
raise(EnvParser::ValueNotConvertibleError, 'cannot parse as a "special type number"')
|
188
|
+
end
|
202
189
|
|
203
190
|
value
|
204
191
|
end
|
@@ -206,14 +193,16 @@ end
|
|
206
193
|
a = EnvParser.parse :A, as: :my_special_type_of_number
|
207
194
|
b = EnvParser.parse :B, as: :my_special_type_of_number
|
208
195
|
|
209
|
-
|
210
|
-
##
|
211
|
-
##
|
196
|
+
|
197
|
+
## Defining a new type makes your code both more maintainable
|
198
|
+
## (all the logic for your special type is only defined once)
|
199
|
+
## and more readable (your "parse" calls aren't littered with
|
200
|
+
## type-checking cruft).
|
212
201
|
```
|
213
202
|
|
214
203
|
---
|
215
204
|
|
216
|
-
[Consult the repo docs](http://nestor-custodio.github.io/env_parser
|
205
|
+
[Consult the repo docs for the full EnvParser documentation.](http://nestor-custodio.github.io/env_parser/EnvParser.html)
|
217
206
|
|
218
207
|
|
219
208
|
## Feature Roadmap / Future Development
|
@@ -232,8 +221,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
|
232
221
|
|
233
222
|
Linting is courtesy of [Rubocop](https://github.com/bbatsov/rubocop) and documentation is built using [Yard](https://yardoc.org/). Neither is included in the Gemspec; you'll need to install these locally to take advantage.
|
234
223
|
|
235
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
236
|
-
|
237
224
|
|
238
225
|
## License
|
239
226
|
|
data/docs/EnvParser.html
CHANGED
@@ -95,7 +95,7 @@
|
|
95
95
|
<dl>
|
96
96
|
<dt>Defined in:</dt>
|
97
97
|
<dd>lib/env_parser.rb<span class="defines">,<br />
|
98
|
-
lib/env_parser/version.rb</span>
|
98
|
+
lib/env_parser/errors.rb,<br /> lib/env_parser/version.rb</span>
|
99
99
|
</dd>
|
100
100
|
</dl>
|
101
101
|
|
@@ -119,7 +119,7 @@ different data types.</p>
|
|
119
119
|
|
120
120
|
|
121
121
|
|
122
|
-
<strong class="classes">Classes:</strong> <span class='object_link'><a href="EnvParser/Error.html" title="EnvParser::Error (class)">Error</a></span>, <span class='object_link'><a href="EnvParser/
|
122
|
+
<strong class="classes">Classes:</strong> <span class='object_link'><a href="EnvParser/Error.html" title="EnvParser::Error (class)">Error</a></span>, <span class='object_link'><a href="EnvParser/TypeAlreadyDefinedError.html" title="EnvParser::TypeAlreadyDefinedError (class)">TypeAlreadyDefinedError</a></span>, <span class='object_link'><a href="EnvParser/UnknownTypeError.html" title="EnvParser::UnknownTypeError (class)">UnknownTypeError</a></span>, <span class='object_link'><a href="EnvParser/ValueNotAllowedError.html" title="EnvParser::ValueNotAllowedError (class)">ValueNotAllowedError</a></span>, <span class='object_link'><a href="EnvParser/ValueNotConvertibleError.html" title="EnvParser::ValueNotConvertibleError (class)">ValueNotConvertibleError</a></span>
|
123
123
|
|
124
124
|
|
125
125
|
</p>
|
@@ -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'>'</span><span class='tstring_content'>0.
|
133
|
+
<dd><pre class="code"><span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>1.0.0</span><span class='tstring_end'>'</span></span><span class='period'>.</span><span class='id identifier rubyid_freeze'>freeze</span></pre></dd>
|
134
134
|
|
135
135
|
</dl>
|
136
136
|
|
@@ -166,8 +166,7 @@ different data types.</p>
|
|
166
166
|
|
167
167
|
|
168
168
|
<span class="summary_desc"><div class='inline'>
|
169
|
-
<p>Creates ENV bindings for EnvParser.parse and EnvParser.register proxy
|
170
|
-
methods.</p>
|
169
|
+
<p>Creates ENV bindings for <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">EnvParser.parse</a></span> and <span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">EnvParser.register</a></span> proxy methods.</p>
|
171
170
|
</div></span>
|
172
171
|
|
173
172
|
</li>
|
@@ -176,7 +175,7 @@ methods.</p>
|
|
176
175
|
<li class="public ">
|
177
176
|
<span class="summary_signature">
|
178
177
|
|
179
|
-
<a href="#define_type-class_method" title="define_type (class method)">.<strong>define_type</strong>(name, options = {}) { ... } ⇒ nil </a>
|
178
|
+
<a href="#define_type-class_method" title="define_type (class method)">.<strong>define_type</strong>(name, options = {}) {|value| ... } ⇒ nil </a>
|
180
179
|
|
181
180
|
|
182
181
|
|
@@ -191,8 +190,8 @@ methods.</p>
|
|
191
190
|
|
192
191
|
|
193
192
|
<span class="summary_desc"><div class='inline'>
|
194
|
-
<p>Defines a new type for use as the “as” option on a subsequent
|
195
|
-
<
|
193
|
+
<p>Defines a new type for use as the “as” option on a subsequent <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">EnvParser.parse</a></span> or
|
194
|
+
<span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">EnvParser.register</a></span> call.</p>
|
196
195
|
</div></span>
|
197
196
|
|
198
197
|
</li>
|
@@ -268,8 +267,7 @@ requested context.</p>
|
|
268
267
|
</h3><div class="docstring">
|
269
268
|
<div class="discussion">
|
270
269
|
|
271
|
-
<p>Creates ENV bindings for EnvParser.parse and EnvParser.register proxy
|
272
|
-
methods.</p>
|
270
|
+
<p>Creates ENV bindings for <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span> and <span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">register</a></span> proxy methods.</p>
|
273
271
|
|
274
272
|
<p>The sole difference between these proxy methods and their EnvParser
|
275
273
|
counterparts is that ENV.parse will interpret any value given as an ENV key
|
@@ -293,7 +291,7 @@ is equivalent to <a href="'XYZ'">EnvParser.parse(ENV</a>, …)</p>
|
|
293
291
|
|
294
292
|
—
|
295
293
|
<div class='inline'>
|
296
|
-
<p>This generates no usable value
|
294
|
+
<p>This generates no usable value.</p>
|
297
295
|
</div>
|
298
296
|
|
299
297
|
</li>
|
@@ -306,22 +304,22 @@ is equivalent to <a href="'XYZ'">EnvParser.parse(ENV</a>, …)</p>
|
|
306
304
|
<pre class="lines">
|
307
305
|
|
308
306
|
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
307
|
+
216
|
308
|
+
217
|
309
|
+
218
|
310
|
+
219
|
311
|
+
220
|
312
|
+
221
|
313
|
+
222
|
314
|
+
223
|
315
|
+
224
|
316
|
+
225
|
317
|
+
226
|
318
|
+
227
|
319
|
+
228</pre>
|
322
320
|
</td>
|
323
321
|
<td>
|
324
|
-
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line
|
322
|
+
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 216</span>
|
325
323
|
|
326
324
|
<span class='kw'>def</span> <span class='id identifier rubyid_add_env_bindings'>add_env_bindings</span>
|
327
325
|
<span class='const'>ENV</span><span class='period'>.</span><span class='id identifier rubyid_instance_eval'>instance_eval</span> <span class='kw'>do</span>
|
@@ -344,7 +342,7 @@ is equivalent to <a href="'XYZ'">EnvParser.parse(ENV</a>, …)</p>
|
|
344
342
|
<div class="method_details ">
|
345
343
|
<h3 class="signature " id="define_type-class_method">
|
346
344
|
|
347
|
-
.<strong>define_type</strong>(name, options = {}) { ... } ⇒ <tt>nil</tt>
|
345
|
+
.<strong>define_type</strong>(name, options = {}) {|value| ... } ⇒ <tt>nil</tt>
|
348
346
|
|
349
347
|
|
350
348
|
|
@@ -353,8 +351,8 @@ is equivalent to <a href="'XYZ'">EnvParser.parse(ENV</a>, …)</p>
|
|
353
351
|
</h3><div class="docstring">
|
354
352
|
<div class="discussion">
|
355
353
|
|
356
|
-
<p>Defines a new type for use as the “as” option on a subsequent
|
357
|
-
<
|
354
|
+
<p>Defines a new type for use as the “as” option on a subsequent <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span> or
|
355
|
+
<span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">register</a></span> call.</p>
|
358
356
|
|
359
357
|
|
360
358
|
</div>
|
@@ -425,14 +423,16 @@ is equivalent to <a href="'XYZ'">EnvParser.parse(ENV</a>, …)</p>
|
|
425
423
|
<span class="type">(<tt>Object</tt>)</span>
|
426
424
|
<span class="default">
|
427
425
|
|
426
|
+
— default:
|
427
|
+
<tt>nil</tt>
|
428
|
+
|
428
429
|
</span>
|
429
430
|
|
430
431
|
— <div class='inline'>
|
431
432
|
<p>Specifies a “sensible default” to return for this type if the value being
|
432
|
-
parsed (via <
|
433
|
-
|
434
|
-
|
435
|
-
“if_unset” option.</p>
|
433
|
+
parsed (via <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span> or <span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">register</a></span>) is either unset (<code>nil</code>) or
|
434
|
+
blank (<code>''</code>). Note this may be overridden by the user
|
435
|
+
via the <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span>/<span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">register</a></span> “if_unset” option.</p>
|
436
436
|
</div>
|
437
437
|
|
438
438
|
</li>
|
@@ -446,24 +446,23 @@ overridden by the user via the <code>.parse</code>/<code>.register</code>
|
|
446
446
|
<li>
|
447
447
|
|
448
448
|
|
449
|
-
<span class='type'
|
449
|
+
<span class='type'>(<tt>value</tt>)</span>
|
450
450
|
|
451
451
|
|
452
452
|
|
453
|
-
|
453
|
+
—
|
454
454
|
<div class='inline'>
|
455
455
|
<p>A block to act as the parser for the this type. If no block is given, an
|
456
456
|
ArgumentError is raised.</p>
|
457
457
|
|
458
|
-
<p>When the type defined is used via a
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
given String value as the type being defined.</p>
|
458
|
+
<p>When the type defined is used via a <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span>/<span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">register</a></span> call, this block
|
459
|
+
is invoked with the value to be parsed. Said value is guaranteed to be a
|
460
|
+
non-empty String (the “if_unset” check will have already run), but no other
|
461
|
+
assurances as to content are given. The block should return the final
|
462
|
+
output of parsing the given String value as the type being defined.</p>
|
464
463
|
|
465
464
|
<p>If the value given cannot be sensibly parsed into the type defined, the
|
466
|
-
block should raise an EnvParser::
|
465
|
+
block should raise an <span class='object_link'><a href="EnvParser/ValueNotConvertibleError.html" title="EnvParser::ValueNotConvertibleError (class)">ValueNotConvertibleError</a></span>.</p>
|
467
466
|
</div>
|
468
467
|
|
469
468
|
</li>
|
@@ -493,7 +492,7 @@ block should raise an EnvParser::ValueNotAllowed exception.</p>
|
|
493
492
|
<li>
|
494
493
|
|
495
494
|
|
496
|
-
<span class='type'>(<tt>ArgumentError</tt>, <tt><span class='object_link'><a href="EnvParser/
|
495
|
+
<span class='type'>(<tt>ArgumentError</tt>, <tt><span class='object_link'><a href="EnvParser/TypeAlreadyDefinedError.html" title="EnvParser::TypeAlreadyDefinedError (class)">EnvParser::TypeAlreadyDefinedError</a></span></tt>)</span>
|
497
496
|
|
498
497
|
|
499
498
|
|
@@ -507,31 +506,31 @@ block should raise an EnvParser::ValueNotAllowed exception.</p>
|
|
507
506
|
<pre class="lines">
|
508
507
|
|
509
508
|
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
509
|
+
39
|
510
|
+
40
|
511
|
+
41
|
512
|
+
42
|
513
|
+
43
|
514
|
+
44
|
515
|
+
45
|
516
|
+
46
|
517
|
+
47
|
518
|
+
48
|
519
|
+
49
|
520
|
+
50
|
521
|
+
51
|
522
|
+
52
|
523
|
+
53</pre>
|
525
524
|
</td>
|
526
525
|
<td>
|
527
|
-
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line
|
526
|
+
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 39</span>
|
528
527
|
|
529
528
|
<span class='kw'>def</span> <span class='id identifier rubyid_define_type'>define_type</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='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_parser'>parser</span><span class='rparen'>)</span>
|
530
529
|
<span class='id identifier rubyid_raise'>raise</span><span class='lparen'>(</span><span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>no parsing block given</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span> <span class='kw'>unless</span> <span class='id identifier rubyid_block_given?'>block_given?</span>
|
531
530
|
|
532
531
|
<span class='id identifier rubyid_given_types'>given_types</span> <span class='op'>=</span> <span class='lparen'>(</span><span class='const'>Array</span><span class='lparen'>(</span><span class='id identifier rubyid_name'>name</span><span class='rparen'>)</span> <span class='op'>+</span> <span class='const'>Array</span><span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:aliases</span><span class='rbracket'>]</span><span class='rparen'>)</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lparen'>(</span><span class='op'>&</span><span class='symbol'>:to_s</span><span class='rparen'>)</span><span class='period'>.</span><span class='id identifier rubyid_map'>map</span><span class='lparen'>(</span><span class='op'>&</span><span class='symbol'>:to_sym</span><span class='rparen'>)</span>
|
533
532
|
<span class='id identifier rubyid_given_types'>given_types</span><span class='period'>.</span><span class='id identifier rubyid_each'>each</span> <span class='kw'>do</span> <span class='op'>|</span><span class='id identifier rubyid_type'>type</span><span class='op'>|</span>
|
534
|
-
<span class='id identifier rubyid_raise'>raise</span><span class='lparen'>(</span><span class='const'><span class='object_link'><a href="EnvParser/
|
533
|
+
<span class='id identifier rubyid_raise'>raise</span><span class='lparen'>(</span><span class='const'><span class='object_link'><a href="EnvParser/TypeAlreadyDefinedError.html" title="EnvParser::TypeAlreadyDefinedError (class)">TypeAlreadyDefinedError</a></span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>cannot redefine </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_type'>type</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_known_types'>known_types</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span><span class='lparen'>(</span><span class='id identifier rubyid_type'>type</span><span class='rparen'>)</span>
|
535
534
|
|
536
535
|
<span class='id identifier rubyid_known_types'>known_types</span><span class='lbracket'>[</span><span class='id identifier rubyid_type'>type</span><span class='rbracket'>]</span> <span class='op'>=</span> <span class='lbrace'>{</span>
|
537
536
|
<span class='label'>parser:</span> <span class='id identifier rubyid_parser'>parser</span><span class='comma'>,</span>
|
@@ -624,8 +623,9 @@ be used.</p>
|
|
624
623
|
<p>The expected return type. A best-effort attempt is made to convert the
|
625
624
|
source String to the requested type.</p>
|
626
625
|
|
627
|
-
<p>If no “as” option is given
|
628
|
-
|
626
|
+
<p>If no “as” option is given, an ArgumentError is raised. If the “as” option
|
627
|
+
given is unknown (the given type has not been previously defined via
|
628
|
+
<span class='object_link'><a href="#define_type-class_method" title="EnvParser.define_type (method)">define_type</a></span>), an <span class='object_link'><a href="EnvParser/UnknownTypeError.html" title="EnvParser::UnknownTypeError (class)">UnknownTypeError</a></span> is raised.</p>
|
629
629
|
</div>
|
630
630
|
|
631
631
|
</li>
|
@@ -658,7 +658,7 @@ type.</p>
|
|
658
658
|
— <div class='inline'>
|
659
659
|
<p>Gives a limited set of allowed values (after type conversion). If, after
|
660
660
|
parsing, the final value is not included in the “from_set” list/range, an
|
661
|
-
EnvParser::
|
661
|
+
<span class='object_link'><a href="EnvParser/ValueNotAllowedError.html" title="EnvParser::ValueNotAllowedError (class)">ValueNotAllowedError</a></span> is raised.</p>
|
662
662
|
|
663
663
|
<p>Note that if the “if_unset” option is given and the value to parse is
|
664
664
|
<code>nil</code>/<code>''</code>, the “if_unset” value will be
|
@@ -667,7 +667,7 @@ returned, even if it is not part of the “from_set” list/range.</p>
|
|
667
667
|
<p>Also note that, due to the nature of the lookup, the “from_set” option is
|
668
668
|
only available for scalar values (i.e. not arrays, hashes, or other
|
669
669
|
enumerables). An attempt to use the “from_set” option with a non-scalar
|
670
|
-
value will raise an ArgumentError
|
670
|
+
value will raise an ArgumentError.</p>
|
671
671
|
</div>
|
672
672
|
|
673
673
|
</li>
|
@@ -680,16 +680,16 @@ value will raise an ArgumentError exception.</p>
|
|
680
680
|
</span>
|
681
681
|
|
682
682
|
— <div class='inline'>
|
683
|
-
<p>If given, the “validated_by”
|
683
|
+
<p>If given, the “validated_by” Proc is called with the parsed value (after
|
684
684
|
type conversion) as its sole argument. This allows for user-defined
|
685
685
|
validation of the parsed value beyond what can be enforced by use of the
|
686
|
-
“from_set” option alone. If the
|
687
|
-
<code>#blank?</code>, an EnvParser::
|
688
|
-
accomodate your syntax of choice, this validation
|
689
|
-
|
686
|
+
“from_set” option alone. If the Proc's return value is
|
687
|
+
<code>#blank?</code>, an <span class='object_link'><a href="EnvParser/ValueNotAllowedError.html" title="EnvParser::ValueNotAllowedError (class)">ValueNotAllowedError</a></span> is raised. To
|
688
|
+
accomodate your syntax of choice, this validation Proc may be given as a
|
689
|
+
block instead.</p>
|
690
690
|
|
691
691
|
<p>Note that this option is intended to provide an inspection mechanism only –
|
692
|
-
no mutation of the parsed value should occur within the given
|
692
|
+
no mutation of the parsed value should occur within the given Proc. To that
|
693
693
|
end, the argument passed is a <em>frozen</em> duplicate of the parsed
|
694
694
|
value.</p>
|
695
695
|
</div>
|
@@ -711,9 +711,10 @@ value.</p>
|
|
711
711
|
|
712
712
|
—
|
713
713
|
<div class='inline'>
|
714
|
-
<p>A block (if given) is treated exactly as the “validated_by” Proc would
|
715
|
-
|
716
|
-
|
714
|
+
<p>A block (if given) is treated exactly as the “validated_by” Proc would.</p>
|
715
|
+
|
716
|
+
<p>Although there is no compelling reason to provide both a “validated_by”
|
717
|
+
Proc <em>and</em> a validation block, there is no technical limitation
|
717
718
|
preventing this. <strong>If both are given, both validation checks must
|
718
719
|
pass.</strong></p>
|
719
720
|
</div>
|
@@ -727,7 +728,7 @@ pass.</strong></p>
|
|
727
728
|
<li>
|
728
729
|
|
729
730
|
|
730
|
-
<span class='type'>(<tt>ArgumentError</tt>, <tt><span class='object_link'><a href="EnvParser/
|
731
|
+
<span class='type'>(<tt>ArgumentError</tt>, <tt><span class='object_link'><a href="EnvParser/UnknownTypeError.html" title="EnvParser::UnknownTypeError (class)">EnvParser::UnknownTypeError</a></span></tt>, <tt><span class='object_link'><a href="EnvParser/ValueNotAllowedError.html" title="EnvParser::ValueNotAllowedError (class)">EnvParser::ValueNotAllowedError</a></span></tt>)</span>
|
731
732
|
|
732
733
|
|
733
734
|
|
@@ -741,31 +742,33 @@ pass.</strong></p>
|
|
741
742
|
<pre class="lines">
|
742
743
|
|
743
744
|
|
745
|
+
107
|
746
|
+
108
|
747
|
+
109
|
748
|
+
110
|
749
|
+
111
|
750
|
+
112
|
751
|
+
113
|
752
|
+
114
|
753
|
+
115
|
754
|
+
116
|
755
|
+
117
|
756
|
+
118
|
744
757
|
119
|
745
758
|
120
|
746
759
|
121
|
747
|
-
122
|
748
|
-
123
|
749
|
-
124
|
750
|
-
125
|
751
|
-
126
|
752
|
-
127
|
753
|
-
128
|
754
|
-
129
|
755
|
-
130
|
756
|
-
131
|
757
|
-
132
|
758
|
-
133</pre>
|
760
|
+
122</pre>
|
759
761
|
</td>
|
760
762
|
<td>
|
761
|
-
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line
|
763
|
+
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 107</span>
|
762
764
|
|
763
765
|
<span class='kw'>def</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='op'>=</span> <span class='lbrace'>{</span><span class='rbrace'>}</span><span class='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_validation_block'>validation_block</span><span class='rparen'>)</span>
|
764
766
|
<span class='id identifier rubyid_value'>value</span> <span class='op'>=</span> <span class='const'>ENV</span><span class='lbracket'>[</span><span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_to_s'>to_s</span><span class='rbracket'>]</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span> <span class='const'>Symbol</span>
|
765
767
|
<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_s'>to_s</span>
|
766
768
|
|
767
769
|
<span class='id identifier rubyid_type'>type</span> <span class='op'>=</span> <span class='id identifier rubyid_known_types'>known_types</span><span class='lbracket'>[</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:as</span><span class='rbracket'>]</span><span class='rbracket'>]</span>
|
768
|
-
<span class='id identifier rubyid_raise'>raise</span><span class='lparen'>(</span><span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'
|
770
|
+
<span class='id identifier rubyid_raise'>raise</span><span class='lparen'>(</span><span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>missing `as` parameter</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span> <span class='kw'>unless</span> <span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span><span class='lparen'>(</span><span class='symbol'>:as</span><span class='rparen'>)</span>
|
771
|
+
<span class='id identifier rubyid_raise'>raise</span><span class='lparen'>(</span><span class='const'><span class='object_link'><a href="EnvParser/UnknownTypeError.html" title="EnvParser::UnknownTypeError (class)">UnknownTypeError</a></span></span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>invalid `as` parameter: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:as</span><span class='rbracket'>]</span><span class='period'>.</span><span class='id identifier rubyid_inspect'>inspect</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span><span class='rparen'>)</span> <span class='kw'>unless</span> <span class='id identifier rubyid_type'>type</span>
|
769
772
|
|
770
773
|
<span class='kw'>return</span> <span class='lparen'>(</span><span class='id identifier rubyid_options'>options</span><span class='period'>.</span><span class='id identifier rubyid_key?'>key?</span><span class='lparen'>(</span><span class='symbol'>:if_unset</span><span class='rparen'>)</span> <span class='op'>?</span> <span class='id identifier rubyid_options'>options</span><span class='lbracket'>[</span><span class='symbol'>:if_unset</span><span class='rbracket'>]</span> <span class='op'>:</span> <span class='id identifier rubyid_type'>type</span><span class='lbracket'>[</span><span class='symbol'>:if_unset</span><span class='rbracket'>]</span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_value'>value</span><span class='period'>.</span><span class='id identifier rubyid_blank?'>blank?</span>
|
771
774
|
|
@@ -795,9 +798,9 @@ pass.</strong></p>
|
|
795
798
|
<p>Parses the referenced value and creates a matching constant in the
|
796
799
|
requested context.</p>
|
797
800
|
|
798
|
-
<p>Multiple calls to
|
799
|
-
the
|
800
|
-
|
801
|
+
<p>Multiple calls to <span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">register</a></span> may be shortcutted by passing in a Hash whose
|
802
|
+
keys are the variable names and whose values are the options set for each
|
803
|
+
variable's <span class='object_link'><a href="#register-class_method" title="EnvParser.register (method)">register</a></span> call.</p>
|
801
804
|
<pre class="code ruby"><code class="ruby">
|
802
805
|
## Example shortcut usage:
|
803
806
|
|
@@ -830,8 +833,8 @@ the same keys as those in the “from” Hash and each value being the
|
|
830
833
|
—
|
831
834
|
<div class='inline'>
|
832
835
|
<p>The name of the value to parse/interpret from the “from” Hash. If the
|
833
|
-
“from” value is ENV
|
834
|
-
will be used instead.</p>
|
836
|
+
“from” value is <code>ENV</code>, you may give a Symbol and the
|
837
|
+
corresponding String key will be used instead.</p>
|
835
838
|
</div>
|
836
839
|
|
837
840
|
</li>
|
@@ -869,11 +872,13 @@ will be used instead.</p>
|
|
869
872
|
<span class="type">(<tt>Hash</tt>)</span>
|
870
873
|
<span class="default">
|
871
874
|
|
875
|
+
— default:
|
876
|
+
<tt>ENV</tt>
|
877
|
+
|
872
878
|
</span>
|
873
879
|
|
874
880
|
— <div class='inline'>
|
875
|
-
<p>The source Hash from which to pull the value referenced by the “name” key
|
876
|
-
Defaults to ENV.</p>
|
881
|
+
<p>The source Hash from which to pull the value referenced by the “name” key.</p>
|
877
882
|
</div>
|
878
883
|
|
879
884
|
</li>
|
@@ -883,11 +888,14 @@ Defaults to ENV.</p>
|
|
883
888
|
<span class="type">(<tt>Module</tt>, <tt>Class</tt>)</span>
|
884
889
|
<span class="default">
|
885
890
|
|
891
|
+
— default:
|
892
|
+
<tt>Kernel</tt>
|
893
|
+
|
886
894
|
</span>
|
887
895
|
|
888
896
|
— <div class='inline'>
|
889
|
-
<p>The module or class in which the constant should be created.
|
890
|
-
|
897
|
+
<p>The module or class in which the constant should be created. Creates global
|
898
|
+
constants by default.</p>
|
891
899
|
</div>
|
892
900
|
|
893
901
|
</li>
|
@@ -900,7 +908,7 @@ Kernel (making it a global constant).</p>
|
|
900
908
|
</span>
|
901
909
|
|
902
910
|
— <div class='inline'>
|
903
|
-
<p>See <
|
911
|
+
<p>See <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span>.</p>
|
904
912
|
</div>
|
905
913
|
|
906
914
|
</li>
|
@@ -913,7 +921,7 @@ Kernel (making it a global constant).</p>
|
|
913
921
|
</span>
|
914
922
|
|
915
923
|
— <div class='inline'>
|
916
|
-
<p>See <
|
924
|
+
<p>See <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span>.</p>
|
917
925
|
</div>
|
918
926
|
|
919
927
|
</li>
|
@@ -926,7 +934,7 @@ Kernel (making it a global constant).</p>
|
|
926
934
|
</span>
|
927
935
|
|
928
936
|
— <div class='inline'>
|
929
|
-
<p>See <
|
937
|
+
<p>See <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span>.</p>
|
930
938
|
</div>
|
931
939
|
|
932
940
|
</li>
|
@@ -939,7 +947,7 @@ Kernel (making it a global constant).</p>
|
|
939
947
|
</span>
|
940
948
|
|
941
949
|
— <div class='inline'>
|
942
|
-
<p>See <
|
950
|
+
<p>See <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span>.</p>
|
943
951
|
</div>
|
944
952
|
|
945
953
|
</li>
|
@@ -959,10 +967,9 @@ Kernel (making it a global constant).</p>
|
|
959
967
|
|
960
968
|
—
|
961
969
|
<div class='inline'>
|
962
|
-
<p>A block (if given) is treated exactly as in <
|
963
|
-
|
964
|
-
|
965
|
-
“validated_by” proc.</p>
|
970
|
+
<p>A block (if given) is treated exactly as in <span class='object_link'><a href="#parse-class_method" title="EnvParser.parse (method)">parse</a></span>. Note, however, that a
|
971
|
+
single block cannot be used to register multiple constants simultaneously –
|
972
|
+
each value needing validation must give its own “validated_by” Proc.</p>
|
966
973
|
</div>
|
967
974
|
|
968
975
|
</li>
|
@@ -988,6 +995,18 @@ constants simultaneously – each value needing validation must give its own
|
|
988
995
|
<pre class="lines">
|
989
996
|
|
990
997
|
|
998
|
+
173
|
999
|
+
174
|
1000
|
+
175
|
1001
|
+
176
|
1002
|
+
177
|
1003
|
+
178
|
1004
|
+
179
|
1005
|
+
180
|
1006
|
+
181
|
1007
|
+
182
|
1008
|
+
183
|
1009
|
+
184
|
991
1010
|
185
|
992
1011
|
186
|
993
1012
|
187
|
@@ -1008,29 +1027,15 @@ constants simultaneously – each value needing validation must give its own
|
|
1008
1027
|
202
|
1009
1028
|
203
|
1010
1029
|
204
|
1011
|
-
205
|
1012
|
-
206
|
1013
|
-
207
|
1014
|
-
208
|
1015
|
-
209
|
1016
|
-
210
|
1017
|
-
211
|
1018
|
-
212
|
1019
|
-
213
|
1020
|
-
214
|
1021
|
-
215
|
1022
|
-
216
|
1023
|
-
217
|
1024
|
-
218</pre>
|
1030
|
+
205</pre>
|
1025
1031
|
</td>
|
1026
1032
|
<td>
|
1027
|
-
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line
|
1033
|
+
<pre class="code"><span class="info file"># File 'lib/env_parser.rb', line 173</span>
|
1028
1034
|
|
1029
1035
|
<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='comma'>,</span> <span class='op'>&</span><span class='id identifier rubyid_validation_block'>validation_block</span><span class='rparen'>)</span>
|
1030
|
-
<span class='comment'>##
|
1031
|
-
</span> <span class='comment'>## method call.
|
1036
|
+
<span class='comment'>## Allow for registering multiple variables simultaneously via a single call.
|
1032
1037
|
</span> <span class='kw'>if</span> <span class='id identifier rubyid_name'>name</span><span class='period'>.</span><span class='id identifier rubyid_is_a?'>is_a?</span> <span class='const'>Hash</span>
|
1033
|
-
<span class='id identifier rubyid_raise'>raise</span>
|
1038
|
+
<span class='id identifier rubyid_raise'>raise</span><span class='lparen'>(</span><span class='const'>ArgumentError</span><span class='comma'>,</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>cannot register multiple values with one block</span><span class='tstring_end'>'</span></span><span class='rparen'>)</span> <span class='kw'>if</span> <span class='id identifier rubyid_block_given?'>block_given?</span>
|
1034
1039
|
<span class='kw'>return</span> <span class='id identifier rubyid_register_all'>register_all</span><span class='lparen'>(</span><span class='id identifier rubyid_name'>name</span><span class='rparen'>)</span>
|
1035
1040
|
<span class='kw'>end</span>
|
1036
1041
|
|
@@ -1070,7 +1075,7 @@ constants simultaneously – each value needing validation must give its own
|
|
1070
1075
|
</div>
|
1071
1076
|
|
1072
1077
|
<div id="footer">
|
1073
|
-
Generated on
|
1078
|
+
Generated on Mon Dec 25 19:13:02 2017 by
|
1074
1079
|
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
1075
1080
|
0.9.11 (ruby-2.4.2).
|
1076
1081
|
</div>
|