environment_helpers 1.4.0 → 1.5.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: 977bcb9a26d78cbddc4857274e59ed796d9454993fa5bc307c3643af08ed53e2
4
- data.tar.gz: 4696a62fd389365b56c79e952a1f4ac947970483faf17122e21cc312a1eff2c4
3
+ metadata.gz: 5e1df5aabc663c1ed5969c56db3d1be35f6b329272718ce174b30260f4c2f1e8
4
+ data.tar.gz: 855f2cf510c568464310ef1bc30fb6115a6dff653618f25472a452fcf901cb77
5
5
  SHA512:
6
- metadata.gz: 1fca3efa7549d3464b174bb84fdaab9d5f0a6c150121d50cf715891eafcca4f097f24c2624f8cab10812260e0c1358928eac811cf299b232afd0a4971641f084
7
- data.tar.gz: 2240bb669c7d0852c581b4ab0c0c343cdc1c1dece4da9b8b1d34c2b08658c9aa4e5a8ddfb6e6c864751ee4f6fd82d1ad351721213b005270faa233aa0f957a79
6
+ metadata.gz: 2dcccd3be2e778d76ce595521efc58204b680acfbe3328c86e357946574c338e323ee7a4e479e39d768f97a67fb6ecb7bb2c7aad7c68db7ed2d6f30bd761f299
7
+ data.tar.gz: 6a9bccc3864329bf295f59c268d64bed2078d072f9eed08ce2ff3063ead716f2e3e5a14b333f710b01b6092ec3be595bb71e74548c83515f230dfb9306e51bd7
@@ -7,7 +7,7 @@ jobs:
7
7
  check:
8
8
  runs-on: ubuntu-latest
9
9
  steps:
10
- - uses: actions/checkout@v3
10
+ - uses: actions/checkout@v4
11
11
  with:
12
12
  fetch-depth: 0
13
13
 
@@ -9,9 +9,11 @@ jobs:
9
9
  fail-fast: false
10
10
  matrix:
11
11
  ruby-version: ['2.7', '3.0', '3.1', '3.2', 'head']
12
+ env:
13
+ NO_STEEP: "Because we need to test on versions that are older than steep supports"
12
14
 
13
15
  steps:
14
- - uses: actions/checkout@v3
16
+ - uses: actions/checkout@v4
15
17
 
16
18
  - name: Set up ruby
17
19
  uses: ruby/setup-ruby@v1
