environment_helpers 1.0.1 → 1.1.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
  SHA256:
3
- metadata.gz: 8909c0a7ad45e4fc50777d074086126849b1b9a3907ecae5f5b6c5fafd547c3a
4
- data.tar.gz: 59f2b7b8cd84a28cc24b545fc240f5145a38b4d445a60f63ef35b881d6ee2c50
3
+ metadata.gz: 202ea78421b65c63b2d3151a55ddfc891aab67569d67dceba358939f0b89fcd7
4
+ data.tar.gz: 82344f0d77473dd80a467a915c33d5897ae763eaa8d9faa18e81b9b9c99e9774
5
5
  SHA512:
6
- metadata.gz: 744fa0ee02661aa41df2a0bee2b88af5402d0ab5dfefa77e93aeabfccb44186c68e79da151ea926b85a0608c9c903f1925e877362d6d7cfad43f5d3172b3dbdb
7
- data.tar.gz: 56099bbc01ff6226b63fedab0ed108eccb65775550782e1635ece461985ad1817e99340a2cedd01be724ee50bbf2d9def8e2cad005d2ae7299750cd418fd9e3c
6
+ metadata.gz: f7b0d5146de039a774301fb24266f4d2cdb467e095e0915121771b543aed10782c88e80f5c9c24e234b6cf9821e76637232f7220812061774599ec03b0f500b9
7
+ data.tar.gz: 3aa3c4435e7e7a9ebf83d1fbf9277e55a7fb12be6280119045ab2dbaf80f20715116a357f09dca9884a57772c889890defa509df7cf2a551ee3ecf8f8f8cbe9a
data/README.md CHANGED
@@ -34,6 +34,7 @@ methods onto `ENV` for your use.
34
34
  ENV.string("APP_NAME", default: "local")
35
35
  ENV.symbol("BUSINESS_DOMAIN", default: :engineering, required: true)
36
36
  ENV.boolean("ENABLE_FEATURE_FOO", default: false)
37
+ ENV.integer_range("ID_RANGE", default: (500..6000))
37
38
  ENV.integer("MAX_THREAD_COUNT", default: 5)
38
39
  ENV.date("SCHEDULED_DATE", required: true, format: "%Y-%m-%d")
39
40
  ```
@@ -51,6 +52,8 @@ The available methods added to `ENV`:
51
52
  a fair variety of strings to map onto those boolean value, though you should probably just use
52
53
  "true" and "false" really. If you specify `required: true` and get a value like "maybe?", it'll
53
54
  raise an `EnvironmentHelpers::InvalidBooleanText` exception.
55
+ * `integer_range` - produces an integer Range object. It accepts `N-N`, `N..N`, or `N...N`, (the
56
+ latter means 'excluding the upper bound, as in ruby).
54
57
  * `integer` - produces an integer from the environment variable, by calling `to_i` on it (if it's
55
58
  present). Note that this means that providing a value like "hello" means you'll get `0`, since
56
59
  that's what ruby does when you call `"hello".to_i`.
@@ -0,0 +1,42 @@
1
+ module EnvironmentHelpers
2
+ module RangeHelpers
3
+ def integer_range(name, default: nil, required: false)
4
+ check_default_type(:integer_range, default, Range)
5
+ check_range_endpoint(:integer_range, default.begin) if default
6
+ check_range_endpoint(:integer_range, default.end) if default
7
+
8
+ text = fetch_value(name, required: required)
9
+ range = parse_range_from(text)
10
+ return range if range
11
+ return default unless required
12
+ fail(InvalidRangeText, "Required Integer Range environment variable #{name} had inappropriate content '#{text}'")
13
+ end
14
+
15
+ private
16
+
17
+ def check_range_endpoint(context, value)
18
+ return if value.is_a?(Integer)
19
+ fail(BadDefault, "Invalid endpoint for default range of #{context} - must be Integer")
20
+ end
21
+
22
+ def parse_range_bound_from(text)
23
+ return nil if text.nil?
24
+ return nil if text.empty?
25
+ text.to_i
26
+ end
27
+
28
+ def parse_range_from(text)
29
+ text =~ /\A(\d*)(-|\.\.|\.\.\.)(\d*)\z/
30
+ lower_bound = parse_range_bound_from($1)
31
+ separator = $2
32
+ upper_bound = parse_range_bound_from($3)
33
+
34
+ return nil if lower_bound.nil? || upper_bound.nil?
35
+ if separator == "..."
36
+ (lower_bound...upper_bound)
37
+ else
38
+ (lower_bound..upper_bound)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,3 +1,3 @@
1
1
  module EnvironmentHelpers
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require_relative "./environment_helpers/access_helpers"
2
2
  require_relative "./environment_helpers/string_helpers"
3
3
  require_relative "./environment_helpers/boolean_helpers"
4
+ require_relative "./environment_helpers/range_helpers"
4
5
  require_relative "./environment_helpers/numeric_helpers"
5
6
  require_relative "./environment_helpers/datetime_helpers"
6
7
 
@@ -11,12 +12,14 @@ module EnvironmentHelpers
11
12
 
12
13
  InvalidValue = Class.new(Error)
13
14
  InvalidBooleanText = Class.new(InvalidValue)
15
+ InvalidRangeText = Class.new(InvalidValue)
14
16
  InvalidIntegerText = Class.new(InvalidValue)
15
17
  InvalidDateText = Class.new(InvalidValue)
16
18
 
17
19
  include AccessHelpers
18
20
  include StringHelpers
19
21
  include BooleanHelpers
22
+ include RangeHelpers
20
23
  include NumericHelpers
21
24
  include DatetimeHelpers
22
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: environment_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Mueller
@@ -103,6 +103,7 @@ files:
103
103
  - lib/environment_helpers/boolean_helpers.rb
104
104
  - lib/environment_helpers/datetime_helpers.rb
105
105
  - lib/environment_helpers/numeric_helpers.rb
106
+ - lib/environment_helpers/range_helpers.rb
106
107
  - lib/environment_helpers/string_helpers.rb
107
108
  - lib/environment_helpers/version.rb
108
109
  homepage: https://github.com/nevinera/environment_helpers