config_default 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 +7 -0
- data/.github/workflows/main.yml +36 -0
- data/.gitignore +13 -0
- data/.rubocop.yml +6 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +209 -0
- data/LICENSE.txt +21 -0
- data/README.md +165 -0
- data/Rakefile +12 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/config/database.default.yml +2 -0
- data/config/database.yml +2 -0
- data/config/example1.default.yml +3 -0
- data/config/example1.yml +2 -0
- data/config/example2.default.yml +2 -0
- data/config/example3.yml +2 -0
- data/config/nested.yml +4 -0
- data/lib/config_default/config.rb +8 -0
- data/lib/config_default/rails/application/configuration_extension.rb +13 -0
- data/lib/config_default/rails/application_extension.rb +10 -0
- data/lib/config_default/struct.rb +44 -0
- data/lib/config_default/version.rb +5 -0
- data/lib/config_default.rb +50 -0
- metadata +86 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: cd56026f65a3563933736e9159c7e051b4fd4899300afe65c4430dbd451bdcc9
|
|
4
|
+
data.tar.gz: 38255f6582b3bd549af219f9c4ce45ac56085223b26fdee55f38c9743af6446e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 71bf47a44b41498644f8bf6209204d265975127e7732bdeb2be05cc3da92a1d44679651348abb190af19cd27638f50367a6bdd535377d94d4de6ed4e583699ff
|
|
7
|
+
data.tar.gz: 991e86e3c91f03abdab3e02e0a747a808fb05625c6fead718b4b3187941601696dba5d5c03e272939afc382e1e3d8c9ccaede17b703133af6c665a20de4d803b
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: Test
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- master
|
|
7
|
+
|
|
8
|
+
pull_request:
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
rubocop:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v2
|
|
15
|
+
- uses: ruby/setup-ruby@v1
|
|
16
|
+
with:
|
|
17
|
+
ruby-version: 3
|
|
18
|
+
bundler-cache: true
|
|
19
|
+
- name: Run rubocop
|
|
20
|
+
run: bundle exec rubocop
|
|
21
|
+
|
|
22
|
+
rspec:
|
|
23
|
+
runs-on: ubuntu-latest
|
|
24
|
+
|
|
25
|
+
strategy:
|
|
26
|
+
matrix:
|
|
27
|
+
ruby: [2.5, 2.6, 2.7, 3.0]
|
|
28
|
+
|
|
29
|
+
steps:
|
|
30
|
+
- uses: actions/checkout@v2
|
|
31
|
+
- uses: ruby/setup-ruby@v1
|
|
32
|
+
with:
|
|
33
|
+
ruby-version: ${{ matrix.ruby }}
|
|
34
|
+
bundler-cache: true
|
|
35
|
+
- name: Run rspec
|
|
36
|
+
run: bundle exec rspec
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
config_default (0.1.0)
|
|
5
|
+
rails (~> 6)
|
|
6
|
+
|
|
7
|
+
GEM
|
|
8
|
+
remote: https://rubygems.org/
|
|
9
|
+
specs:
|
|
10
|
+
actioncable (6.1.4.1)
|
|
11
|
+
actionpack (= 6.1.4.1)
|
|
12
|
+
activesupport (= 6.1.4.1)
|
|
13
|
+
nio4r (~> 2.0)
|
|
14
|
+
websocket-driver (>= 0.6.1)
|
|
15
|
+
actionmailbox (6.1.4.1)
|
|
16
|
+
actionpack (= 6.1.4.1)
|
|
17
|
+
activejob (= 6.1.4.1)
|
|
18
|
+
activerecord (= 6.1.4.1)
|
|
19
|
+
activestorage (= 6.1.4.1)
|
|
20
|
+
activesupport (= 6.1.4.1)
|
|
21
|
+
mail (>= 2.7.1)
|
|
22
|
+
actionmailer (6.1.4.1)
|
|
23
|
+
actionpack (= 6.1.4.1)
|
|
24
|
+
actionview (= 6.1.4.1)
|
|
25
|
+
activejob (= 6.1.4.1)
|
|
26
|
+
activesupport (= 6.1.4.1)
|
|
27
|
+
mail (~> 2.5, >= 2.5.4)
|
|
28
|
+
rails-dom-testing (~> 2.0)
|
|
29
|
+
actionpack (6.1.4.1)
|
|
30
|
+
actionview (= 6.1.4.1)
|
|
31
|
+
activesupport (= 6.1.4.1)
|
|
32
|
+
rack (~> 2.0, >= 2.0.9)
|
|
33
|
+
rack-test (>= 0.6.3)
|
|
34
|
+
rails-dom-testing (~> 2.0)
|
|
35
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
|
36
|
+
actiontext (6.1.4.1)
|
|
37
|
+
actionpack (= 6.1.4.1)
|
|
38
|
+
activerecord (= 6.1.4.1)
|
|
39
|
+
activestorage (= 6.1.4.1)
|
|
40
|
+
activesupport (= 6.1.4.1)
|
|
41
|
+
nokogiri (>= 1.8.5)
|
|
42
|
+
actionview (6.1.4.1)
|
|
43
|
+
activesupport (= 6.1.4.1)
|
|
44
|
+
builder (~> 3.1)
|
|
45
|
+
erubi (~> 1.4)
|
|
46
|
+
rails-dom-testing (~> 2.0)
|
|
47
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
|
48
|
+
activejob (6.1.4.1)
|
|
49
|
+
activesupport (= 6.1.4.1)
|
|
50
|
+
globalid (>= 0.3.6)
|
|
51
|
+
activemodel (6.1.4.1)
|
|
52
|
+
activesupport (= 6.1.4.1)
|
|
53
|
+
activerecord (6.1.4.1)
|
|
54
|
+
activemodel (= 6.1.4.1)
|
|
55
|
+
activesupport (= 6.1.4.1)
|
|
56
|
+
activestorage (6.1.4.1)
|
|
57
|
+
actionpack (= 6.1.4.1)
|
|
58
|
+
activejob (= 6.1.4.1)
|
|
59
|
+
activerecord (= 6.1.4.1)
|
|
60
|
+
activesupport (= 6.1.4.1)
|
|
61
|
+
marcel (~> 1.0.0)
|
|
62
|
+
mini_mime (>= 1.1.0)
|
|
63
|
+
activesupport (6.1.4.1)
|
|
64
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
65
|
+
i18n (>= 1.6, < 2)
|
|
66
|
+
minitest (>= 5.1)
|
|
67
|
+
tzinfo (~> 2.0)
|
|
68
|
+
zeitwerk (~> 2.3)
|
|
69
|
+
ast (2.4.2)
|
|
70
|
+
builder (3.2.4)
|
|
71
|
+
coderay (1.1.3)
|
|
72
|
+
concurrent-ruby (1.1.9)
|
|
73
|
+
crass (1.0.6)
|
|
74
|
+
diff-lcs (1.4.4)
|
|
75
|
+
erubi (1.10.0)
|
|
76
|
+
globalid (0.5.2)
|
|
77
|
+
activesupport (>= 5.0)
|
|
78
|
+
i18n (1.8.11)
|
|
79
|
+
concurrent-ruby (~> 1.0)
|
|
80
|
+
loofah (2.12.0)
|
|
81
|
+
crass (~> 1.0.2)
|
|
82
|
+
nokogiri (>= 1.5.9)
|
|
83
|
+
mail (2.7.1)
|
|
84
|
+
mini_mime (>= 0.1.1)
|
|
85
|
+
marcel (1.0.2)
|
|
86
|
+
method_source (1.0.0)
|
|
87
|
+
mini_mime (1.1.2)
|
|
88
|
+
minitest (5.14.4)
|
|
89
|
+
nio4r (2.5.8)
|
|
90
|
+
nokogiri (1.12.5-x86_64-darwin)
|
|
91
|
+
racc (~> 1.4)
|
|
92
|
+
nokogiri (1.12.5-x86_64-linux)
|
|
93
|
+
racc (~> 1.4)
|
|
94
|
+
parallel (1.21.0)
|
|
95
|
+
parser (3.0.2.0)
|
|
96
|
+
ast (~> 2.4.1)
|
|
97
|
+
pry (0.14.1)
|
|
98
|
+
coderay (~> 1.1)
|
|
99
|
+
method_source (~> 1.0)
|
|
100
|
+
racc (1.6.0)
|
|
101
|
+
rack (2.2.3)
|
|
102
|
+
rack-test (1.1.0)
|
|
103
|
+
rack (>= 1.0, < 3)
|
|
104
|
+
rails (6.1.4.1)
|
|
105
|
+
actioncable (= 6.1.4.1)
|
|
106
|
+
actionmailbox (= 6.1.4.1)
|
|
107
|
+
actionmailer (= 6.1.4.1)
|
|
108
|
+
actionpack (= 6.1.4.1)
|
|
109
|
+
actiontext (= 6.1.4.1)
|
|
110
|
+
actionview (= 6.1.4.1)
|
|
111
|
+
activejob (= 6.1.4.1)
|
|
112
|
+
activemodel (= 6.1.4.1)
|
|
113
|
+
activerecord (= 6.1.4.1)
|
|
114
|
+
activestorage (= 6.1.4.1)
|
|
115
|
+
activesupport (= 6.1.4.1)
|
|
116
|
+
bundler (>= 1.15.0)
|
|
117
|
+
railties (= 6.1.4.1)
|
|
118
|
+
sprockets-rails (>= 2.0.0)
|
|
119
|
+
rails-dom-testing (2.0.3)
|
|
120
|
+
activesupport (>= 4.2.0)
|
|
121
|
+
nokogiri (>= 1.6)
|
|
122
|
+
rails-html-sanitizer (1.4.2)
|
|
123
|
+
loofah (~> 2.3)
|
|
124
|
+
railties (6.1.4.1)
|
|
125
|
+
actionpack (= 6.1.4.1)
|
|
126
|
+
activesupport (= 6.1.4.1)
|
|
127
|
+
method_source
|
|
128
|
+
rake (>= 0.13)
|
|
129
|
+
thor (~> 1.0)
|
|
130
|
+
rainbow (3.0.0)
|
|
131
|
+
rake (13.0.6)
|
|
132
|
+
regexp_parser (2.1.1)
|
|
133
|
+
rexml (3.2.5)
|
|
134
|
+
rspec (3.10.0)
|
|
135
|
+
rspec-core (~> 3.10.0)
|
|
136
|
+
rspec-expectations (~> 3.10.0)
|
|
137
|
+
rspec-mocks (~> 3.10.0)
|
|
138
|
+
rspec-core (3.10.1)
|
|
139
|
+
rspec-support (~> 3.10.0)
|
|
140
|
+
rspec-expectations (3.10.1)
|
|
141
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
142
|
+
rspec-support (~> 3.10.0)
|
|
143
|
+
rspec-mocks (3.10.2)
|
|
144
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
145
|
+
rspec-support (~> 3.10.0)
|
|
146
|
+
rspec-support (3.10.3)
|
|
147
|
+
rubocop (1.17.0)
|
|
148
|
+
parallel (~> 1.10)
|
|
149
|
+
parser (>= 3.0.0.0)
|
|
150
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
151
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
152
|
+
rexml
|
|
153
|
+
rubocop-ast (>= 1.7.0, < 2.0)
|
|
154
|
+
ruby-progressbar (~> 1.7)
|
|
155
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
156
|
+
rubocop-ast (1.13.0)
|
|
157
|
+
parser (>= 3.0.1.1)
|
|
158
|
+
rubocop-config-umbrellio (1.17.0.53)
|
|
159
|
+
rubocop (= 1.17.0)
|
|
160
|
+
rubocop-performance (= 1.10.0)
|
|
161
|
+
rubocop-rails (= 2.9.1)
|
|
162
|
+
rubocop-rake (= 0.5.1)
|
|
163
|
+
rubocop-rspec (= 2.2.0)
|
|
164
|
+
rubocop-sequel (= 0.2.0)
|
|
165
|
+
rubocop-performance (1.10.0)
|
|
166
|
+
rubocop (>= 0.90.0, < 2.0)
|
|
167
|
+
rubocop-ast (>= 0.4.0)
|
|
168
|
+
rubocop-rails (2.9.1)
|
|
169
|
+
activesupport (>= 4.2.0)
|
|
170
|
+
rack (>= 1.1)
|
|
171
|
+
rubocop (>= 0.90.0, < 2.0)
|
|
172
|
+
rubocop-rake (0.5.1)
|
|
173
|
+
rubocop
|
|
174
|
+
rubocop-rspec (2.2.0)
|
|
175
|
+
rubocop (~> 1.0)
|
|
176
|
+
rubocop-ast (>= 1.1.0)
|
|
177
|
+
rubocop-sequel (0.2.0)
|
|
178
|
+
rubocop (~> 1.0)
|
|
179
|
+
ruby-progressbar (1.11.0)
|
|
180
|
+
sprockets (4.0.2)
|
|
181
|
+
concurrent-ruby (~> 1.0)
|
|
182
|
+
rack (> 1, < 3)
|
|
183
|
+
sprockets-rails (3.4.0)
|
|
184
|
+
actionpack (>= 5.2)
|
|
185
|
+
activesupport (>= 5.2)
|
|
186
|
+
sprockets (>= 3.0.0)
|
|
187
|
+
thor (1.1.0)
|
|
188
|
+
tzinfo (2.0.4)
|
|
189
|
+
concurrent-ruby (~> 1.0)
|
|
190
|
+
unicode-display_width (2.1.0)
|
|
191
|
+
websocket-driver (0.7.5)
|
|
192
|
+
websocket-extensions (>= 0.1.0)
|
|
193
|
+
websocket-extensions (0.1.5)
|
|
194
|
+
zeitwerk (2.5.1)
|
|
195
|
+
|
|
196
|
+
PLATFORMS
|
|
197
|
+
x86_64-darwin-20
|
|
198
|
+
x86_64-linux
|
|
199
|
+
|
|
200
|
+
DEPENDENCIES
|
|
201
|
+
config_default!
|
|
202
|
+
pry
|
|
203
|
+
rails
|
|
204
|
+
rake
|
|
205
|
+
rspec
|
|
206
|
+
rubocop-config-umbrellio
|
|
207
|
+
|
|
208
|
+
BUNDLED WITH
|
|
209
|
+
2.2.28
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 ash
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# ConfigDefault
|
|
2
|
+
|
|
3
|
+
ConfigDefault is a simple way to separate your test/development config from your staging/production config.
|
|
4
|
+
Designed to work with [Rails](https://github.com/rails/rails). It depends on `ActiveSupport`.
|
|
5
|
+
Maybe in future I'll remove Rails dependency (cause it's never cool) and leave only ActiveSupport.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'config_default'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle install
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install config_default
|
|
22
|
+
|
|
23
|
+
## Configuration
|
|
24
|
+
|
|
25
|
+
You can control your configuration path and postfix which ConfigDefault will use.
|
|
26
|
+
Please use it in your `application.rb` file.
|
|
27
|
+
This example implement default values:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
ConfigDefault.configure do |config|
|
|
31
|
+
config.config_path = "./config"
|
|
32
|
+
config.postfix = "default"
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
### Default behaviour
|
|
39
|
+
|
|
40
|
+
First you need to separate you configuration to default and not.
|
|
41
|
+
Let's check an example for Rails default config file `config/database.yml`:
|
|
42
|
+
|
|
43
|
+
```yaml
|
|
44
|
+
# config/database.default.yml
|
|
45
|
+
default: &default
|
|
46
|
+
adapter: postgres
|
|
47
|
+
max_connections: 5
|
|
48
|
+
host: postgres
|
|
49
|
+
database: db
|
|
50
|
+
|
|
51
|
+
development:
|
|
52
|
+
<<: *default
|
|
53
|
+
database: db_development
|
|
54
|
+
|
|
55
|
+
test:
|
|
56
|
+
<<: *default
|
|
57
|
+
database: db_test
|
|
58
|
+
|
|
59
|
+
staging: *default
|
|
60
|
+
|
|
61
|
+
production: *default
|
|
62
|
+
|
|
63
|
+
public_production: *default
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
So this config file is enough for development/test environment.
|
|
67
|
+
But in staging/production you need to add other SECRET variables to this config.
|
|
68
|
+
Now you can do in your secret repository this:
|
|
69
|
+
|
|
70
|
+
```yaml
|
|
71
|
+
# config/database.yml
|
|
72
|
+
production:
|
|
73
|
+
user: db_superuser
|
|
74
|
+
password: db_password
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
All you need after creating this file in your secret location is to place this file near with
|
|
78
|
+
previous one. ConfigDefault will merge them automatically on startup.
|
|
79
|
+
|
|
80
|
+
### Your application configuration
|
|
81
|
+
|
|
82
|
+
Also `ConfigDefault` can help you with your application config and secrets.
|
|
83
|
+
Just create `config/app.default.yml` (test/staging) and `config/app.yml` (staging/production) files.
|
|
84
|
+
And then in your application load it:
|
|
85
|
+
|
|
86
|
+
```ruby
|
|
87
|
+
config = ConfigDefault.load(:app)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
It will load result hash with merging `app.yml` and `app.default.yml` files.
|
|
91
|
+
By default `ConfigDefault` load YAML files as is.
|
|
92
|
+
So it's mean that what key (string or symbol) you will define in the YAML file => it'll be the same on `#load` result.
|
|
93
|
+
|
|
94
|
+
You can change this behaviour with `#load` options:
|
|
95
|
+
```ruby
|
|
96
|
+
config = ConfigDefault.load(:app, symbolize_keys: true) # Hash with symbolized first keys
|
|
97
|
+
config = ConfigDefault.load(:app, deep_symbolize_keys: true) # Hash with symbolized all keys
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
By default `ConfigDefault` using `Rails.env` to determnine what key you need from you config file.
|
|
101
|
+
You can pass this key by your own:
|
|
102
|
+
```ruby
|
|
103
|
+
config = ConfigDefault.load(:app, key: nil) # Will not use key at all and result by full file
|
|
104
|
+
config = ConfigDefault.load(:app, key: "preprod") # Will search preprod key in file
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Struct using
|
|
108
|
+
|
|
109
|
+
If you want to use configuration as an struct object you can use `#load_struct` method.
|
|
110
|
+
Let's see an example with `database.yaml` config above:
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
config = ConfigDefault.load_struct(:database)
|
|
114
|
+
config.host
|
|
115
|
+
# => "postgres"
|
|
116
|
+
config.lolkek
|
|
117
|
+
# => StandardError: There is no option :lolkek in configuration.
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
If your want to not raise an error on wrong key using (sometimes it's helpful) please use `allow_nil` option:
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
config = ConfigDefault.load_struct(:database, allow_nil: true)
|
|
124
|
+
config.host
|
|
125
|
+
# => "postgres"
|
|
126
|
+
config.lolkek
|
|
127
|
+
# => nil
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
If your need nested object using use `recursive` option on configuration load:
|
|
131
|
+
|
|
132
|
+
```yaml
|
|
133
|
+
# config/app.default.yml
|
|
134
|
+
first:
|
|
135
|
+
second:
|
|
136
|
+
third: "option"
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
```ruby
|
|
140
|
+
config = ConfigDefault.load_struct(:app, key: nil, recursive: true)
|
|
141
|
+
config.first.second.third
|
|
142
|
+
# => "option"
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
At any setting you can use `#to_hash` method to get a `Hash` for this configuration branch.
|
|
146
|
+
It will be `ActiveSupport::HashWithIndifferentAccess`.
|
|
147
|
+
Example with `app.default.yml` from above:
|
|
148
|
+
|
|
149
|
+
```ruby
|
|
150
|
+
config = ConfigDefault.load_struct(:app, key: nil, recursive: true)
|
|
151
|
+
config.to_hash
|
|
152
|
+
# => { "first" => { "second" => { "third" => "option" } } }
|
|
153
|
+
config.first.to_hash
|
|
154
|
+
# => { "second" => { "third" => "option" } }
|
|
155
|
+
config.first.second.to_hash
|
|
156
|
+
# => { "third" => "option" }
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
## Contributing
|
|
160
|
+
|
|
161
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/skirushkin/config_default.
|
|
162
|
+
|
|
163
|
+
## License
|
|
164
|
+
|
|
165
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bundler/setup"
|
|
5
|
+
require "rails"
|
|
6
|
+
require "config_default"
|
|
7
|
+
|
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
10
|
+
|
|
11
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
12
|
+
# require "pry"
|
|
13
|
+
# Pry.start
|
|
14
|
+
|
|
15
|
+
require "irb"
|
|
16
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/config/database.yml
ADDED
data/config/example1.yml
ADDED
data/config/example3.yml
ADDED
data/config/nested.yml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rails::Application::ConfigurationExtension
|
|
4
|
+
def load_database_yaml
|
|
5
|
+
ConfigDefault.load(:database, key: nil)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def database_configuration
|
|
9
|
+
load_database_yaml
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Rails::Application::Configuration.prepend(Rails::Application::ConfigurationExtension)
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Rails::ApplicationExtension
|
|
4
|
+
def config_for(name, env: Rails.env)
|
|
5
|
+
data = ConfigDefault.load(name, key: env, deep_symbolize_keys: true)
|
|
6
|
+
ActiveSupport::OrderedOptions.new.merge(data)
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
Rails::Application.prepend(Rails::ApplicationExtension)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class ConfigDefault::Struct
|
|
4
|
+
def initialize(attributes = {}, recursive: false, allow_nil: false)
|
|
5
|
+
@attributes = ActiveSupport::HashWithIndifferentAccess.new(attributes)
|
|
6
|
+
@allow_nil = allow_nil
|
|
7
|
+
@recursive = recursive
|
|
8
|
+
|
|
9
|
+
if @recursive
|
|
10
|
+
@attributes.each do |key, value|
|
|
11
|
+
next unless value.is_a?(Hash)
|
|
12
|
+
@attributes[key] = self.class.new(value, recursive: @recursive, allow_nil: @allow_nil)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
@attributes.freeze
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def [](key)
|
|
20
|
+
@attributes[key]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def method_missing(method, *_args)
|
|
24
|
+
return @attributes[method] if @attributes.key?(method)
|
|
25
|
+
raise StandardError.new("There is no option :#{method} in configuration.") unless @allow_nil
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def respond_to_missing?(*_args)
|
|
29
|
+
true
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def to_hash
|
|
33
|
+
dup = @attributes.dup
|
|
34
|
+
|
|
35
|
+
if @recursive
|
|
36
|
+
dup.each do |key, value|
|
|
37
|
+
next unless value.is_a?(self.class)
|
|
38
|
+
dup[key] = value.to_hash
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
dup
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/core_ext/hash"
|
|
4
|
+
|
|
5
|
+
require "config_default/version"
|
|
6
|
+
require "config_default/config"
|
|
7
|
+
require "config_default/struct"
|
|
8
|
+
|
|
9
|
+
require "config_default/rails/application/configuration_extension"
|
|
10
|
+
require "config_default/rails/application_extension"
|
|
11
|
+
|
|
12
|
+
module ConfigDefault
|
|
13
|
+
extend self
|
|
14
|
+
|
|
15
|
+
attr_accessor :config
|
|
16
|
+
|
|
17
|
+
@config = ConfigDefault::Config.new
|
|
18
|
+
|
|
19
|
+
def configure
|
|
20
|
+
yield(config) if block_given?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def load(name, key: Rails.env, symbolize_keys: false, deep_symbolize_keys: false)
|
|
24
|
+
base_config = load_file("#{name}.#{config.postfix}")
|
|
25
|
+
config = load_file(name)
|
|
26
|
+
|
|
27
|
+
data = base_config.deep_merge(config)
|
|
28
|
+
data = key ? data[key] : data
|
|
29
|
+
|
|
30
|
+
if deep_symbolize_keys
|
|
31
|
+
data.deep_symbolize_keys
|
|
32
|
+
elsif symbolize_keys
|
|
33
|
+
data.symbolize_keys
|
|
34
|
+
else
|
|
35
|
+
data
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def load_file(name)
|
|
40
|
+
file_name = File.join(config.config_path, "#{name}.yml")
|
|
41
|
+
ActiveSupport::ConfigurationFile.parse(file_name)
|
|
42
|
+
rescue
|
|
43
|
+
{}
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def load_struct(name, key: Rails.env, recursive: false, allow_nil: false)
|
|
47
|
+
attributes = load(name, key: key)
|
|
48
|
+
ConfigDefault::Struct.new(attributes, recursive: recursive, allow_nil: allow_nil)
|
|
49
|
+
end
|
|
50
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: config_default
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Stepan Kirushkin
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-11-19 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rails
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '6'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '6'
|
|
27
|
+
description: |2
|
|
28
|
+
ConfigDefault add an ability to separate your configuration on *.default.yml and *.yml files.
|
|
29
|
+
It's very useful to mix in configuration when you deploy your application.
|
|
30
|
+
email:
|
|
31
|
+
- stepan.kirushkin@gmail.com
|
|
32
|
+
executables: []
|
|
33
|
+
extensions: []
|
|
34
|
+
extra_rdoc_files: []
|
|
35
|
+
files:
|
|
36
|
+
- ".github/workflows/main.yml"
|
|
37
|
+
- ".gitignore"
|
|
38
|
+
- ".rubocop.yml"
|
|
39
|
+
- CHANGELOG.md
|
|
40
|
+
- Gemfile
|
|
41
|
+
- Gemfile.lock
|
|
42
|
+
- LICENSE.txt
|
|
43
|
+
- README.md
|
|
44
|
+
- Rakefile
|
|
45
|
+
- bin/console
|
|
46
|
+
- bin/setup
|
|
47
|
+
- config/database.default.yml
|
|
48
|
+
- config/database.yml
|
|
49
|
+
- config/example1.default.yml
|
|
50
|
+
- config/example1.yml
|
|
51
|
+
- config/example2.default.yml
|
|
52
|
+
- config/example3.yml
|
|
53
|
+
- config/nested.yml
|
|
54
|
+
- lib/config_default.rb
|
|
55
|
+
- lib/config_default/config.rb
|
|
56
|
+
- lib/config_default/rails/application/configuration_extension.rb
|
|
57
|
+
- lib/config_default/rails/application_extension.rb
|
|
58
|
+
- lib/config_default/struct.rb
|
|
59
|
+
- lib/config_default/version.rb
|
|
60
|
+
homepage: https://github.com/skirushkin/config_default
|
|
61
|
+
licenses:
|
|
62
|
+
- MIT
|
|
63
|
+
metadata:
|
|
64
|
+
homepage_uri: https://github.com/skirushkin/config_default
|
|
65
|
+
source_code_uri: https://github.com/skirushkin/config_default
|
|
66
|
+
changelog_uri: https://github.com/skirushkin/config_default/blob/master/CHANGELOG.md
|
|
67
|
+
post_install_message:
|
|
68
|
+
rdoc_options: []
|
|
69
|
+
require_paths:
|
|
70
|
+
- lib
|
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: 2.5.0
|
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
|
+
requirements:
|
|
78
|
+
- - ">="
|
|
79
|
+
- !ruby/object:Gem::Version
|
|
80
|
+
version: '0'
|
|
81
|
+
requirements: []
|
|
82
|
+
rubygems_version: 3.2.15
|
|
83
|
+
signing_key:
|
|
84
|
+
specification_version: 4
|
|
85
|
+
summary: A simple way to manage default and env configuration in Rails.
|
|
86
|
+
test_files: []
|