loadout 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b53dcf7016fe7d052fa02cfa1b4f7afba43f1deb7fc3bb4dcd0a87d065394174
4
- data.tar.gz: 656914809769c23d3d4822aca33eb8e276c8cfbda0bf409aa64d9ea87c3223df
3
+ metadata.gz: 6c35aa7a1b51f1a26726c71f263a05e57c3014944d54446e405aea8132885245
4
+ data.tar.gz: c69c4f2297f31499db694247daf03428666b31b1d728ff77638ef2d2dac46d36
5
5
  SHA512:
6
- metadata.gz: ab1768597fa1d238cdfb15f36c3358dec842d4a8f045e1b0d87ff3ce71a50984ffb53ce8364d6ab432a53e6028e3e0f8d0693325e3094fcacd6a1912c027eb5c
7
- data.tar.gz: 5b8ca42f8a44182e9007c3372f9ac470d0abd632d202dd54da71f22bf909a6b00def14b97c13d6d917b308c6123fde5bfd959b8d9226a953e6ae911f57df037c
6
+ metadata.gz: 68cd692af87709c29bcd547119b0930fae89f28e02f79b37b25babf6214823c07ed5f7edf4e4dd02bb480b815f660558b79fb9271a379258f3b54a4cdc0f3dbf
7
+ data.tar.gz: f9c5abe54da9dfc8fcd9e7e978fb0a90f38bd4a3b6cba675022fb93ee5f32239d84e87bcb1195202b7b691d8ab071e116991d620f09bd162736c38fe2d3c8b1c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2024-08-11
4
+
5
+ - Always convert ENV values to string
6
+ - Consider "" an invalid ENV value for list
7
+ - Minor internal refactors
8
+
3
9
  ## [0.1.0] - 2024-08-11
4
10
 
5
11
  - Initial release
data/README.md CHANGED
@@ -55,18 +55,36 @@ If bundler is not being used to manage dependencies, install the gem by executin
55
55
  config.key = cred(:key_name)
56
56
  ```
57
57
 
58
+ If you don't set this credential, you will get an error:
59
+
60
+ ```ruby
61
+ Loadout::MissingConfigError: required credential (key_name) is not set
62
+ ```
63
+
58
64
  3. Or from ENV:
59
65
 
60
66
  ```ruby
61
67
  config.key = env(:key_name)
62
68
  ```
63
69
 
64
- 4. Or whichever one is found first:
70
+ If you don't set this env, you will get an error:
71
+
72
+ ```ruby
73
+ Loadout::MissingConfigError: required environment variable (KEY_NAME) is not set
74
+ ```
75
+
76
+ 4. Look up ENV, then credentials, then fail:
65
77
 
66
78
  ```ruby
67
79
  config.key = env.cred(:key_name)
68
80
  ```
69
81
 
82
+ If neither are set, you will get an error:
83
+
84
+ ```ruby
85
+ Loadout::MissingConfigError: required environment variable (KEY_NAME) or credential (key_name) is not set
86
+ ```
87
+
70
88
  5. Or the other way around:
71
89
 
72
90
  ```ruby
@@ -98,6 +116,12 @@ If bundler is not being used to manage dependencies, install the gem by executin
98
116
  config.some_flag = bool.cred.env(:key_name)
99
117
  ```
100
118
 
119
+ If you set an invalid value, you will get an error:
120
+
121
+ ```ruby
122
+ Loadout::InvalidConfigError: invalid value for bool (`value`) in KEY_NAME
123
+ ```
124
+
101
125
  Note: because credentials come from YAML, they don't need to be parsed. Only ENV values are parsed.
102
126
 
103
127
  9. Integers and floats are also supported:
@@ -117,7 +141,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
117
141
  11. You can set your own list separator (string or regex):
118
142
 
119
143
  ```ruby
120
- # Parses 'foo0bar0baz' as ['foo', 'bar', 'baz']
144
+ # Parses 'foo0bar0baz' into ['foo', 'bar', 'baz']
121
145
  config.some_list = list('0').env(:key_name)
