myway_config 0.1.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.
@@ -0,0 +1,296 @@
1
+ # Loaders
2
+
3
+ MywayConfig provides custom loaders that extend Anyway Config's loading system.
4
+
5
+ ## Loading Priority
6
+
7
+ Configuration values are loaded in order (lowest to highest priority):
8
+
9
+ 1. **DefaultsLoader** - Bundled defaults from your gem
10
+ 2. **XdgConfigLoader** - User's XDG config files
11
+ 3. **Anyway Config loaders** - Project config, local overrides
12
+ 4. **Environment variables** - `PREFIX_KEY=value`
13
+ 5. **Constructor overrides** - Hash passed to `new`
14
+
15
+ Higher priority sources override lower priority values.
16
+
17
+ ---
18
+
19
+ ## DefaultsLoader
20
+
21
+ Loads bundled default configuration from a YAML file.
22
+
23
+ ### Purpose
24
+
25
+ Ensures default values are always available regardless of where the gem is installed. The defaults file is the single source of truth for configuration structure.
26
+
27
+ ### Registration
28
+
29
+ ```ruby
30
+ class MyApp::Config < MywayConfig::Base
31
+ config_name :myapp
32
+ defaults_path File.expand_path("config/defaults.yml", __dir__)
33
+ end
34
+ ```
35
+
36
+ ### Class Methods
37
+
38
+ #### register
39
+
40
+ Register a defaults file path.
41
+
42
+ ```ruby
43
+ MywayConfig::Loaders::DefaultsLoader.register(:myapp, "/path/to/defaults.yml")
44
+ ```
45
+
46
+ ---
47
+
48
+ #### defaults_path
49
+
50
+ Get the registered path for a config name.
51
+
52
+ ```ruby
53
+ MywayConfig::Loaders::DefaultsLoader.defaults_path(:myapp)
54
+ # => "/path/to/defaults.yml"
55
+ ```
56
+
57
+ ---
58
+
59
+ #### defaults_exist?
60
+
61
+ Check if defaults file exists.
62
+
63
+ ```ruby
64
+ MywayConfig::Loaders::DefaultsLoader.defaults_exist?(:myapp)
65
+ # => true
66
+ ```
67
+
68
+ ---
69
+
70
+ #### schema
71
+
72
+ Get the defaults section from the YAML file.
73
+
74
+ ```ruby
75
+ MywayConfig::Loaders::DefaultsLoader.schema(:myapp)
76
+ # => {database: {host: "localhost", ...}, log_level: :info, ...}
77
+ ```
78
+
79
+ ---
80
+
81
+ #### valid_environments
82
+
83
+ Get list of valid environment names.
84
+
85
+ ```ruby
86
+ MywayConfig::Loaders::DefaultsLoader.valid_environments(:myapp)
87
+ # => [:development, :production, :test]
88
+ ```
89
+
90
+ ---
91
+
92
+ #### valid_environment?
93
+
94
+ Check if an environment name is valid.
95
+
96
+ ```ruby
97
+ MywayConfig::Loaders::DefaultsLoader.valid_environment?(:myapp, :production)
98
+ # => true
99
+
100
+ MywayConfig::Loaders::DefaultsLoader.valid_environment?(:myapp, :staging)
101
+ # => false
102
+ ```
103
+
104
+ ---
105
+
106
+ ### Loading Behavior
107
+
108
+ The loader:
109
+
110
+ 1. Reads the `defaults:` section as base values
111
+ 2. Deep-merges current environment's overrides
112
+ 3. Returns the merged configuration
113
+
114
+ ```yaml
115
+ # defaults.yml
116
+ defaults:
117
+ database:
118
+ host: localhost
119
+ port: 5432
120
+
121
+ production:
122
+ database:
123
+ host: prod-db.example.com
124
+ ```
125
+
126
+ In production, the loader returns:
127
+
128
+ ```ruby
129
+ {database: {host: "prod-db.example.com", port: 5432}}
130
+ ```
131
+
132
+ ---
133
+
134
+ ## XdgConfigLoader
135
+
136
+ Loads user configuration from XDG Base Directory paths.
137
+
138
+ ### Purpose
139
+
140
+ Allows users to override configuration globally without modifying project files. Useful for personal preferences or machine-specific settings.
141
+
142
+ ### XDG Paths
143
+
144
+ The loader checks these paths (in order of priority):
145
+
146
+ 1. **macOS only:** `~/Library/Application Support/{app}/{app}.yml`
147
+ 2. `~/.config/{app}/{app}.yml` (XDG default)
148
+ 3. `$XDG_CONFIG_HOME/{app}/{app}.yml` (if set)
149
+
150
+ Higher-numbered paths take precedence.
151
+
152
+ ### Class Methods
153
+
154
+ #### config_paths
155
+
156
+ Get all potential config directory paths.
157
+
158
+ ```ruby
159
+ MywayConfig::Loaders::XdgConfigLoader.config_paths(:myapp)
160
+ # => [
161
+ # "/Users/me/Library/Application Support/myapp", # macOS only
162
+ # "/Users/me/.config/myapp"
163
+ # ]
164
+ ```
165
+
166
+ ---
167
+
168
+ #### find_config_file
169
+
170
+ Find the first existing config file.
171
+
172
+ ```ruby
173
+ MywayConfig::Loaders::XdgConfigLoader.find_config_file(:myapp)
174
+ # => "/Users/me/.config/myapp/myapp.yml" or nil
175
+ ```
176
+
177
+ ---
178
+
179
+ ### File Format
180
+
181
+ XDG config files can be flat or environment-specific:
182
+
183
+ **Flat format:**
184
+
185
+ ```yaml
186
+ # ~/.config/myapp/myapp.yml
187
+ database:
188
+ host: my-custom-host
189
+ port: 5433
190
+ ```
191
+
192
+ **With environments:**
193
+
194
+ ```yaml
195
+ # ~/.config/myapp/myapp.yml
196
+ development:
197
+ database:
198
+ host: dev-custom-host
199
+
200
+ production:
201
+ database:
202
+ host: prod-custom-host
203
+ ```
204
+
205
+ ### Example Usage
206
+
207
+ Create a user-specific override:
208
+
209
+ ```bash
210
+ mkdir -p ~/.config/myapp
211
+ cat > ~/.config/myapp/myapp.yml << 'EOF'
212
+ database:
213
+ host: custom-db.local
214
+ port: 5433
215
+ EOF
216
+ ```
217
+
218
+ Now your application uses these values:
219
+
220
+ ```ruby
221
+ config = MyApp::Config.new
222
+ config.database.host # => "custom-db.local"
223
+ config.database.port # => 5433
224
+ ```
225
+
226
+ ---
227
+
228
+ ## Loader Registration
229
+
230
+ Loaders are automatically registered when you call `MywayConfig.setup!` (which happens when requiring the gem).
231
+
232
+ ```ruby
233
+ # lib/myway_config.rb
234
+ module MywayConfig
235
+ def self.setup!
236
+ return if @setup_complete
237
+
238
+ Anyway.loaders.insert_before(
239
+ :yml,
240
+ :xdg_config,
241
+ Loaders::XdgConfigLoader
242
+ )
243
+
244
+ Anyway.loaders.insert_before(
245
+ :xdg_config,
246
+ :bundled_defaults,
247
+ Loaders::DefaultsLoader
248
+ )
249
+
250
+ @setup_complete = true
251
+ end
252
+ end
253
+ ```
254
+
255
+ ### Manual Setup
256
+
257
+ If you need to control when loaders are registered:
258
+
259
+ ```ruby
260
+ require 'myway_config'
261
+
262
+ # Check if already set up
263
+ MywayConfig.setup? # => true (automatic)
264
+
265
+ # Reset and re-setup (mainly for testing)
266
+ MywayConfig.reset!
267
+ MywayConfig.setup!
268
+ ```
269
+
270
+ ---
271
+
272
+ ## Custom Loaders
273
+
274
+ You can create custom loaders by extending `Anyway::Loaders::Base`:
275
+
276
+ ```ruby
277
+ class MyCustomLoader < Anyway::Loaders::Base
278
+ def call(name:, **options)
279
+ trace!(:custom, source: "my_source") do
280
+ # Return a Hash of configuration values
281
+ load_from_custom_source(name)
282
+ end
283
+ end
284
+
285
+ private
286
+
287
+ def load_from_custom_source(name)
288
+ # Your loading logic here
289
+ {}
290
+ end
291
+ end
292
+
293
+ # Register the loader
294
+ Anyway.loaders.insert_after(:yml, :custom, MyCustomLoader)
295
+ ```
296
+
@@ -0,0 +1,215 @@
1
+ # Contributing
2
+
3
+ Thank you for your interest in contributing to MywayConfig!
4
+
5
+ ## Getting Started
6
+
7
+ ### Prerequisites
8
+
9
+ - Ruby 3.2 or later
10
+ - Git
11
+ - Bundler
12
+
13
+ ### Setup
14
+
15
+ ```bash
16
+ # Fork and clone the repository
17
+ git clone https://github.com/YOUR_USERNAME/myway_config.git
18
+ cd myway_config
19
+
20
+ # Install dependencies
21
+ bin/setup
22
+
23
+ # Verify everything works
24
+ bundle exec rake test
25
+ ```
26
+
27
+ ## Development Workflow
28
+
29
+ ### 1. Create a Branch
30
+
31
+ ```bash
32
+ git checkout -b feature/your-feature-name
33
+ # or
34
+ git checkout -b fix/your-bug-fix
35
+ ```
36
+
37
+ ### 2. Make Changes
38
+
39
+ - Write code following the existing style
40
+ - Add tests for new functionality
41
+ - Update documentation if needed
42
+
43
+ ### 3. Run Tests
44
+
45
+ ```bash
46
+ # Run all tests
47
+ bundle exec rake test
48
+
49
+ # Run a specific test file
50
+ bundle exec ruby -Itest test/test_myway_config.rb
51
+
52
+ # Run a specific test
53
+ bundle exec ruby -Itest test/test_myway_config.rb -n test_auto_configure
54
+ ```
55
+
56
+ ### 4. Check Coverage
57
+
58
+ ```bash
59
+ # Run tests with coverage
60
+ bundle exec rake test
61
+
62
+ # Coverage report is displayed after tests complete
63
+ ```
64
+
65
+ The project uses `single_cov` for line-level coverage. All lines should be covered.
66
+
67
+ ### 5. Commit Changes
68
+
69
+ ```bash
70
+ git add .
71
+ git commit -m "Add feature: description of changes"
72
+ ```
73
+
74
+ ### 6. Push and Create PR
75
+
76
+ ```bash
77
+ git push origin feature/your-feature-name
78
+ ```
79
+
80
+ Then create a Pull Request on GitHub.
81
+
82
+ ## Code Style
83
+
84
+ ### Ruby Style
85
+
86
+ - Use 2 spaces for indentation
87
+ - Use `frozen_string_literal: true` pragma
88
+ - Prefer `do...end` for multi-line blocks
89
+ - Prefer `{ }` for single-line blocks
90
+
91
+ ### Documentation
92
+
93
+ - Add YARD documentation for public methods
94
+ - Update guides for user-facing changes
95
+ - Include examples in documentation
96
+
97
+ ### Testing
98
+
99
+ - Write tests for all new code
100
+ - Test edge cases and error conditions
101
+ - Use descriptive test names
102
+
103
+ ## Project Structure
104
+
105
+ ```
106
+ lib/
107
+ ├── myway_config.rb # Entry point
108
+ └── myway_config/
109
+ ├── base.rb # Configuration base class
110
+ ├── config_section.rb # Hash-like wrapper
111
+ ├── version.rb # Version number
112
+ └── loaders/
113
+ ├── defaults_loader.rb # Bundled defaults
114
+ └── xdg_config_loader.rb # XDG paths
115
+ ```
116
+
117
+ ## Adding Features
118
+
119
+ ### New Configuration Option
120
+
121
+ 1. Add to `defaults.yml` if applicable
122
+ 2. Add accessor method if needed
123
+ 3. Add tests
124
+ 4. Update documentation
125
+
126
+ ### New Loader
127
+
128
+ 1. Create loader in `lib/myway_config/loaders/`
129
+ 2. Extend `Anyway::Loaders::Base`
130
+ 3. Register in `MywayConfig.setup!`
131
+ 4. Add tests
132
+ 5. Document in `docs/api/loaders.md`
133
+
134
+ ### New ConfigSection Method
135
+
136
+ 1. Add method to `config_section.rb`
137
+ 2. Add YARD documentation
138
+ 3. Add tests
139
+ 4. Update `docs/api/config-section.md`
140
+
141
+ ## Running the Demo
142
+
143
+ ```bash
144
+ # Run the demo application
145
+ ruby examples/xyzzy.rb
146
+
147
+ # With different environments
148
+ RACK_ENV=production ruby examples/xyzzy.rb
149
+ RACK_ENV=test ruby examples/xyzzy.rb
150
+
151
+ # With environment variable overrides
152
+ XYZZY_DATABASE__HOST=custom-db.local ruby examples/xyzzy.rb
153
+ ```
154
+
155
+ ## Interactive Console
156
+
157
+ ```bash
158
+ # Start IRB with the gem loaded
159
+ bin/console
160
+
161
+ # Or from the examples directory
162
+ cd examples
163
+ irb -r ./xyzzy.rb
164
+ ```
165
+
166
+ ## Documentation
167
+
168
+ Documentation is built with MkDocs and Material theme.
169
+
170
+ ### Preview Documentation
171
+
172
+ ```bash
173
+ # Install MkDocs (if not installed)
174
+ pip install mkdocs mkdocs-material
175
+
176
+ # Serve documentation locally
177
+ mkdocs serve
178
+
179
+ # Build static site
180
+ mkdocs build
181
+ ```
182
+
183
+ ### Documentation Structure
184
+
185
+ ```
186
+ docs/
187
+ ├── index.md # Home page
188
+ ├── getting-started/ # Installation and quick start
189
+ ├── guides/ # Usage guides
190
+ ├── examples/ # Real-world examples
191
+ ├── api/ # API reference
192
+ └── development/ # Contributing info
193
+ ```
194
+
195
+ ## Releasing
196
+
197
+ Releases are managed by maintainers:
198
+
199
+ 1. Update `lib/myway_config/version.rb`
200
+ 2. Update `CHANGELOG.md`
201
+ 3. Commit: `git commit -m "Release vX.Y.Z"`
202
+ 4. Tag: `git tag vX.Y.Z`
203
+ 5. Push: `git push origin main --tags`
204
+ 6. Build and publish: `gem build && gem push myway_config-X.Y.Z.gem`
205
+
206
+ ## Getting Help
207
+
208
+ - Open an issue for bugs or feature requests
209
+ - Check existing issues before creating new ones
210
+ - Provide reproduction steps for bugs
211
+
212
+ ## License
213
+
214
+ By contributing, you agree that your contributions will be licensed under the MIT License.
215
+
@@ -0,0 +1,100 @@
1
+ # Development
2
+
3
+ Information for contributing to MywayConfig.
4
+
5
+ ## Guides
6
+
7
+ ### [Contributing](contributing.md)
8
+
9
+ How to contribute to the project:
10
+
11
+ - Setting up the development environment
12
+ - Making changes
13
+ - Submitting pull requests
14
+
15
+ ### [Testing](testing.md)
16
+
17
+ Running and writing tests:
18
+
19
+ - Test suite overview
20
+ - Running tests
21
+ - Code coverage
22
+
23
+ ## Quick Start
24
+
25
+ ```bash
26
+ # Clone the repository
27
+ git clone https://github.com/madbomber/myway_config.git
28
+ cd myway_config
29
+
30
+ # Install dependencies
31
+ bin/setup
32
+
33
+ # Run tests
34
+ bundle exec rake test
35
+
36
+ # Start interactive console
37
+ bin/console
38
+ ```
39
+
40
+ ## Project Structure
41
+
42
+ ```
43
+ myway_config/
44
+ ├── lib/
45
+ │ ├── myway_config.rb # Main module and setup
46
+ │ └── myway_config/
47
+ │ ├── base.rb # Base configuration class
48
+ │ ├── config_section.rb # Hash-like wrapper
49
+ │ ├── version.rb # Version constant
50
+ │ └── loaders/
51
+ │ ├── defaults_loader.rb # Bundled defaults loader
52
+ │ └── xdg_config_loader.rb # XDG config loader
53
+ ├── test/
54
+ │ ├── test_helper.rb # Test setup
55
+ │ ├── test_myway_config.rb # Main tests
56
+ │ └── fixtures/ # Test YAML files
57
+ ├── examples/
58
+ │ ├── xyzzy.rb # Demo application
59
+ │ └── config/
60
+ │ └── defaults.yml # Demo defaults
61
+ ├── docs/ # MkDocs documentation
62
+ └── .github/
63
+ └── workflows/
64
+ └── ci.yml # GitHub Actions CI
65
+ ```
66
+
67
+ ## Architecture
68
+
69
+ MywayConfig extends [Anyway Config](https://github.com/palkan/anyway_config) with:
70
+
71
+ 1. **DefaultsLoader** - Loads bundled YAML defaults
72
+ 2. **XdgConfigLoader** - Loads user XDG config files
73
+ 3. **ConfigSection** - Hash-like wrapper with Enumerable
74
+ 4. **Base** - Configuration base class with `auto_configure!`
75
+
76
+ ### Loading Order
77
+
78
+ ```
79
+ DefaultsLoader (lowest priority)
80
+
81
+ XdgConfigLoader
82
+
83
+ Anyway Config loaders (yml, local)
84
+
85
+ Environment variables
86
+
87
+ Constructor overrides (highest priority)
88
+ ```
89
+
90
+ ## Dependencies
91
+
92
+ - Ruby 3.2+
93
+ - anyway_config (~> 2.0)
94
+
95
+ ### Development Dependencies
96
+
97
+ - minitest
98
+ - rake
99
+ - single_cov
100
+