env_bang 0.3.0 → 0.4.0

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: 56076f21b1106dd3b9f43505dc300ccd27d09cdb
4
- data.tar.gz: a8d957643766fbf0e01c3386fa8f91ff84dbaa17
3
+ metadata.gz: b42325fe65dd1f25327d31375eef610c15ff1c31
4
+ data.tar.gz: bfbfb4e949f607e562cdd0128ea5058bb0729edb
5
5
  SHA512:
6
- metadata.gz: 3f0f5477d06e160baf27e3cd37c6c3837f7facf41d029fa82be8f8a427ad2629aab132f9f80bf0a9088718bef8be62c628072054b451ef3990da18c97864b96c
7
- data.tar.gz: a06cf701db3d782c7cd8f528f9043b35fd72951da9b960ff7b8fd13fd0e5147ab5a117966c791c42727a0f10970c43421e410826c82b18090b681ddd259830fb
6
+ metadata.gz: b1c29a0e1109565143174fb540f536a628c4e24151ba562ac83e659b85e0aaa10c59a3c17a51f2bb7edb160d8b905249513f2f30c5c3447e442a1c723e6792c7
7
+ data.tar.gz: b2f70f65708e68570e6a0eb9b7af2b06110b364b863c7d41325325a8ab3b3d187ba45df75252e40eb7b4ca4b7a11fe095e4f12592fa4402bc01e9c230855190d
@@ -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) && args.shift
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
- raise KeyError.new Formatter.formatted_error(var, description)
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
@@ -39,9 +39,32 @@ class ENV_BANG
39
39
  boolean(value, options) && value
40
40
  end
41
41
 
42
- # Delegate methods like Integer(), Float(), String(), etc. to the Kernel module
43
- def method_missing(klass, value, options, &block)
44
- Kernel.send(klass, value)
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
@@ -2,11 +2,11 @@ class ENV_BANG
2
2
  module Formatter
3
3
  class << self
4
4
  def formatted_error(var, description)
5
- indent 4, <<-EOS
5
+ indent(4, "
6
6
 
7
- Missing required environment variable: #{var}#{ description and "\n" <<
8
- unindent(description) }
9
- EOS
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 "\n", "\n#{' ' * width}"
18
+ string.gsub("\n", "\n#{' ' * width}")
19
19
  end
20
20
  end
21
21
  end
@@ -1,3 +1,3 @@
1
1
  class ENV_BANG
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -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.3.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: 2016-01-26 00:00:00.000000000 Z
11
+ date: 2017-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake