environment_helpers 1.2.0 → 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: fdd872a3aa32975aeeb47dbe0236e1571c41c47632c0fcae86fea957f8c1eafa
4
- data.tar.gz: d9f41beec0bba097c4bda2c0aa6c365c0967106843daeb79650e58b08c2e6a43
3
+ metadata.gz: c789efc6ec01f80dab6e39fcf2ae8c1941642b61751e8585ed4765e7679260c2
4
+ data.tar.gz: 29fab39b2237db66ef6d980880f606d4ec0ba4be4991ccb38a3aface1149d68f
5
5
  SHA512:
6
- metadata.gz: 5a0adb44b8ce267e6e6d072c3f7e474c55fce9709bb924736b5f8abad59d54cd0e88d401892adbf850c589abfed455eec7925077fc2b62393984943c636c938b
7
- data.tar.gz: c64a226be91f881470d39671fc8a559927c475ea9e32e347ad54a2dae02dc534190a743f1927601bd321e2acbe37d872c49c66251c1fad9be2f74e3586025894
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.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -1,3 +1,7 @@
1
+ # 'set' doesn't need requiring after ruby 3.2, but it won't hurt anything.
2
+ # And we're compatible back to 2.6
3
+ require "set" # rubocop:disable Lint/RedundantRequireStatement
4
+
1
5
  require_relative "./environment_helpers/access_helpers"
2
6
  require_relative "./environment_helpers/string_helpers"
3
7
  require_relative "./environment_helpers/boolean_helpers"
@@ -10,12 +14,14 @@ module EnvironmentHelpers
10
14
  Error = Class.new(::StandardError)
11
15
  MissingVariableError = Class.new(Error)
12
16
  BadDefault = Class.new(Error)
17
+ BadFormat = Class.new(Error)
13
18
 
14
19
  InvalidValue = Class.new(Error)
15
20
  InvalidBooleanText = Class.new(InvalidValue)
16
21
  InvalidRangeText = Class.new(InvalidValue)
17
22
  InvalidIntegerText = Class.new(InvalidValue)
18
23
  InvalidDateText = Class.new(InvalidValue)
24
+ InvalidDateTimeText = Class.new(InvalidValue)
19
25
 
20
26
  include AccessHelpers
21
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.0
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-25 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