@@ -0,0 +1,18 @@
1
+ name: Steep Type Checking
2
+
3
+ on: push
4
+
5
+ jobs:
6
+ check:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v4
10
+
11
+ - name: Set up ruby
12
+ uses: ruby/setup-ruby@v1
13
+ with:
14
+ ruby-version: 3.3
15
+ bundler-cache: true
16
+
17
+ - name: Run Steep
18
+ run: bundle exec steep check
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## Release 1.5.0
4
+
5
+ * Add rbs/steep, and enforce types in CI (.rbs file is exported as part
6
+ of the gem) (#28, resolves #21)
7
+ * Support env-based customization of the truthy/falsey strings used by
8
+ `ENV.boolean` (#29, resolves #27)
9
+ * Update github workflows to use checkout@v4 (which should have no real
10
+ impact on us, aside from staying current). (#30)
11
+ * Update README to explain `ENV.array`
12
+
3
13
  ## Release 1.4.0
4
14
 
5
15
  * Drop support for ruby 2.6
data/Gemfile CHANGED
@@ -1,3 +1,7 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
+
5
+ unless ENV["NO_STEEP"]
6
+ gem "steep", "~> 1.6.0"
7
+ end
data/README.md CHANGED
@@ -43,6 +43,7 @@ ENV.integer("MAX_THREAD_COUNT", default: 5)
43
43
  ENV.file_path("FILE_PATH", default: "/some/path", required: true)
44
44
  ENV.date("SCHEDULED_DATE", required: true, format: "%Y-%m-%d")
45
45
  ENV.date_time("RUN_AT", required: true, default: DateTime.now)
46
+ ENV.array("QUEUE_NAMES", of: :strings, required: false, default: ["high", "low"])
46
47
  ```
47
48
 
48
49
  Each of the supplied methods takes a positional parameter for the name of the
@@ -83,3 +84,17 @@ The available methods added to `ENV`:
83
84
  as a strptime format string (the `:unix` format is equivalent to the format
84
85
  string `"%s"`). It handles invalid or unparseable values like `ENV.date` does,
85
86
  in that they are treated as if not supplied.
87
+ * `array` - produces an array of strings, symbols, or integers, depending on the
88
+ value of the `of` parameter. You can specify the delimiter using a `delimiter`
89
+ parameter (it defaults to a comma).
90
+
91
+ ## Configuration
92
+
93
+ If you want to specify your own list of truthy/falsey strings, you can do that
94
+ by setting either or both of these environment variables, supplying comma-
95
+ separated (whitespace-free) strings:
96
+
97
+ * `ENVIRONMENT_HELPERS_TRUTHY_STRINGS` - the default value used is
98
+ `true,yes,on,enabled,enable,allow,t,y,1,ok,okay`
99
+ * `ENVIRONMENT_HELPERS_FALSEY_STRINGS` - the default value used is
100
+ `false,no,off,disabled,disable,deny,f,n,0,nope`
data/Steepfile ADDED
@@ -0,0 +1,17 @@
1
+ D = Steep::Diagnostic
2
+
3
+ target :lib do
4
+ signature "sig"
5
+ check "lib"
6
+ configure_code_diagnostics(D::Ruby.default)
7
+ library "pathname"
8
+ library "date"
9
+ end
10
+
11
+ target :test do
12
+ signature "sig"
13
+ check "test"
14
+ configure_code_diagnostics(D::Ruby.default)
15
+ library "pathname"
16
+ library "date"
17
+ end
@@ -1,7 +1,5 @@
1
1
  module EnvironmentHelpers
2
2
  module BooleanHelpers
3
- TRUTHY_STRINGS = %w[true yes on enabled enable allow t y 1 ok okay].to_set
4
- FALSEY_STRINGS = %w[false no off disabled disable deny f n 0 nope].to_set
5
3
  BOOLEAN_VALUES = [true, false, nil].to_set
6
4
 
7
5
  def boolean(name, default: nil, required: false)
@@ -19,12 +17,26 @@ module EnvironmentHelpers
19
17
 
20
18
  def truthy_text?(text)
21
19
  return false if text.nil?
22
- TRUTHY_STRINGS.include?(text.strip.downcase)
20
+ truthy_strings.include?(text.strip.downcase)
23
21
  end
24
22
 
25
23
  def falsey_text?(text)
26
24
  return false if text.nil?
27
- FALSEY_STRINGS.include?(text.strip.downcase)
25
+ falsey_strings.include?(text.strip.downcase)
26
+ end
27
+
28
+ def truthy_strings
29
+ @_truthy_strings ||=
30
+ ENV.fetch("ENVIRONMENT_HELPERS_TRUTHY_STRINGS", "true,yes,on,enabled,enable,allow,t,y,1,ok,okay")
31
+ .split(",")
32
+ .to_set
33
+ end
34
+
35
+ def falsey_strings
36
+ @_falsey_strings ||=
37
+ ENV.fetch("ENVIRONMENT_HELPERS_FALSEY_STRINGS", "false,no,off,disabled,disable,deny,f,n,0,nope")
38
+ .split(",")
39
+ .to_set
28
40
  end
29
41
  end
30
42
  end
@@ -2,6 +2,18 @@ module EnvironmentHelpers
2
2
  module EnumerableHelpers
3
3
  VALID_TYPES = %i[strings symbols integers]
4
4
 
5
+ TYPE_HANDLERS = {
6
+ integers: :to_i,
7
+ strings: :to_s,
8
+ symbols: :to_sym
9
+ }
10
+
11
+ TYPE_MAP = {
12
+ integers: Integer,
13
+ strings: String,
14
+ symbols: Symbol
15
+ }
16
+
5
17
  def array(key, of: :strings, delimiter: ",", default: nil, required: false)
6
18
  check_default_type(:array, default, Array)
7
19
  check_valid_data_type!(of)
@@ -22,23 +34,12 @@ module EnvironmentHelpers
22
34
  end
23
35
 
24
36
  def check_default_data_types!(default, type)
37
+ return if default.nil?
25
38
  invalid = Array(default).reject { |val| val.is_a? TYPE_MAP[type] }
26
39
 
27
40
  unless invalid.empty?
28
41
  fail(BadDefault, "Default array contains values not of type `#{type}': #{invalid.join(", ")}")
29
42
  end
30
43
  end
31
-
32
- TYPE_HANDLERS = {
33
- integers: :to_i,
34
- strings: :to_s,
35
- symbols: :to_sym
36
- }
37
-
38
- TYPE_MAP = {
39
- integers: Integer,
40
- strings: String,
41
- symbols: Symbol
42
- }
43
44
  end
44
45
  end
@@ -6,7 +6,7 @@ module EnvironmentHelpers
6
6
  check_range_endpoint(:integer_range, default.end) if default
7
7
 
8
8
  text = fetch_value(name, required: required)
9
- range = parse_range_from(text)
9
+ range = text ? parse_range_from(text) : nil
10
10
  return range if range
11
11
  return default unless required
12
12
  fail(InvalidRangeText, "Required Integer Range environment variable #{name} had inappropriate content '#{text}'")
@@ -6,7 +6,7 @@ module EnvironmentHelpers
6
6
 
7
7
  def symbol(name, default: nil, required: false)
8
8
  check_default_type(:symbol, default, Symbol)
9
- string(name, default: default, required: required)&.to_sym
9
+ string(name, default: default&.to_s, required: required)&.to_sym
10
10
  end
11
11
  end
12
12
  end
@@ -1,3 +1,3 @@
1
1
  module EnvironmentHelpers
2
- VERSION = "1.4.0"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -2,14 +2,14 @@
2
2
  # And we're compatible back to 2.6
3
3
  require "set" # rubocop:disable Lint/RedundantRequireStatement
4
4
 
5
- require_relative "./environment_helpers/access_helpers"
6
- require_relative "./environment_helpers/string_helpers"
7
- require_relative "./environment_helpers/boolean_helpers"
8
- require_relative "./environment_helpers/range_helpers"
9
- require_relative "./environment_helpers/numeric_helpers"
10
- require_relative "./environment_helpers/file_helpers"
11
- require_relative "./environment_helpers/datetime_helpers"
12
- require_relative "./environment_helpers/enumerable_helpers"
5
+ require_relative "environment_helpers/access_helpers"
6
+ require_relative "environment_helpers/string_helpers"
7
+ require_relative "environment_helpers/boolean_helpers"
8
+ require_relative "environment_helpers/range_helpers"
9
+ require_relative "environment_helpers/numeric_helpers"
10
+ require_relative "environment_helpers/file_helpers"
11
+ require_relative "environment_helpers/datetime_helpers"
12
+ require_relative "environment_helpers/enumerable_helpers"
13
13
 
14
14
  module EnvironmentHelpers
15
15
  Error = Class.new(::StandardError)
@@ -0,0 +1,96 @@
1
+ module EnvironmentHelpers
2
+ VERSION: String
3
+
4
+ class Error < StandardError
5
+ end
6
+
7
+ class MissingVariableError < Error
8
+ end
9
+
10
+ class BadDefault < Error
11
+ end
12
+
13
+ class BadFormat < Error
14
+ end
15
+
16
+ class InvalidType < Error
17
+ end
18
+
19
+ class InvalidValue < Error
20
+ end
21
+
22
+ class InvalidBooleanText < InvalidValue
23
+ end
24
+
25
+ class InvalidRangeText < InvalidValue
26
+ end
27
+
28
+ class InvalidIntegerText < InvalidValue
29
+ end
30
+
31
+ class InvalidDateText < InvalidValue
32
+ end
33
+
34
+ class InvalidDateTimeText < InvalidValue
35
+ end
36
+
37
+ module AccessHelpers
38
+ # (Actually provided by ENV, which these are all extended onto)
39
+ def fetch: (String name) -> String?
40
+ | (String name, String? default) -> String?
41
+
42
+ private def fetch_value: (String name, required: bool) -> String?
43
+ private def check_default_type: (String | Symbol context, untyped value, *Class types) -> void
44
+ private def check_default_value: (String | Symbol context, untyped value, allow: Enumerable[untyped]) -> void
45
+ end
46
+
47
+ module FileHelpers : AccessHelpers
48
+ def file_path: (String name, default: String?, required: bool) -> Pathname?
49
+ end
50
+
51
+ module DatetimeHelpers : AccessHelpers
52
+ type date_time_result = DateTime? | Time?
53
+ def date: (String name, format: String, default: Date?, required: bool) -> Date?
54
+ def date_time: (String name, format: String | Symbol, default: date_time_result, required: bool) -> date_time_result
55
+ private def parse_date_from: (String? text, format: String) -> Date?
56
+ private def parse_date_time_from: (String? text, format: String | Symbol) -> DateTime?
57
+ private def iso8601_date_time: (String) -> DateTime?
58
+ private def unix_date_time: (String) -> DateTime?
59
+ private def strptime_date_time: (String, format: String) -> DateTime?
60
+ end
61
+
62
+ module NumericHelpers : AccessHelpers
63
+ def integer: (String name, default: Integer?, required: bool) -> Integer?
64
+ end
65
+
66
+ module RangeHelpers : AccessHelpers
67
+ def integer_range: (String name, default: Range[Integer]?, required: bool) -> Range[Integer]?
68
+ private def check_range_endpoint: (String | Symbol context, untyped value) -> void
69
+ private def parse_range_bound_from: (String?) -> Integer?
70
+ private def parse_range_from: (String) -> Range[Integer]?
71
+ end
72
+
73
+ module BooleanHelpers : AccessHelpers
74
+ BOOLEAN_VALUES: Set[bool]
75
+ def boolean: (String name, default: bool?, required: bool) -> boolish
76
+ private def truthy_text?: (String?) -> boolish
77
+ private def falsey_text?: (String?) -> boolish
78
+ private def truthy_strings: () -> Set[String]
79
+ private def falsey_strings: () -> Set[String]
80
+ end
81
+
82
+ module EnumerableHelpers : AccessHelpers
83
+ VALID_TYPES: Array[Symbol]
84
+ TYPE_HANDLERS: Hash[Symbol, Symbol]
85
+ TYPE_MAP: Hash[Symbol, Class]
86
+ type arrayable = Integer | String | Symbol
87
+ def array: (String key, of: Symbol, delimiter: String, default: Array[arrayable]?, required: bool) -> Array[arrayable]?
88
+ private def check_valid_data_type!: (Symbol) -> void
89
+ private def check_default_data_types!: (Array[arrayable]?, Symbol) -> void
90
+ end
91
+
92
+ module StringHelpers : AccessHelpers
93
+ def string: (String name, default: String?, required: bool) -> String?
94
+ def symbol: (String name, default: Symbol?, required: bool) -> Symbol?
95
+ end
96
+ end
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.4.0
4
+ version: 1.5.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-06-14 00:00:00.000000000 Z
11
+ date: 2023-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -133,6 +133,7 @@ extra_rdoc_files: []
133
133
  files:
134
134
  - ".github/workflows/quality.yml"
135
135
  - ".github/workflows/rspec.yml"
136
+ - ".github/workflows/type_check.yml"
136
137
  - ".gitignore"
137
138
  - ".mdl_rules.rb"
138
139
  - ".mdlrc"
@@ -144,6 +145,7 @@ files:
144
145
  - Gemfile
145
146
  - LICENSE
146
147
  - README.md
148
+ - Steepfile
147
149
  - environment_helpers.gemspec
148
150
  - lib/environment_helpers.rb
149
151
  - lib/environment_helpers/access_helpers.rb
@@ -155,6 +157,7 @@ files:
155
157
  - lib/environment_helpers/range_helpers.rb
156
158
  - lib/environment_helpers/string_helpers.rb
157
159
  - lib/environment_helpers/version.rb
160
+ - sig/environment_helpers.rbs
158
161
  homepage: https://github.com/nevinera/environment_helpers
159
162
  licenses:
160
163
  - MIT