environment_helpers 1.2.1 → 1.3.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: 71bfb123e7f90d29f9e038849bc995207b9c11b166aae7e0938cf162e7ed70f5
4
- data.tar.gz: bb88e8d31c0c343a54f7be6a6919af08446a2a9fff973868b9ba22a9f1fc3f9a
3
+ metadata.gz: c789efc6ec01f80dab6e39fcf2ae8c1941642b61751e8585ed4765e7679260c2
4
+ data.tar.gz: 29fab39b2237db66ef6d980880f606d4ec0ba4be4991ccb38a3aface1149d68f
5
5
  SHA512:
6
- metadata.gz: f9adafc223d6c2edff2c4179bb86d172e2febe28d8761a2d55b9b76db02d6076ce981229cd8f758c905c4e4e524ad4b6b6e589ff55ced624fbc510ad8a183ed1
7
- data.tar.gz: 7d6ae5d630de28a2d5db1776d06ed7d2a6117a905b66c12aa61b2b7fe5c4dd64dd8cd5e764c450fcf846b077ee23c7f74f6bd393d3cec8b161f6365a27cd498b
6
+ metadata.gz: 617d37082c5f021bbf1691332e4cade122014508394c8f6f3a8b6e80e20847a57d41793a1fbb99ff3f9ca15bfd5f86666ff8b72883643681f72341757f88043e
7
+ data.tar.gz: 040fc4d6d63f5306c96bdf39fd4814608912c23063b9ced4f101b9250c39cbbf0ebf93626e47d41813e8872bebc946dfcbd54d4003fcf994b711ba820c201672
data/README.md CHANGED
@@ -38,6 +38,7 @@ ENV.integer_range("ID_RANGE", default: (500..6000))
38
38
  ENV.integer("MAX_THREAD_COUNT", default: 5)
39
39
  ENV.file_path("FILE_PATH", default: "/some/path", required: true)
40
40
  ENV.date("SCHEDULED_DATE", required: true, format: "%Y-%m-%d")
41
+ ENV.date_time("RUN_AT", required: true, default: DateTime.now)
41
42
  ```
42
43
 
43
44
  Each of the supplied methods takes a positional parameter for the name of the environment variable,
@@ -64,3 +65,8 @@ The available methods added to `ENV`:
64
65
  which would parse a date like `2023-12-25`. It will handle invalid values (or format strings) like
65
66
  the variable not being present, though if it's specified as `required`, you will see a different
66
67
  exception in each case.
68
+ * `date_time` - produces a `DateTime` object, using either `DateTime.strptime` or `DateTime.iso8601`.
69
+ The default format is `:iso8601`, and `:unix` is also an allowed 'format'. But if it is supplied
70
+ as a _string_, it will be handled as a strptime format string (the `:unix` format is equivalent to
71
+ the format string `"%s"`). It handles invalid or unparseable values like `ENV.date` does, in that
72
+ they are treated as if not supplied.
@@ -12,6 +12,16 @@ module EnvironmentHelpers
12
12
  fail(InvalidDateText, "Required date environment variable #{name} had inappropriate content '#{text}'")
13
13
  end
14
14
 
15
+ def date_time(name, format: :iso8601, default: nil, required: false)
16
+ check_default_type(:date_time, default, DateTime)
17
+ text = fetch_value(name, required: required)
18
+ dt = parse_date_time_from(text, format: format)
19
+
20
+ return dt if dt
21
+ return default unless required
22
+ fail(InvalidDateTimeText, "Require date_time environment variable #{name} had inappropriate content '#{text}'")
23
+ end
24
+
15
25
  private
16
26
 
17
27
  def parse_date_from(text, format:)
@@ -20,5 +30,37 @@ module EnvironmentHelpers
20
30
  rescue ArgumentError
21
31
  nil
22
32
  end
33
+
34
+ def parse_date_time_from(text, format:)
35
+ if text.nil?
36
+ nil
37
+ elsif format == :iso8601
38
+ iso8601_date_time(text)
39
+ elsif format == :unix
40
+ unix_date_time(text)
41
+ elsif format.is_a?(String)
42
+ strptime_date_time(text, format: format)
43
+ else
44
+ fail(BadFormat, "ENV.date_time requires either a strptime format string, :unix, or :iso8601")
45
+ end
46
+ end
47
+
48
+ def iso8601_date_time(text)
49
+ DateTime.iso8601(text)
50
+ rescue
51
+ nil
52
+ end
53
+
54
+ def unix_date_time(text)
55
+ DateTime.strptime(text, "%s")
56
+ rescue
57
+ nil
58
+ end
59
+
60
+ def strptime_date_time(text, format:)
61
+ DateTime.strptime(text, format)
62
+ rescue
63
+ nil
64
+ end
23
65
  end
24
66
  end
@@ -1,3 +1,3 @@
1
1
  module EnvironmentHelpers
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -14,12 +14,14 @@ module EnvironmentHelpers
14
14
  Error = Class.new(::StandardError)
15
15
  MissingVariableError = Class.new(Error)
16
16
  BadDefault = Class.new(Error)
17
+ BadFormat = Class.new(Error)
17
18
 
18
19
  InvalidValue = Class.new(Error)
19
20
  InvalidBooleanText = Class.new(InvalidValue)
20
21
  InvalidRangeText = Class.new(InvalidValue)
21
22
  InvalidIntegerText = Class.new(InvalidValue)
22
23
  InvalidDateText = Class.new(InvalidValue)
24
+ InvalidDateTimeText = Class.new(InvalidValue)
23
25
 
24
26
  include AccessHelpers
25
27
  include StringHelpers
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: environment_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Mueller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-04-28 00:00:00.000000000 Z
11
+ date: 2023-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
128
  - !ruby/object:Gem::Version
129
129
  version: '0'
130
130
  requirements: []
131
- rubygems_version: 3.3.26
131
+ rubygems_version: 3.1.6
132
132
  signing_key:
133
133
  specification_version: 4
134
134
  summary: A set of convenience methods for accessing environment data