environment_helpers 1.0.1 → 1.2.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: fdd872a3aa32975aeeb47dbe0236e1571c41c47632c0fcae86fea957f8c1eafa
4
+ data.tar.gz: d9f41beec0bba097c4bda2c0aa6c365c0967106843daeb79650e58b08c2e6a43
5
5
  SHA512:
6
- metadata.gz: 744fa0ee02661aa41df2a0bee2b88af5402d0ab5dfefa77e93aeabfccb44186c68e79da151ea926b85a0608c9c903f1925e877362d6d7cfad43f5d3172b3dbdb
7
- data.tar.gz: 56099bbc01ff6226b63fedab0ed108eccb65775550782e1635ece461985ad1817e99340a2cedd01be724ee50bbf2d9def8e2cad005d2ae7299750cd418fd9e3c
6
+ metadata.gz: 5a0adb44b8ce267e6e6d072c3f7e474c55fce9709bb924736b5f8abad59d54cd0e88d401892adbf850c589abfed455eec7925077fc2b62393984943c636c938b
7
+ data.tar.gz: c64a226be91f881470d39671fc8a559927c475ea9e32e347ad54a2dae02dc534190a743f1927601bd321e2acbe37d872c49c66251c1fad9be2f74e3586025894
data/README.md CHANGED
@@ -30,11 +30,13 @@ methods onto `ENV` for your use.
30
30
 
31
31
  ## Usage
32
32
 
33
- ```
33
+ ```shell
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)
39
+ ENV.file_path("FILE_PATH", default: "/some/path", required: true)
38
40
  ENV.date("SCHEDULED_DATE", required: true, format: "%Y-%m-%d")
39
41
  ```
40
42
 
@@ -45,15 +47,19 @@ isn't present in the environment. If `required` is set to a truthy value, then i
45
47
  present in the environment, an `EnvironmentHelpers::MissingVariableError` is raised.
46
48
 
47
49
  The available methods added to `ENV`:
50
+
48
51
  * `string` - environment values are already strings, so this is the simplest of the methods.
49
52
  * `symbol` - produces a symbol, and enforces that the default value is either `nil` or a Symbol.
50
53
  * `boolean` - produces `nil`, `true`, or `false` (and only allows those as defaults). Supports..
51
54
  a fair variety of strings to map onto those boolean value, though you should probably just use
52
55
  "true" and "false" really. If you specify `required: true` and get a value like "maybe?", it'll
53
56
  raise an `EnvironmentHelpers::InvalidBooleanText` exception.
57
+ * `integer_range` - produces an integer Range object. It accepts `N-N`, `N..N`, or `N...N`, (the
58
+ latter means 'excluding the upper bound, as in ruby).
54
59
  * `integer` - produces an integer from the environment variable, by calling `to_i` on it (if it's
55
60
  present). Note that this means that providing a value like "hello" means you'll get `0`, since
56
61
  that's what ruby does when you call `"hello".to_i`.
62
+ * `file_path` - produces a `Pathname` initialized with the path specified by the environment variable.
57
63
  * `date` - produces a `Date` object, using `Date.strptime`. The default format string is `%Y-%m-%d`,
58
64
  which would parse a date like `2023-12-25`. It will handle invalid values (or format strings) like
59
65
  the variable not being present, though if it's specified as `required`, you will see a different
@@ -0,0 +1,12 @@
1
+ require "pathname"
2
+
3
+ module EnvironmentHelpers
4
+ module FileHelpers
5
+ def file_path(name, default: nil, required: false)
6
+ check_default_type(:file_path, default, String)
7
+ path = fetch_value(name, required: required) || default
8
+ return nil if path.nil?
9
+ Pathname.new(path)
10
+ end
11
+ end
12
+ end
@@ -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.2.0"
3
3
  end
@@ -1,7 +1,9 @@
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"
6
+ require_relative "./environment_helpers/file_helpers"
5
7
  require_relative "./environment_helpers/datetime_helpers"
6
8
 
7
9
  module EnvironmentHelpers
@@ -11,13 +13,16 @@ module EnvironmentHelpers
11
13
 
12
14
  InvalidValue = Class.new(Error)
13
15
  InvalidBooleanText = Class.new(InvalidValue)
16
+ InvalidRangeText = Class.new(InvalidValue)
14
17
  InvalidIntegerText = Class.new(InvalidValue)
15
18
  InvalidDateText = Class.new(InvalidValue)
16
19
 
17
20
  include AccessHelpers
18
21
  include StringHelpers
19
22
  include BooleanHelpers
23
+ include RangeHelpers
20
24
  include NumericHelpers
25
+ include FileHelpers
21
26
  include DatetimeHelpers
22
27
  end
23
28
 
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.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Mueller
@@ -102,7 +102,9 @@ files:
102
102
  - lib/environment_helpers/access_helpers.rb
103
103
  - lib/environment_helpers/boolean_helpers.rb
104
104
  - lib/environment_helpers/datetime_helpers.rb
105
+ - lib/environment_helpers/file_helpers.rb
105
106
  - lib/environment_helpers/numeric_helpers.rb
107
+ - lib/environment_helpers/range_helpers.rb
106
108
  - lib/environment_helpers/string_helpers.rb
107
109
  - lib/environment_helpers/version.rb
108
110
  homepage: https://github.com/nevinera/environment_helpers