122
146
  ```
123
147
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Loadout
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/loadout.rb CHANGED
@@ -32,26 +32,26 @@ module Loadout
32
32
 
33
33
  class Config
34
34
  protected attr_writer :type
35
- protected attr_reader :lookup_stack
35
+ protected attr_reader :lookup_list
36
36
 
37
37
  def initialize(env, creds)
38
38
  @env = env
39
39
  @creds = creds
40
40
  @type = nil
41
41
  @prefix_stack = []
42
- @lookup_stack = Set[]
42
+ @lookup_list = Set[]
43
43
  @prefix_default = NONE
44
44
  end
45
45
 
46
46
  def env(*keys, &default)
47
- return dup.tap { _1.lookup_stack << :env } if keys.empty?
48
- @lookup_stack << :env
47
+ return dup.tap { _1.lookup_list << :env } if keys.empty?
48
+ @lookup_list << :env
49
49
  lookup(keys, &default)
50
50
  end
51
51
 
52
52
  def cred(*keys, &default)
53
- return dup.tap { _1.lookup_stack << :cred } if keys.empty?
54
- @lookup_stack << :cred
53
+ return dup.tap { _1.lookup_list << :cred } if keys.empty?
54
+ @lookup_list << :cred
55
55
  lookup(keys, &default)
56
56
  end
57
57
 
@@ -67,10 +67,9 @@ module Loadout
67
67
  def list(sep = DEFAULT_LIST_SEP) = dup.tap { _1.type = [:list, sep] }
68
68
 
69
69
  def initialize_dup(other)
70
- @creds = other.instance_variable_get(:@creds)
71
- @type = other.instance_variable_get(:@type).dup
72
- @prefix_stack = other.instance_variable_get(:@prefix_stack).dup
73
- @lookup_stack = other.instance_variable_get(:@lookup_stack).dup
70
+ @type = other.instance_variable_get(:@type).dup
71
+ @prefix_stack = other.instance_variable_get(:@prefix_stack).dup
72
+ @lookup_list = other.instance_variable_get(:@lookup_list).dup
74
73
 
75
74
  unless other.instance_variable_get(:@prefix_default).equal?(NONE)
76
75
  @prefix_default = other.instance_variable_get(:@prefix_default).dup
@@ -85,7 +84,7 @@ module Loadout
85
84
  value = NONE
86
85
  keys = @prefix_stack.flatten + keys
87
86
 
88
- @lookup_stack.each do |source|
87
+ @lookup_list.each do |source|
89
88
  value =
90
89
  case source
91
90
  when :cred; lookup_cred(keys)
@@ -99,7 +98,7 @@ module Loadout
99
98
  return @prefix_default.call unless @prefix_default.equal?(NONE)
100
99
  raise_missing(keys)
101
100
  ensure
102
- @lookup_stack.clear
101
+ @lookup_list.clear
103
102
  end
104
103
 
105
104
  def lookup_cred(keys)
@@ -116,41 +115,51 @@ module Loadout
116
115
 
117
116
  def coerce(key, value)
118
117
  case @type
119
- in :bool
120
- value = value.to_s
121
- return false if value == ''
122
- return false if %w[0 n no f false].include?(value.downcase)
123
- return true if %w[1 y yes t true].include?(value.downcase)
124
- raise_invalid :bool, key, value
125
- in :int; enhance_exception(:int, key, value) { Integer(value) }
126
- in :float; enhance_exception(:float, key, value) { Float(value) }
127
- in :list, sep; value.split(sep)
118
+ in :bool; parse_bool(key, value)
119
+ in :int; parse_int(key, value)
120
+ in :float; parse_float(key, value)
121
+ in :list, sep; parse_list(key, value, sep)
128
122
  else; value
129
123
  end
130
124
  end
131
125
 
132
- def enhance_exception(type, key, val)
133
- yield
126
+ def parse_bool(key, value)
127
+ value = value.to_s
128
+ return false if value == ''
129
+ return false if %w[0 n no f false].include?(value.downcase)
130
+ return true if %w[1 y yes t true].include?(value.downcase)
131
+ raise_invalid :bool, key, value
132
+ end
133
+
134
+ def parse_int(key, val)
135
+ Integer(val.to_s)
134
136
  rescue ArgumentError
135
- raise_invalid(type, key, val)
137
+ raise_invalid :int, key, val
138
+ end
139
+
140
+ def parse_float(key, val)
141
+ Float(val.to_s)
142
+ rescue ArgumentError
143
+ raise_invalid :float, key, val
144
+ end
145
+
146
+ def parse_list(key, val, sep)
147
+ val = val.to_s
148
+ raise_invalid(:list, key, val) if val == ''
149
+ val.split(sep)
136
150
  end
137
151
 
138
152
  def raise_missing(keys)
139
- src = []
140
- val = []
153
+ pairs = []
141
154
 
142
- @lookup_stack.each do |source|
155
+ @lookup_list.each do |source|
143
156
  case source
144
- when :cred
145
- src << "credential"
146
- val << keys.join('.')
147
- when :env
148
- src << "environment variable"
149
- val << keys.join('_').upcase
157
+ when :cred; pairs << ["credential", keys.join('.')]
158
+ when :env; pairs << ["environment variable", keys.join('_').upcase]
150
159
  end
151
160
  end
152
161
 
153
- msg = src.zip(val).map { |s, v| "#{s} (#{v})" }.join(' or ')
162
+ msg = pairs.map { |s, v| "#{s} (#{v})" }.join(' or ')
154
163
  raise MissingConfigError, "required #{msg} is not set"
155
164
  end
156
165
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loadout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max Chernyak
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
50
  requirements: []
51
- rubygems_version: 3.2.33
51
+ rubygems_version: 3.5.17
52
52
  signing_key:
53
53
  specification_version: 4
54
54
  summary: Rails configuration helpers