gonfic 0.6.3 → 0.7.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/README.md +39 -9
- data/lib/gonfic/version.rb +1 -1
- data/lib/gonfic.rb +15 -10
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5540b5acbed6a77e6822f0aea120d20cac396ec86c7011edf7b665fe82e2f96b
|
4
|
+
data.tar.gz: e27a4fa9e41ae1696a14192835c5e31290a146dd3719708cf1bee481747c5c26
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5310196e4f2681a3f1b931101f7349782700b8b57666b517fc21625808f00272a41bf42a03f84692a4ca5caf0a1b05a58da249362bab242b14dad2565141b6e
|
7
|
+
data.tar.gz: 70f3c1f5fe26fd365657b9480265cf8efbe5f9ff7f4a6eb9959cab6d95355d1312bd7dd88a5ede693f0539ff1b0a7b35be59bbfb1532fe0c49f1c09369819dc0
|
data/README.md
CHANGED
@@ -8,10 +8,12 @@ Add `gem 'gonfic'` to your application's `Gemfile`, then run `bundle install`.
|
|
8
8
|
|
9
9
|
## Usage
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
```ruby
|
12
|
+
CONFIGURATION_FILENAME = '.my_tool' # YAML config file format, by default
|
13
|
+
CONFIGURATION_DEFAULTS = {a: 1, b: {c: 2}} # will deep_merge! using Hashie magic
|
14
|
+
CONFIG = Gonfic.new(filename: CONFIGURATION_FILENAME, defaults: CONFIGURATION_DEFAULTS)
|
15
|
+
puts CONFIG.b.c
|
16
|
+
```
|
15
17
|
|
16
18
|
## Design
|
17
19
|
|
@@ -49,11 +51,13 @@ As 90+% of the functionality is configured by different ways of calling `Gonfic.
|
|
49
51
|
|
50
52
|
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:
|
51
53
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
54
|
+
```ruby
|
55
|
+
module MyTool
|
56
|
+
def self.config
|
57
|
+
@config ||= Gonfic.new(...)
|
56
58
|
end
|
59
|
+
end
|
60
|
+
```
|
57
61
|
|
58
62
|
In this way you are lazily deferring the "dangerous" operations until first usage, then memoizing.
|
59
63
|
Additionally, with access to your config via a method that's easy to stub, your test suite will be happy :)
|
@@ -63,6 +67,32 @@ Additionally, with access to your config via a method that's easy to stub, your
|
|
63
67
|
By default, `Gonfic.new` will look for `filename:` in `~/` and `./` (merging them, if both are present).
|
64
68
|
If you want to load from other directories, just set the `directories:` parameter to your list.
|
65
69
|
|
70
|
+
### Providing list of full paths (when filenames differ)
|
71
|
+
|
72
|
+
Alternatively, you don't have to use the same `.my_tool` configuration filename, but instead support e.g. all of these:
|
73
|
+
|
74
|
+
```
|
75
|
+
~
|
76
|
+
├── .my_tool.yml
|
77
|
+
└── .config
|
78
|
+
├── my_tool.yml
|
79
|
+
└── my_tool
|
80
|
+
├── aliases.yml
|
81
|
+
└── secrets.yml
|
82
|
+
```
|
83
|
+
|
84
|
+
If you want these files to be merged, set the `file_paths:` parameter to a list of full paths including filenames:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
CONFIG = Gonfic.new(
|
88
|
+
file_paths: %w[~/.my_tool.yml ~/.config/my_tool.yml ~/.config/my_tool/aliases.yml ~/.config/my_tool/secrets.yml],
|
89
|
+
defaults: CONFIGURATION_DEFAULTS,
|
90
|
+
)
|
91
|
+
```
|
92
|
+
|
93
|
+
All the paths passed to `Gonfic.new`, either in `directories:` + `filename:`, or in `file_paths:` mode, are
|
94
|
+
passed through `File.expand_path` - so you can use `~/` and `./` as parts of the path parameters.
|
95
|
+
|
66
96
|
### Loading configuration from ENV
|
67
97
|
|
68
98
|
If you provide `filename:`, `Gonfic.new` will also check `ENV` for variables it considers relevant.
|
@@ -71,7 +101,7 @@ The pattern, in general, is that `.tool-name` becomes `TOOL_NAME_*` - and any va
|
|
71
101
|
|
72
102
|
To disable this behavior, set `env_variable_prefix:` to falsey (`nil` will do).
|
73
103
|
|
74
|
-
You can also override the prefix
|
104
|
+
You can also override the prefix - set it independently from `filename:`. Underscore at the end is optional.
|
75
105
|
|
76
106
|
## Source code
|
77
107
|
|
data/lib/gonfic/version.rb
CHANGED
data/lib/gonfic.rb
CHANGED
@@ -9,14 +9,14 @@ require_relative 'gonfic/directories'
|
|
9
9
|
module Gonfic
|
10
10
|
YAML_FILE_LOADER = ->(filename){ YAML.safe_load(File.read(filename)) }
|
11
11
|
|
12
|
-
def self.new(
|
13
|
-
defaults: {},
|
14
|
-
filename: nil,
|
12
|
+
def self.new( # rubocop:disable Metrics/ParameterLists
|
13
|
+
defaults: {}, file_loader: YAML_FILE_LOADER,
|
14
|
+
filename: nil, directories: Directories.home_then_here, file_paths: join_into_paths(directories, filename),
|
15
15
|
env_variable_prefix: EnvExtractor.env_prefix_from_filename(filename)
|
16
16
|
)
|
17
17
|
configs =
|
18
18
|
configs_from_defaults(defaults: defaults) +
|
19
|
-
configs_from_files(
|
19
|
+
configs_from_files(file_loader: file_loader, file_paths: file_paths) +
|
20
20
|
configs_from_env(env_variable_prefix: env_variable_prefix, defaults: defaults)
|
21
21
|
|
22
22
|
configs.reduce(:merge!)
|
@@ -26,11 +26,16 @@ module Gonfic
|
|
26
26
|
[Config.new(defaults)]
|
27
27
|
end
|
28
28
|
|
29
|
-
def self.
|
30
|
-
return [] unless filename
|
29
|
+
def self.join_into_paths(directories, filename)
|
30
|
+
return [] unless directories && filename
|
31
31
|
|
32
|
-
directories.map
|
33
|
-
|
32
|
+
directories.map{ |directory| File.expand_path(File.join(directory, filename)) }
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.configs_from_files(file_loader:, file_paths:)
|
36
|
+
return [] unless file_paths
|
37
|
+
|
38
|
+
file_paths.map do |filepath|
|
34
39
|
load_configuration_file(filepath, file_loader)
|
35
40
|
end
|
36
41
|
end
|
@@ -39,8 +44,8 @@ module Gonfic
|
|
39
44
|
return {} unless File.exist?(filename)
|
40
45
|
|
41
46
|
file_loader.call(filename)
|
42
|
-
rescue StandardError =>
|
43
|
-
Hashie.logger.error(
|
47
|
+
rescue StandardError => exc
|
48
|
+
Hashie.logger.error(exc)
|
44
49
|
{}
|
45
50
|
end
|
46
51
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gonfic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emil Chludziński
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-04-07 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: hashie
|
@@ -54,7 +54,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
54
|
requirements:
|
55
55
|
- - ">="
|
56
56
|
- !ruby/object:Gem::Version
|
57
|
-
version: 2.
|
57
|
+
version: 2.7.0
|
58
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
60
|
- - ">="
|