yiffspace-config 0.0.1 → 0.1.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 +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +15 -0
- data/Rakefile +6 -0
- data/lib/yiffspace/config/builder.rb +62 -21
- data/lib/yiffspace/config/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f89628b8dfcca94addceb7086a718d5fc00597bd434a98a983c351bdc9d7e528
|
|
4
|
+
data.tar.gz: 9c3bd21964bbcd311f5c96844ec7fe759dff03702256ee3a3908c29f529b023d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ad8a6a0a257d55babbf873ed8d588bd0682ab31ec3f1f8521cb3b6beb247abefc192c33a50f47ac080ccb075c8a83d92c64a8ff95b18a6def2af1c3409fd4a15
|
|
7
|
+
data.tar.gz: 81a2fa52d712af9a0b085edb1002a65d06533c32625aa49eee3f5c8b66d7f37b9b5806ffdc681b1dda3904541d7815317ac2115cb99cf25fdeac9df5867fbdfe
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
+
## Unreleased
|
|
2
|
+
|
|
3
|
+
- Added `Builder["PREFIX"]`, which returns one builder instance per env prefix,
|
|
4
|
+
so several configs can live side by side.
|
|
5
|
+
- **Breaking:** builder state is now per class instead of shared by every
|
|
6
|
+
subclass, so two `Builder` subclasses no longer see each other's options or
|
|
7
|
+
`env_name`. Subclasses still inherit options defined on their parent.
|
|
8
|
+
- Added `<prefix>_<NAME>_FILE` support: when `<prefix>_<NAME>` is unset, the
|
|
9
|
+
file at that path is read (trailing newline removed) and revived as usual.
|
|
10
|
+
Disable per option with `file: false`; `env: false` disables it too.
|
|
11
|
+
- `env: false` is now actually honored - the option is never read from ENV.
|
|
12
|
+
- Fixed `subconfig` crashing when ActiveSupport's core extensions weren't
|
|
13
|
+
already loaded (e.g. outside Rails).
|
|
14
|
+
- Fixed `ensure_required_set!` raising `NotImplementedError` instead of
|
|
15
|
+
listing the missing options when a required option had no default.
|
|
16
|
+
|
|
1
17
|
## 0.0.1
|
|
2
18
|
|
|
3
19
|
- Initial release, extracted from the `yiffspace` gem's `YiffSpace::Config::Builder`.
|
data/README.md
CHANGED
|
@@ -27,6 +27,21 @@ class Config < YiffSpace::Config::Builder
|
|
|
27
27
|
end
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
+
Or, for a builder per env prefix (each prefix always gives back the same
|
|
31
|
+
instance, with its own options):
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
FemboyFans = YiffSpace::Config::Builder["FEMBOYFANS"]
|
|
35
|
+
FemboyFans.config(:some_value, required: true)
|
|
36
|
+
FemboyFans.some_value # from FEMBOYFANS_SOME_VALUE
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
A subclass uses the `CONFIG` prefix unless it sets `self.env_name = "..."`.
|
|
40
|
+
|
|
41
|
+
Values come from `<PREFIX>_<NAME>`, or from the file at `<PREFIX>_<NAME>_FILE`
|
|
42
|
+
(e.g. `/run/secrets/some_value`) if that's unset. Pass `file: false` to only
|
|
43
|
+
allow the plain variable, or `env: false` to ignore ENV entirely.
|
|
44
|
+
|
|
30
45
|
See `YiffSpace::Config::Builder` for the full API.
|
|
31
46
|
|
|
32
47
|
## Contributing
|
data/Rakefile
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require("bundler/gem_tasks")
|
|
4
|
+
|
|
5
|
+
# CI releases from an already-tagged, already-pushed commit checked out at a detached HEAD, so
|
|
6
|
+
# the default release task's git tag/push step has nothing to do and fails trying to push a
|
|
7
|
+
# branch ref that does not exist in that checkout.
|
|
8
|
+
Rake::Task["release"].clear
|
|
9
|
+
task("release" => "release:rubygem_push")
|
|
@@ -1,24 +1,55 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require("singleton")
|
|
4
|
-
require("
|
|
5
|
-
require("active_support/
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
require("concurrent/map")
|
|
5
|
+
require("active_support/core_ext/class/attribute")
|
|
6
|
+
require("active_support/core_ext/object/deep_dup")
|
|
7
|
+
require("active_support/core_ext/hash/indifferent_access")
|
|
8
|
+
require("active_support/core_ext/object/blank")
|
|
9
|
+
|
|
10
|
+
# This is intended to only be used externally, do not use it within this project.
|
|
11
|
+
# Either subclass it (`class Config < Builder`, prefix set with `self.env_name =`), or use
|
|
12
|
+
# `Builder["PREFIX"]` for a separate builder per env prefix.
|
|
8
13
|
module YiffSpace
|
|
9
14
|
module Config
|
|
10
15
|
class Builder
|
|
11
16
|
include(Singleton)
|
|
12
17
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
STATE = %i[list env_set file_set unset required reviver_map subconfigs].freeze
|
|
19
|
+
|
|
20
|
+
class_attribute(:list, default: [], instance_writer: false)
|
|
21
|
+
class_attribute(:env_set, default: ActiveSupport::HashWithIndifferentAccess.new, instance_writer: false)
|
|
22
|
+
class_attribute(:file_set, default: ActiveSupport::HashWithIndifferentAccess.new, instance_writer: false)
|
|
23
|
+
class_attribute(:unset, default: [], instance_writer: false)
|
|
24
|
+
class_attribute(:required, default: [], instance_writer: false)
|
|
25
|
+
class_attribute(:reviver_map, default: ActiveSupport::HashWithIndifferentAccess.new, instance_writer: false)
|
|
26
|
+
class_attribute(:subconfigs, default: ActiveSupport::HashWithIndifferentAccess.new { |h, k| h[k] = {} }, instance_writer: false)
|
|
27
|
+
class_attribute(:env_name, default: "CONFIG", instance_writer: false)
|
|
20
28
|
|
|
21
|
-
|
|
29
|
+
BUILDERS = Concurrent::Map.new
|
|
30
|
+
|
|
31
|
+
# Each subclass gets its own copy, so builders don't share options
|
|
32
|
+
def self.inherited(subclass)
|
|
33
|
+
super
|
|
34
|
+
STATE.each { |key| subclass.public_send("#{key}=", public_send(key).deep_dup) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# One builder per prefix, always the same instance
|
|
38
|
+
def self.[](prefix)
|
|
39
|
+
prefix = prefix.to_s
|
|
40
|
+
BUILDERS.compute_if_absent(prefix) do
|
|
41
|
+
Class.new(Builder) { self.env_name = prefix }.instance.extend(InstanceDSL)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Lets `Builder["PREFIX"].config(...)` work, since it hands out an instance rather than a class
|
|
46
|
+
module InstanceDSL
|
|
47
|
+
%i[config remove_config reviver subconfig ensure_required_set!].each do |method|
|
|
48
|
+
define_method(method) { |*args, **kwargs, &block| self.class.public_send(method, *args, **kwargs, &block) }
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.config(name, type = :string, env: true, file: true, required: false, blank: false, &block)
|
|
22
53
|
name = name.to_sym
|
|
23
54
|
remove_config(name) if list.include?(name)
|
|
24
55
|
list << name
|
|
@@ -36,7 +67,8 @@ module YiffSpace
|
|
|
36
67
|
end
|
|
37
68
|
end
|
|
38
69
|
reviver(name, type) if type && type != :string
|
|
39
|
-
env_set[name]
|
|
70
|
+
env_set[name] = env
|
|
71
|
+
file_set[name] = env && file
|
|
40
72
|
end
|
|
41
73
|
|
|
42
74
|
def self.remove_config(name, reviver: false)
|
|
@@ -46,8 +78,9 @@ module YiffSpace
|
|
|
46
78
|
unset.delete(name)
|
|
47
79
|
reviver_map.delete(name) if reviver
|
|
48
80
|
env_set.delete(name)
|
|
49
|
-
|
|
50
|
-
remove_method(
|
|
81
|
+
file_set.delete(name)
|
|
82
|
+
remove_method(name) if method_defined?(name, false)
|
|
83
|
+
remove_method("#{name}?") if method_defined?("#{name}?", false)
|
|
51
84
|
end
|
|
52
85
|
|
|
53
86
|
def env_or_value(name, args, blank: false, &)
|
|
@@ -61,8 +94,16 @@ module YiffSpace
|
|
|
61
94
|
end
|
|
62
95
|
end
|
|
63
96
|
|
|
97
|
+
# <prefix>_<NAME> wins over <prefix>_<NAME>_FILE, which is read with its trailing newline removed
|
|
64
98
|
def env(name)
|
|
65
|
-
|
|
99
|
+
return nil unless env_set.fetch(name, true)
|
|
100
|
+
|
|
101
|
+
key = "#{self.class.env_name}_#{name.to_s.upcase}"
|
|
102
|
+
value = ENV.fetch(key, nil)
|
|
103
|
+
return value unless value.nil? && file_set.fetch(name, true)
|
|
104
|
+
|
|
105
|
+
path = ENV.fetch("#{key}_FILE", nil)
|
|
106
|
+
File.read(path).chomp if path.present?
|
|
66
107
|
end
|
|
67
108
|
|
|
68
109
|
def self.reviver(name, type = nil, &block)
|
|
@@ -105,8 +146,8 @@ module YiffSpace
|
|
|
105
146
|
@collected ||= []
|
|
106
147
|
end
|
|
107
148
|
|
|
108
|
-
def self.config(name, type = :string, env: true, required: false, blank: false, &block)
|
|
109
|
-
collected << [:config, name, type, env, required, blank, block]
|
|
149
|
+
def self.config(name, type = :string, env: true, file: true, required: false, blank: false, &block)
|
|
150
|
+
collected << [:config, name, type, env, file, required, blank, block]
|
|
110
151
|
end
|
|
111
152
|
|
|
112
153
|
def self.reviver(name, type = nil, &block)
|
|
@@ -124,7 +165,7 @@ module YiffSpace
|
|
|
124
165
|
case kind
|
|
125
166
|
when :config
|
|
126
167
|
full = (path + [name]).join("_").to_sym
|
|
127
|
-
config(full, args[0], env: args[1],
|
|
168
|
+
config(full, args[0], env: args[1], file: args[2], required: args[3], blank: args[4], &args[5])
|
|
128
169
|
when :reviver
|
|
129
170
|
full = (path + [name]).join("_").to_sym
|
|
130
171
|
reviver(full, args[0], &args[1])
|
|
@@ -139,7 +180,7 @@ module YiffSpace
|
|
|
139
180
|
required.each do |name|
|
|
140
181
|
value = begin
|
|
141
182
|
instance.public_send(name)
|
|
142
|
-
rescue StandardError
|
|
183
|
+
rescue StandardError, NotImplementedError
|
|
143
184
|
nil
|
|
144
185
|
end
|
|
145
186
|
unset << name if value.blank?
|
|
@@ -182,7 +223,7 @@ module YiffSpace
|
|
|
182
223
|
private
|
|
183
224
|
|
|
184
225
|
def subconfig_path?(path)
|
|
185
|
-
node =
|
|
226
|
+
node = @owner.subconfigs
|
|
186
227
|
path.each do |p|
|
|
187
228
|
return false unless node.key?(p)
|
|
188
229
|
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: yiffspace-config
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Donovan_DMC
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-
|
|
10
|
+
date: 2026-12-04 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: activesupport
|