gonfic 0.6.3 → 0.7.1

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +46 -9
  3. data/lib/gonfic/version.rb +1 -1
  4. data/lib/gonfic.rb +16 -10
  5. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6ad26346c85a9ae7084d9889d8e5f633f5e0a350ef780054d81012638e32cb5
4
- data.tar.gz: 7bff4a1c1c1df7950d81299b449c71049f745fe8158f55e284b9ff0de434989b
3
+ metadata.gz: 6ecd1286abfa268a78ffb750a5f47fe2be0eb90249c566bea5a9362c3b4d54ad
4
+ data.tar.gz: 5854cc9db210db4c3a45885ceecf6b43db5c486c96819fd4d30445bb0d23ccf9
5
5
  SHA512:
6
- metadata.gz: 3341cf94bb91e9f69b8a667438ffa9639bbcd9e7004d83fc2d0e1d4d7bb028c201007b486b2dc1627b750b275241e2b1f48b567374dbd876835dbe12822c3131
7
- data.tar.gz: f6d00634a769200640b62c53a36567c27dd8fa70632b63ce4b0a60db939ba0a7dfeb5a4d8095bfb3931118ab63cf9965dbebc5ff295e142c2d00f9c709a43ad0
6
+ metadata.gz: 6ae7706db4df2837309c6b03ce3a14c3b6cf810b916f549fb780a0825e893f037e2c8768df8e3702244b4584c6762ac531ae07c128654ab8ad2461bf14274632
7
+ data.tar.gz: 6110157ff9cc6bb5f403b1c89709c36f0e8919cd4c8afbef5fcdc88824c4ff0440761f497b0791fc212ac4e2cb8dde02412ccecc0e4996f39d6984c2e888c4be
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
- CONFIGURATION_FILENAME = '.my_tool' # YAML config file format, by default
12
- CONFIGURATION_DEFAULTS = {a: 1, b: {c: 2}} # will deep_merge! using Hashie magic
13
- CONFIG = Gonfic.new(filename: CONFIGURATION_FILENAME, defaults: CONFIGURATION_DEFAULTS)
14
- puts CONFIG.b.c
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
- module MyTool
53
- def self.config
54
- @config ||= Gonfic.new(...)
55
- end
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. Underscore at the end is optional.
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
 
@@ -94,6 +124,13 @@ You can also run `bin/console` for an interactive prompt that will allow you to
94
124
 
95
125
  To install this gem onto your local machine from sources, run `bundle exec rake install`.
96
126
 
127
+ ### Version bump
128
+
129
+ In `fish` syntax, even if you are on Windows:
130
+ ```sh
131
+ gem bump -v patch; dos2unix (fd version.rb); and bundle install; and bundle exec rake; and git commit -a --amend
132
+ ```
133
+
97
134
  ## Contributing
98
135
 
99
136
  Bug reports and merge requests are welcome on GitLab at https://gitlab.com/tanstaafl/gonfic.
@@ -1,3 +1,3 @@
1
1
  module Gonfic
2
- VERSION = '0.6.3'.freeze
2
+ VERSION = '0.7.1'.freeze
3
3
  end
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, file_loader: YAML_FILE_LOADER, directories: Directories.home_then_here,
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(filename: filename, file_loader: file_loader, directories: directories) +
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,21 +26,27 @@ module Gonfic
26
26
  [Config.new(defaults)]
27
27
  end
28
28
 
29
- def self.configs_from_files(filename:, file_loader:, directories:)
30
- return [] unless filename
29
+ def self.join_into_paths(directories, filename)
30
+ return [] unless directories && filename
31
31
 
32
- directories.map do |directory|
33
- filepath = File.expand_path(File.join(directory, filename))
32
+ directories.map{ |directory| 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
37
42
 
38
43
  def self.load_configuration_file(filename, file_loader)
44
+ filename = File.expand_path(filename)
39
45
  return {} unless File.exist?(filename)
40
46
 
41
47
  file_loader.call(filename)
42
- rescue StandardError => e
43
- Hashie.logger.error(e)
48
+ rescue StandardError => exc
49
+ Hashie.logger.error(exc)
44
50
  {}
45
51
  end
46
52
 
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.6.3
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Chludziński
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-01-02 00:00:00.000000000 Z
10
+ date: 2025-04-23 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.6.0
57
+ version: 2.7.0
58
58
  required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ">="