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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f61fe363d185a95ba2baba7dfe930515d5b484ae
4
- data.tar.gz: a89ea79b5655e95d808c5c18bd362e2c671010a9
3
+ metadata.gz: 55f77b7c37c5f38cc2d89f6564976aeb3e71dee8
4
+ data.tar.gz: 6e84440c64a0ea51fcc2a9be15a2a78f64506b77
5
5
  SHA512:
6
- metadata.gz: 8fe82b08b5a0646f74a1507d5c086be3782f0efb46d289263fc81eca1427dc0760029114e973188d6b03774a1065bbabc1da14132dbe77ca0f41ca8721702df6
7
- data.tar.gz: 9c61fb918a554319585259bc50d09537b89d0dbd5457209fdad34042bde68a34709da0bb2755ed7b781d5e6b1d248d4904e5c49f3154bb33714281bb6902d0bf
6
+ metadata.gz: b02a627cd7bc398627292834eff7efda1b8938bce66722d37e33d61f157a196f1da4abd6271fd4eba6c2ae766f0546b53acece6568a865a094d91a436425eb87
7
+ data.tar.gz: 2cc8df056e3b654eb50cc0ab25a0cea4c64cf119d0420c6337a6cc0720affbfa59d29a5a65fdb3555bfd8d605c190c9c4056fe49493e62d925de11e1c8c3c85c
@@ -1,3 +1,3 @@
1
1
  class ENV_BANG
2
- VERSION = "0.2"
2
+ VERSION = "0.2.1"
3
3
  end
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, String)}"
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, String)
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)
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env_bang
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Camenisch