env_bang 0.4.1 → 0.4.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 40326b30460e728e3e5da019de6cfc93ae84b32c
4
- data.tar.gz: 3be97c644bcad37d63d69ec8314cef353596f1a2
3
+ metadata.gz: 8b8b5ff7d55793a3606129515bdea01f59f072c7
4
+ data.tar.gz: 0abf994bd7a5c82fbcd26e36e36411e9a73031f4
5
5
  SHA512:
6
- metadata.gz: 2c43cd17a437cc0b8e6b2ecdc666cd19a9d5f989ba9dad8c51792ca50a9f6ade297df55761f50de2d47581d7b0b94f50afe21921934d5ceca4df759ff54027ad
7
- data.tar.gz: 5ce51c4a23064f9402672d56ef018ba2e10a056d972ff07830651ef6b2f976457390147fdbc11520ff55bf627811164bae37d92a285318499d401b1b68dcc275
6
+ metadata.gz: e57c9ffeb718dd8e49ce40edd1a69d34f72fa700967546ff4f545a9e92f793fab176299ae77771a97f7f2f0e33569b098808782bafe5673c151e3c70e467789f
7
+ data.tar.gz: a10569c4a1ca1fcd75703c1f8cfaddeca1ee20fd9c6521d58dfd932f490e5472985102f575c8b240d4dcb21ba6e7f7d6ec3dee0b571b9c2ecead60b705682e92
@@ -6,11 +6,19 @@ class ENV_BANG
6
6
  File.join(Rails.root.to_s, 'config/env.rb')
7
7
  end
8
8
 
9
+ def warn(msg)
10
+ if Rails.logger
11
+ Rails.logger.warn(msg)
12
+ else
13
+ puts "WARNING: #{msg}"
14
+ end
15
+ end
16
+
9
17
  config.before_configuration do
10
18
  if File.exists?(env_rb_file)
11
19
  load env_rb_file
12
20
  else
13
- Rails.logger.warn "ENV! could not find your environment variable configuration. Please create #{env_rb_file} to set up environment variables at Rails boot."
21
+ warn("ENV! could not find your environment variable configuration. Please create #{env_rb_file} to set up environment variables at Rails boot.")
14
22
  end
15
23
  end
16
24
  end
@@ -71,19 +71,42 @@ class ENV_BANG
71
71
  end
72
72
 
73
73
  def Date(value, options)
74
- require 'date'
75
74
  Date.parse(value)
76
75
  end
77
76
 
78
77
  def DateTime(value, options)
79
- require 'date'
80
78
  DateTime.parse(value)
81
79
  end
82
80
 
83
81
  def Time(value, options)
84
- require 'time'
85
82
  Time.parse(value)
86
83
  end
84
+
85
+ def Range(value, options = {})
86
+ beginning, ending = value.split('...')
87
+ if beginning && ending
88
+ options[:exclusive] = true unless options.has_key?(:exclusive)
89
+ else
90
+ beginning, ending = value.split('..')
91
+ end
92
+ unless beginning && ending
93
+ raise ArgumentError.new("Range '#{value}' cannot be parsed as a range. Must be in the form <start>..<end> or <start>...<end>")
94
+ end
95
+
96
+ options[:of] ||= Integer
97
+ beginning = cast(beginning, class: options[:of])
98
+ ending = cast(ending, class: options[:of])
99
+
100
+ if options[:exclusive]
101
+ beginning...ending
102
+ else
103
+ beginning..ending
104
+ end
105
+ end
106
+
107
+ def Regexp(value, options)
108
+ Regexp.new(value)
109
+ end
87
110
  end
88
111
  end
89
112
  end
@@ -1,3 +1,3 @@
1
1
  class ENV_BANG
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
@@ -190,6 +190,49 @@ describe ENV_BANG do
190
190
  ENV!['A_TIME'].must_equal Time.new(2005, 5, 5, 17, 5)
191
191
  end
192
192
 
193
+ it "casts Regexps" do
194
+ # Escaping backslashes is not without its pitfalls. Developer beware.
195
+ ENV['A_REGEX'] = '^(this|is|a|[^tes.*\|]t.\.\*/\\\)$'
196
+ ENV!.use 'A_REGEX', class: Regexp
197
+
198
+ ENV!['A_REGEX'].class.must_equal Regexp
199
+ ENV!['A_REGEX'].must_equal(/^(this|is|a|[^tes.*\|]t.\.\*\/\\)$/)
200
+ end
201
+
202
+ it "casts inclusive Ranges of Integers by default" do
203
+ ENV['A_RANGE'] = '1..100'
204
+ ENV!.use 'A_RANGE', class: Range
205
+
206
+ ENV!['A_RANGE'].class.must_equal Range
207
+ ENV!['A_RANGE'].must_equal 1..100
208
+ end
209
+
210
+ it "casts exclusive Ranges as directed" do
211
+ ENV['EXCLUSIVE_RANGE'] = '1..100'
212
+ ENV!.use 'EXCLUSIVE_RANGE', class: Range, exclusive: true
213
+
214
+ ENV!['EXCLUSIVE_RANGE'].must_equal 1...100
215
+
216
+ ENV['ANOTHER_EXCLUSIVE_RANGE'] = '1...100'
217
+ ENV!.use 'ANOTHER_EXCLUSIVE_RANGE', class: Range, exclusive: true
218
+
219
+ ENV!['ANOTHER_EXCLUSIVE_RANGE'].must_equal 1...100
220
+ end
221
+
222
+ it "casts Ranges of floats" do
223
+ ENV['FLOAT_RANGE'] = '1.5..100.7'
224
+ ENV!.use 'FLOAT_RANGE', class: Range, of: Float
225
+
226
+ ENV!['FLOAT_RANGE'].must_equal 1.5..100.7
227
+ end
228
+
229
+ it "casts Ranges of strings" do
230
+ ENV['FLOAT_RANGE'] = 'az..za'
231
+ ENV!.use 'FLOAT_RANGE', class: Range, of: String
232
+
233
+ ENV!['FLOAT_RANGE'].must_equal 'az'..'za'
234
+ end
235
+
193
236
  it "allows default class to be overridden" do
194
237
  ENV!.default_class.must_equal :StringUnlessFalsey
195
238
  ENV!.config { default_class String }
@@ -3,7 +3,6 @@ require 'coveralls'
3
3
 
4
4
  require 'minitest/autorun'
5
5
  require 'minitest/spec'
6
- require 'minitest/pride'
7
6
 
8
7
  SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
9
8
  SimpleCov::Formatter::HTMLFormatter,
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.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Camenisch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-22 00:00:00.000000000 Z
11
+ date: 2017-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake