env_bang 0.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/env_bang/version.rb +1 -1
- data/lib/env_bang.rb +10 -2
- data/test/env_bang_test.rb +21 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55f77b7c37c5f38cc2d89f6564976aeb3e71dee8
|
4
|
+
data.tar.gz: 6e84440c64a0ea51fcc2a9be15a2a78f64506b77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b02a627cd7bc398627292834eff7efda1b8938bce66722d37e33d61f157a196f1da4abd6271fd4eba6c2ae766f0546b53acece6568a865a094d91a436425eb87
|
7
|
+
data.tar.gz: 2cc8df056e3b654eb50cc0ab25a0cea4c64cf119d0420c6337a6cc0720affbfa59d29a5a65fdb3555bfd8d605c190c9c4056fe49493e62d925de11e1c8c3c85c
|
data/lib/env_bang/version.rb
CHANGED
data/lib/env_bang.rb
CHANGED
@@ -15,7 +15,7 @@ class ENV_BANG
|
|
15
15
|
end
|
16
16
|
|
17
17
|
# Store the variable, converted to requested class
|
18
|
-
klass = :"#{options.fetch(:class,
|
18
|
+
klass = :"#{options.fetch(:class, :StringUnlessEmpty)}"
|
19
19
|
vars[var] = Classes.cast ENV[var], klass, options
|
20
20
|
end
|
21
21
|
|
@@ -52,10 +52,18 @@ class ENV_BANG
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def Array(value, options)
|
55
|
-
klass = options.fetch(:of,
|
55
|
+
klass = options.fetch(:of, :StringUnlessEmpty)
|
56
56
|
value.split(',').map { |value| cast(value.strip, klass) }
|
57
57
|
end
|
58
58
|
|
59
|
+
def Symbol(value, options)
|
60
|
+
value.to_sym
|
61
|
+
end
|
62
|
+
|
63
|
+
def StringUnlessEmpty(value, options)
|
64
|
+
value.length > 0 ? value : nil
|
65
|
+
end
|
66
|
+
|
59
67
|
# Delegate methods like Integer(), Float(), String(), etc. to the Kernel module
|
60
68
|
def method_missing(klass, value, options, &block)
|
61
69
|
Kernel.send(klass, value)
|
data/test/env_bang_test.rb
CHANGED
@@ -59,6 +59,13 @@ describe ENV_BANG do
|
|
59
59
|
ENV!['INTEGER'].must_equal integer.to_i
|
60
60
|
end
|
61
61
|
|
62
|
+
it "Casts Symbols" do
|
63
|
+
ENV['SYMBOL'] = 'symbol'
|
64
|
+
ENV!.use 'SYMBOL', class: Symbol
|
65
|
+
|
66
|
+
ENV!['SYMBOL'].must_equal :symbol
|
67
|
+
end
|
68
|
+
|
62
69
|
it "Casts Floats" do
|
63
70
|
float = floats.sample
|
64
71
|
ENV['FLOAT'] = float
|
@@ -102,5 +109,19 @@ describe ENV_BANG do
|
|
102
109
|
|
103
110
|
ENV!['FALSE'].must_equal false
|
104
111
|
end
|
112
|
+
|
113
|
+
it "converts an empty string to nil by default" do
|
114
|
+
ENV['NIL'] = ''
|
115
|
+
ENV!.use 'NIL'
|
116
|
+
|
117
|
+
ENV!['NIL'].must_be_nil
|
118
|
+
end
|
119
|
+
|
120
|
+
it "leaves an empty string as String if specified" do
|
121
|
+
ENV['EMPTY_STRING'] = ''
|
122
|
+
ENV!.use 'EMPTY_STRING', class: String
|
123
|
+
|
124
|
+
ENV!['EMPTY_STRING'].must_equal ''
|
125
|
+
end
|
105
126
|
end
|
106
127
|
end
|