env_bang 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 +4 -4
- data/lib/env_bang.rb +5 -3
- data/lib/env_bang/classes.rb +26 -3
- data/lib/env_bang/formatter.rb +5 -5
- data/lib/env_bang/version.rb +1 -1
- data/test/env_bang_test.rb +55 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b42325fe65dd1f25327d31375eef610c15ff1c31
|
4
|
+
data.tar.gz: bfbfb4e949f607e562cdd0128ea5058bb0729edb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b1c29a0e1109565143174fb540f536a628c4e24151ba562ac83e659b85e0aaa10c59a3c17a51f2bb7edb160d8b905249513f2f30c5c3447e442a1c723e6792c7
|
7
|
+
data.tar.gz: b2f70f65708e68570e6a0eb9b7af2b06110b364b863c7d41325325a8ab3b3d187ba45df75252e40eb7b4ca4b7a11fe095e4f12592fa4402bc01e9c230855190d
|
data/lib/env_bang.rb
CHANGED
@@ -10,7 +10,7 @@ class ENV_BANG
|
|
10
10
|
|
11
11
|
def use(var, *args)
|
12
12
|
var = var.to_s
|
13
|
-
description = args.first.is_a?(String)
|
13
|
+
description = args.first.is_a?(String) ? args.shift : nil
|
14
14
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
15
15
|
|
16
16
|
unless ENV.has_key?(var)
|
@@ -20,8 +20,10 @@ class ENV_BANG
|
|
20
20
|
vars[var] = options
|
21
21
|
end
|
22
22
|
|
23
|
-
def raise_formatted_error(var, description)
|
24
|
-
|
23
|
+
def raise_formatted_error(var, description)
|
24
|
+
e = KeyError.new(Formatter.formatted_error(var, description))
|
25
|
+
e.set_backtrace(caller[3..-1])
|
26
|
+
raise e
|
25
27
|
end
|
26
28
|
|
27
29
|
def vars
|
data/lib/env_bang/classes.rb
CHANGED
@@ -39,9 +39,32 @@ class ENV_BANG
|
|
39
39
|
boolean(value, options) && value
|
40
40
|
end
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
def Integer(value, options)
|
43
|
+
Kernel.Integer(value)
|
44
|
+
end
|
45
|
+
|
46
|
+
def Float(value, options)
|
47
|
+
Kernel.Float(value)
|
48
|
+
end
|
49
|
+
|
50
|
+
def String(value, options)
|
51
|
+
Kernel.String(value)
|
52
|
+
end
|
53
|
+
|
54
|
+
def Rational(value, options)
|
55
|
+
Kernel.Rational(value)
|
56
|
+
end
|
57
|
+
|
58
|
+
def Complex(value, options)
|
59
|
+
Kernel.Complex(value)
|
60
|
+
end
|
61
|
+
|
62
|
+
def Pathname(value, options)
|
63
|
+
Kernel.Pathname(value)
|
64
|
+
end
|
65
|
+
|
66
|
+
def URI(value, options)
|
67
|
+
Kernel.URI(value)
|
45
68
|
end
|
46
69
|
end
|
47
70
|
end
|
data/lib/env_bang/formatter.rb
CHANGED
@@ -2,11 +2,11 @@ class ENV_BANG
|
|
2
2
|
module Formatter
|
3
3
|
class << self
|
4
4
|
def formatted_error(var, description)
|
5
|
-
indent
|
5
|
+
indent(4, "
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
Missing required environment variable: #{var}#{description and "\n" <<
|
8
|
+
unindent(description) }
|
9
|
+
")
|
10
10
|
end
|
11
11
|
|
12
12
|
def unindent(string)
|
@@ -15,7 +15,7 @@ class ENV_BANG
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def indent(width, string)
|
18
|
-
string.gsub
|
18
|
+
string.gsub("\n", "\n#{' ' * width}")
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
data/lib/env_bang/version.rb
CHANGED
data/test/env_bang_test.rb
CHANGED
@@ -171,6 +171,61 @@ describe ENV_BANG do
|
|
171
171
|
|
172
172
|
ENV!['NUMBER_SET'].must_equal Set.new [1, 3, 5, 7, 9]
|
173
173
|
end
|
174
|
+
|
175
|
+
describe "Kernel casting delegators" do
|
176
|
+
it "casts Integers" do
|
177
|
+
ENV['A_INTEGER'] = '-123'
|
178
|
+
ENV!.use 'A_INTEGER', class: Integer
|
179
|
+
|
180
|
+
ENV!['A_INTEGER'].must_equal(-123)
|
181
|
+
end
|
182
|
+
|
183
|
+
it "casts Floats" do
|
184
|
+
ENV['A_FLOAT'] = '123.456'
|
185
|
+
ENV!.use 'A_FLOAT', class: Float
|
186
|
+
|
187
|
+
ENV!['A_FLOAT'].must_equal 123.456
|
188
|
+
end
|
189
|
+
|
190
|
+
it "casts Strings" do
|
191
|
+
ENV['A_STRING'] = 'What do I write here?'
|
192
|
+
ENV!.use 'A_STRING', class: String
|
193
|
+
ENV!['A_STRING'].must_equal 'What do I write here?'
|
194
|
+
end
|
195
|
+
|
196
|
+
it "casts Rationals" do
|
197
|
+
ENV['A_RATIONAL'] = '3/32'
|
198
|
+
ENV!.use 'A_RATIONAL', class: Rational
|
199
|
+
|
200
|
+
ENV!['A_RATIONAL'].class.must_equal Rational
|
201
|
+
ENV!['A_RATIONAL'].must_equal 3.to_r/32
|
202
|
+
ENV!['A_RATIONAL'].to_s.must_equal '3/32'
|
203
|
+
end
|
204
|
+
|
205
|
+
it "casts Complexes" do
|
206
|
+
ENV['A_COMPLEX'] = '123+4i'
|
207
|
+
ENV!.use 'A_COMPLEX', class: Complex
|
208
|
+
|
209
|
+
ENV!['A_COMPLEX'].class.must_equal Complex
|
210
|
+
ENV!['A_COMPLEX'].to_s.must_equal '123+4i'
|
211
|
+
end
|
212
|
+
|
213
|
+
it "casts Pathnames" do
|
214
|
+
ENV['A_PATHNAME'] = '~/.git/config'
|
215
|
+
ENV!.use 'A_PATHNAME', class: Pathname
|
216
|
+
|
217
|
+
ENV!['A_PATHNAME'].class.must_equal Pathname
|
218
|
+
ENV!['A_PATHNAME'].to_s.must_equal '~/.git/config'
|
219
|
+
end
|
220
|
+
|
221
|
+
it "casts URIs" do
|
222
|
+
ENV['A_URI'] = 'http://www.example.com/path/to/nowhere'
|
223
|
+
ENV!.use 'A_URI', class: URI
|
224
|
+
|
225
|
+
ENV!['A_URI'].class.must_equal URI::HTTP
|
226
|
+
ENV!['A_URI'].to_s.must_equal 'http://www.example.com/path/to/nowhere'
|
227
|
+
end
|
228
|
+
end
|
174
229
|
end
|
175
230
|
|
176
231
|
describe "Hash-like behavior" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: env_bang
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Camenisch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|