gonfic 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -1
- data/README.md +14 -1
- data/lib/gonfic/env_extractor.rb +40 -0
- data/lib/gonfic/version.rb +1 -1
- data/lib/gonfic.rb +9 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cdf1477e7d76fc97fe5ee9a224079275e563e90c22d837346c0a6ba43f0a4841
|
4
|
+
data.tar.gz: 8f8121dfac2bc667c067008352763f1a5a276a96d1817180d0f2c4ae6d3f1e36
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 564f42cc15d5a2a7055c8a9d58e960bdf3206b5f7b340c908c45ab1af243f02ccf8ab8444345d36901e998f83d9c7e22de418c24d099ce1ae0a4aef82ae19946
|
7
|
+
data.tar.gz: 1910650dfe238d8d4e01db9dffc03e9f4edb0f7ac9385b460cee285dedc6fbcb8471621dd2417316f547a1bbc26030cf2c8435dbc2dae7b186d633f74f58a04b
|
data/.rubocop.yml
CHANGED
@@ -21,6 +21,10 @@ Layout/IndentationWidth: # weird setting to enable other Layout cops to work wit
|
|
21
21
|
Width: 1
|
22
22
|
# tab country ends
|
23
23
|
|
24
|
+
Layout/MultilineMethodCallIndentation:
|
25
|
+
Enabled: true
|
26
|
+
EnforcedStyle: indented
|
27
|
+
|
24
28
|
Layout/SpaceBeforeBlockBraces:
|
25
29
|
Enabled: true
|
26
30
|
EnforcedStyle: no_space
|
@@ -47,4 +51,3 @@ Style/TrailingCommaInHashLiteral:
|
|
47
51
|
Layout/SpaceInsideHashLiteralBraces:
|
48
52
|
Enabled: true
|
49
53
|
EnforcedStyle: no_space
|
50
|
-
|
data/README.md
CHANGED
@@ -43,6 +43,8 @@ This one is actually kind of cool, in its minimalism - and intended for similar
|
|
43
43
|
|
44
44
|
## Advanced scenarios
|
45
45
|
|
46
|
+
As 90+% of the functionality is configured by different ways of calling `Gonfic.new`, this section delves into possible variants you might want to consider - to fine-tune `Gonfic` to your needs.
|
47
|
+
|
46
48
|
### Deferring dangerous operations
|
47
49
|
|
48
50
|
The `Gonfic.new` call will peform file I/O and other operations, which can potentially fail and raise errors. Doing things like that is generally frowned upon in initializers and immediately when files are `require`d - so `CONFIG = Gonfic.new` might not be the best practice ever, succinct as it is. A more prudent approach would be:
|
@@ -56,6 +58,16 @@ The `Gonfic.new` call will peform file I/O and other operations, which can poten
|
|
56
58
|
In this way you are lazily deferring the "dangerous" operations until first usage, then memoizing.
|
57
59
|
Additionally, with access to your config via a method that's easy to stub, your test suite will be happy :)
|
58
60
|
|
61
|
+
### Loading configuration from ENV
|
62
|
+
|
63
|
+
If you provide `filename:`, `Gonfic.new` will also check `ENV` for variables it considers relevant.
|
64
|
+
See `EnvExtractor.env_prefix_from_filename` for what it will scan - either in source code, or in `bin/console`.
|
65
|
+
The pattern, in general, is that `.tool-name` becomes `TOOL_NAME_*` - and any variables matching the pattern, and present in `defaults:` (again, converted to upcase with underscores), will be merged into resulting configuration.
|
66
|
+
|
67
|
+
To disable this behavior, set `env_variable_prefix:` to falsey (`nil` will do).
|
68
|
+
|
69
|
+
You can also override the prefix. Underscore at the end is optional.
|
70
|
+
|
59
71
|
## Source code
|
60
72
|
|
61
73
|
https://gitlab.com/tanstaafl/gonfic
|
@@ -73,6 +85,7 @@ This will run tests (Minitest) and linter (Rubocop, custom config, see `.rubocop
|
|
73
85
|
|
74
86
|
Note: the repo does not force the bundle to be vendorized,
|
75
87
|
but should work fine if you set `BUNDLE_PATH: "vendor"` in your `~/.bundle/config`.
|
88
|
+
Hint, hint ;)
|
76
89
|
|
77
90
|
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
78
91
|
|
@@ -85,7 +98,7 @@ Bug reports and merge requests are welcome on GitLab at https://gitlab.com/tanst
|
|
85
98
|
## Name
|
86
99
|
|
87
100
|
Yeah, it's not great. As in: not obvious.
|
88
|
-
If you
|
101
|
+
If you have an idea how to make it better:
|
89
102
|
|
90
103
|
1. Go to https://rubygems.org/gems?letter=C
|
91
104
|
2. Binary-search to `conf*` (at this stage you might begin to appreciate how hard naming is in this particular case, I hope).
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Gonfic
|
2
|
+
# extracts config options from env vars
|
3
|
+
class EnvExtractor
|
4
|
+
def self.extract_variables_from_env(prefix:, defaults:)
|
5
|
+
extracted = {}
|
6
|
+
|
7
|
+
defaults.each_key do |key_name|
|
8
|
+
extract_variable(prefix: prefix, key_name: key_name, into: extracted)
|
9
|
+
end
|
10
|
+
|
11
|
+
extracted
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.env_prefix_from_filename(filename)
|
15
|
+
return nil unless filename
|
16
|
+
|
17
|
+
converted = upcase_and_simplify(File.basename(filename, '.*'))
|
18
|
+
|
19
|
+
"#{converted}_"
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.upcase_and_simplify(sym)
|
23
|
+
sym
|
24
|
+
.to_s
|
25
|
+
.gsub(/[^\w]+/, '_')
|
26
|
+
.delete_prefix('_')
|
27
|
+
.delete_suffix('_')
|
28
|
+
.upcase
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.extract_variable(prefix:, key_name:, into:)
|
32
|
+
env_var_name = prefix + upcase_and_simplify(key_name)
|
33
|
+
into[key_name] = env[env_var_name] if env.key?(env_var_name)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.env
|
37
|
+
ENV
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/gonfic/version.rb
CHANGED
data/lib/gonfic.rb
CHANGED
@@ -2,15 +2,23 @@
|
|
2
2
|
|
3
3
|
require_relative 'gonfic/version'
|
4
4
|
require_relative 'gonfic/config'
|
5
|
+
require_relative 'gonfic/env_extractor'
|
5
6
|
|
6
7
|
# external interface to creating a Config with data loaded from files
|
7
8
|
module Gonfic
|
8
9
|
FILE_LOADER = ->(filename){ YAML.safe_load(File.read(filename)) }
|
9
10
|
|
10
|
-
def self.new(
|
11
|
+
def self.new(
|
12
|
+
defaults: {},
|
13
|
+
filename: nil, file_loader: FILE_LOADER,
|
14
|
+
env_variable_prefix: EnvExtractor.env_prefix_from_filename(filename)
|
15
|
+
)
|
11
16
|
config = Config.new(defaults)
|
12
17
|
config.merge!(load_configuration_file("~/#{filename}", file_loader)) if filename
|
13
18
|
config.merge!(load_configuration_file("./#{filename}", file_loader)) if filename
|
19
|
+
if env_variable_prefix
|
20
|
+
config.merge!(EnvExtractor.extract_variables_from_env(prefix: env_variable_prefix, defaults: defaults))
|
21
|
+
end
|
14
22
|
config
|
15
23
|
end
|
16
24
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gonfic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emil Chludziński
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-07-
|
11
|
+
date: 2023-07-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -41,6 +41,7 @@ files:
|
|
41
41
|
- gonfic.gemspec
|
42
42
|
- lib/gonfic.rb
|
43
43
|
- lib/gonfic/config.rb
|
44
|
+
- lib/gonfic/env_extractor.rb
|
44
45
|
- lib/gonfic/version.rb
|
45
46
|
homepage: https://gitlab.com/tanstaafl/gonfic
|
46
47
|
licenses:
|