config_reader 2.0.1 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.deepsource.toml +4 -0
- data/.github/workflows/ruby.yml +31 -0
- data/.nvmrc +1 -0
- data/.prettierrc +6 -0
- data/.rubocop.yml +64 -0
- data/.standard.yml +23 -0
- data/.vscode/settings.json +3 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +107 -34
- data/README.md +70 -0
- data/checksums/config_reader-2.0.2.gem.sha512 +1 -0
- data/config_reader.gemspec +0 -3
- data/lib/config_reader/magic_hash.rb +13 -16
- data/lib/config_reader/version.rb +1 -1
- data/lib/config_reader.rb +51 -26
- data/package.json +6 -0
- data/spec/config_reader_spec.rb +50 -50
- data/spec/no_key_no_error_config.rb +2 -2
- data/spec/sekrets_config.rb +10 -3
- data/spec/sekrets_ruby_3_2.yml +9 -0
- data/spec/sekrets_ruby_3_2.yml.enc +2 -0
- data/spec/spec_helper.rb +7 -7
- data/spec/test_config.rb +2 -2
- data/yarn.lock +13 -0
- metadata +20 -20
- data/README.rdoc +0 -59
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 78fbec26257c57d41e0e8969f2ec3ffb6ae73736134d6f3d885a4f44af401041
|
4
|
+
data.tar.gz: 5b224c74ab1a851277239b562d45b9411b94f5c40b56208303e0b62b73897448
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ebd13bac5a7341da8fa7cc8f367aa4bbbaf66a705e9c54f18ab76f5cfa85c93448bb99883372033d98b63f8bc7da6b0d92cb70d60bd5b2dbf17302de62cb4855
|
7
|
+
data.tar.gz: 36e99fa9848a53c5d10f640801f60ddb865ac4ab1aa6b84bd239a7abf8dd04e144e2334740d906ea0b056694654e1d2c668206352685eb6d7ee0845810fd255e
|
data/.deepsource.toml
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
name: Specs
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ "main" ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ "main" ]
|
8
|
+
|
9
|
+
permissions:
|
10
|
+
contents: read
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
test:
|
14
|
+
|
15
|
+
runs-on: ubuntu-latest
|
16
|
+
strategy:
|
17
|
+
matrix:
|
18
|
+
ruby-version: ['3.0', '3.1', '3.2']
|
19
|
+
|
20
|
+
steps:
|
21
|
+
- uses: actions/checkout@v3
|
22
|
+
- name: Set up Ruby
|
23
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
24
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
25
|
+
# uses: ruby/setup-ruby@v1
|
26
|
+
uses: ruby/setup-ruby@v1
|
27
|
+
with:
|
28
|
+
ruby-version: ${{ matrix.ruby-version }}
|
29
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
30
|
+
- name: Run tests
|
31
|
+
run: bundle exec rspec
|
data/.nvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
lts/Hydrogen
|
data/.prettierrc
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# https://docs.rubocop.org/en/latest/cops_layout/
|
2
|
+
|
3
|
+
require: rubocop-rspec
|
4
|
+
|
5
|
+
AllCops:
|
6
|
+
Exclude:
|
7
|
+
- "app/controllers/graphql_controller.rb"
|
8
|
+
- "app/graphql/vivo_point_schema.rb"
|
9
|
+
- "bin/**/*"
|
10
|
+
- "config/**/*"
|
11
|
+
- "db/**/*"
|
12
|
+
- "lib/util.rb"
|
13
|
+
- "node_modules/**/*"
|
14
|
+
- "script/**/*"
|
15
|
+
- "spec/**/*"
|
16
|
+
TargetRubyVersion: 2.6
|
17
|
+
|
18
|
+
Rails/UnknownEnv:
|
19
|
+
Environments:
|
20
|
+
- development
|
21
|
+
- test
|
22
|
+
- staging
|
23
|
+
- uat
|
24
|
+
- production
|
25
|
+
|
26
|
+
Layout/MultilineMethodCallIndentation:
|
27
|
+
EnforcedStyle: indented
|
28
|
+
|
29
|
+
Metrics/AbcSize:
|
30
|
+
Max: 20
|
31
|
+
|
32
|
+
Metrics/BlockLength:
|
33
|
+
ExcludedMethods:
|
34
|
+
- "generate_jwt!"
|
35
|
+
- "guard"
|
36
|
+
- "included"
|
37
|
+
- "namespace"
|
38
|
+
|
39
|
+
Metrics/MethodLength:
|
40
|
+
ExcludedMethods:
|
41
|
+
- "generate_jwt!"
|
42
|
+
- "add_resource_roles_to_user"
|
43
|
+
Max: 15
|
44
|
+
|
45
|
+
Style/StringLiterals:
|
46
|
+
EnforcedStyle: double_quotes
|
47
|
+
|
48
|
+
Style/AndOr:
|
49
|
+
Enabled: false
|
50
|
+
|
51
|
+
Style/Documentation:
|
52
|
+
Enabled: false
|
53
|
+
|
54
|
+
Style/MultilineMemoization:
|
55
|
+
EnforcedStyle: braces
|
56
|
+
|
57
|
+
Style/StringLiteralsInInterpolation:
|
58
|
+
EnforcedStyle: double_quotes
|
59
|
+
|
60
|
+
Style/FrozenStringLiteralComment:
|
61
|
+
Enabled: false
|
62
|
+
|
63
|
+
Style/YodaCondition:
|
64
|
+
EnforcedStyle: require_for_equality_operators_only
|
data/.standard.yml
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
default_ignores: false
|
2
|
+
fix: true
|
3
|
+
format: progress
|
4
|
+
parallel: true
|
5
|
+
|
6
|
+
ignore:
|
7
|
+
- "db/schema.rb"
|
8
|
+
- "vendor/**/*"
|
9
|
+
- "test/**/*":
|
10
|
+
- Layout/AlignHash
|
11
|
+
- "**/*":
|
12
|
+
- Layout/ArgumentAlignment
|
13
|
+
- Layout/ArrayAlignment
|
14
|
+
- Layout/BlockAlignment
|
15
|
+
- Layout/ClosingHeredocIndentation
|
16
|
+
- Layout/FirstArgumentIndentation
|
17
|
+
- Layout/MultilineMethodCallIndentation
|
18
|
+
- Layout/MultilineOperationIndentation
|
19
|
+
- Layout/SpaceInsideHashLiteralBraces
|
20
|
+
- Lint/AssignmentInCondition
|
21
|
+
- Lint/ShadowedException
|
22
|
+
- Style/MultilineMemoization
|
23
|
+
- Style/RescueStandardError
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -1,4 +1,13 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
|
+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
|
4
|
+
|
3
5
|
# Specify your gem's dependencies in config_reader.gemspec
|
4
6
|
gemspec
|
7
|
+
|
8
|
+
gem "sekrets", github: "UnderpantsGnome/sekrets", branch: "ruby-3-2-support"
|
9
|
+
|
10
|
+
group :development do
|
11
|
+
gem "prettier", "~> 4.0"
|
12
|
+
gem "standard", "~> 1.29"
|
13
|
+
end
|
data/Gemfile.lock
CHANGED
@@ -1,61 +1,134 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/UnderpantsGnome/sekrets.git
|
3
|
+
revision: 9f939c7789e2ed03902db9534b9df5cc8030c365
|
4
|
+
branch: ruby-3-2-support
|
5
|
+
specs:
|
6
|
+
sekrets (1.13.0)
|
7
|
+
coerce (~> 0.0.3)
|
8
|
+
fattr (~> 2.2)
|
9
|
+
highline (~> 1.6)
|
10
|
+
main (~> 6.1)
|
11
|
+
map (~> 6.3)
|
12
|
+
|
1
13
|
PATH
|
2
14
|
remote: .
|
3
15
|
specs:
|
4
|
-
config_reader (2.0.
|
5
|
-
deep_merge
|
16
|
+
config_reader (2.0.3)
|
6
17
|
|
7
18
|
GEM
|
8
19
|
remote: http://rubygems.org/
|
9
20
|
specs:
|
21
|
+
abbrev (0.1.2)
|
10
22
|
arrayfields (4.9.2)
|
23
|
+
ast (2.4.2)
|
11
24
|
chronic (0.10.2)
|
12
|
-
coderay (1.1.
|
25
|
+
coderay (1.1.3)
|
13
26
|
coerce (0.0.8)
|
14
27
|
chronic (>= 0.6.2)
|
15
|
-
|
16
|
-
diff-lcs (1.3)
|
28
|
+
diff-lcs (1.5.0)
|
17
29
|
fattr (2.4.0)
|
30
|
+
haml (6.3.0)
|
31
|
+
temple (>= 0.8.2)
|
32
|
+
thor
|
33
|
+
tilt
|
18
34
|
highline (1.7.10)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
35
|
+
json (2.7.2)
|
36
|
+
language_server-protocol (3.17.0.3)
|
37
|
+
lint_roller (1.1.0)
|
38
|
+
main (6.3.0)
|
39
|
+
arrayfields (~> 4.9, >= 4.9.2)
|
40
|
+
chronic (~> 0.10, >= 0.10.2)
|
41
|
+
fattr (~> 2.4, >= 2.4.0)
|
42
|
+
map (~> 6.6, >= 6.6.0)
|
24
43
|
map (6.6.0)
|
25
|
-
method_source (0.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
44
|
+
method_source (1.0.0)
|
45
|
+
parallel (1.24.0)
|
46
|
+
parser (3.3.1.0)
|
47
|
+
ast (~> 2.4.1)
|
48
|
+
racc
|
49
|
+
prettier (4.0.4)
|
50
|
+
syntax_tree (>= 4.0.1)
|
51
|
+
syntax_tree-haml (>= 2.0.0)
|
52
|
+
syntax_tree-rbs (>= 0.2.0)
|
53
|
+
prettier_print (1.2.1)
|
54
|
+
pry (0.14.2)
|
55
|
+
coderay (~> 1.1)
|
56
|
+
method_source (~> 1.0)
|
57
|
+
racc (1.7.3)
|
58
|
+
rainbow (3.1.1)
|
59
|
+
rake (13.0.6)
|
60
|
+
rbs (3.4.4)
|
61
|
+
abbrev
|
62
|
+
regexp_parser (2.9.0)
|
63
|
+
rexml (3.2.6)
|
64
|
+
rspec (3.12.0)
|
65
|
+
rspec-core (~> 3.12.0)
|
66
|
+
rspec-expectations (~> 3.12.0)
|
67
|
+
rspec-mocks (~> 3.12.0)
|
68
|
+
rspec-core (3.12.1)
|
69
|
+
rspec-support (~> 3.12.0)
|
70
|
+
rspec-expectations (3.12.2)
|
37
71
|
diff-lcs (>= 1.2.0, < 2.0)
|
38
|
-
rspec-support (~> 3.
|
39
|
-
rspec-mocks (3.
|
72
|
+
rspec-support (~> 3.12.0)
|
73
|
+
rspec-mocks (3.12.3)
|
40
74
|
diff-lcs (>= 1.2.0, < 2.0)
|
41
|
-
rspec-support (~> 3.
|
42
|
-
rspec-support (3.
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
75
|
+
rspec-support (~> 3.12.0)
|
76
|
+
rspec-support (3.12.0)
|
77
|
+
rubocop (1.62.1)
|
78
|
+
json (~> 2.3)
|
79
|
+
language_server-protocol (>= 3.17.0)
|
80
|
+
parallel (~> 1.10)
|
81
|
+
parser (>= 3.3.0.2)
|
82
|
+
rainbow (>= 2.2.2, < 4.0)
|
83
|
+
regexp_parser (>= 1.8, < 3.0)
|
84
|
+
rexml (>= 3.2.5, < 4.0)
|
85
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
86
|
+
ruby-progressbar (~> 1.7)
|
87
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
88
|
+
rubocop-ast (1.31.3)
|
89
|
+
parser (>= 3.3.1.0)
|
90
|
+
rubocop-performance (1.20.2)
|
91
|
+
rubocop (>= 1.48.1, < 2.0)
|
92
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
93
|
+
ruby-progressbar (1.13.0)
|
94
|
+
standard (1.35.1)
|
95
|
+
language_server-protocol (~> 3.17.0.2)
|
96
|
+
lint_roller (~> 1.0)
|
97
|
+
rubocop (~> 1.62.0)
|
98
|
+
standard-custom (~> 1.0.0)
|
99
|
+
standard-performance (~> 1.3)
|
100
|
+
standard-custom (1.0.2)
|
101
|
+
lint_roller (~> 1.0)
|
102
|
+
rubocop (~> 1.50)
|
103
|
+
standard-performance (1.3.1)
|
104
|
+
lint_roller (~> 1.1)
|
105
|
+
rubocop-performance (~> 1.20.2)
|
106
|
+
syntax_tree (6.2.0)
|
107
|
+
prettier_print (>= 1.2.0)
|
108
|
+
syntax_tree-haml (4.0.3)
|
109
|
+
haml (>= 5.2)
|
110
|
+
prettier_print (>= 1.2.1)
|
111
|
+
syntax_tree (>= 6.0.0)
|
112
|
+
syntax_tree-rbs (1.0.0)
|
113
|
+
prettier_print
|
114
|
+
rbs
|
115
|
+
syntax_tree (>= 2.0.1)
|
116
|
+
temple (0.10.3)
|
117
|
+
thor (1.3.1)
|
118
|
+
tilt (2.3.0)
|
119
|
+
unicode-display_width (2.5.0)
|
49
120
|
|
50
121
|
PLATFORMS
|
51
122
|
ruby
|
52
123
|
|
53
124
|
DEPENDENCIES
|
54
125
|
config_reader!
|
126
|
+
prettier (~> 4.0)
|
55
127
|
pry
|
56
128
|
rake
|
57
129
|
rspec (~> 3.9)
|
58
|
-
sekrets
|
130
|
+
sekrets!
|
131
|
+
standard (~> 1.29)
|
59
132
|
|
60
133
|
BUNDLED WITH
|
61
|
-
|
134
|
+
2.3.13
|
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# ConfigReader
|
2
|
+
|
3
|
+
[![Maintainability](https://codeclimate.com/github/UnderpantsGnome/config_reader-gem.png)](https://codeclimate.com/github/UnderpantsGnome/config_reader-gem)
|
4
|
+
![Specs](https://github.com/UnderpantsGnome/config_reader-gem/actions/workflows/ruby.yml/badge.svg)
|
5
|
+
![Ruby 3.0+](https://img.shields.io/badge/Ruby-%3E%3D%203.0-success)
|
6
|
+
|
7
|
+
Provides a way to manage environment specific configuration settings. It will
|
8
|
+
use the defaults for any environment and override any values you specify for
|
9
|
+
an environment.
|
10
|
+
|
11
|
+
Example config file:
|
12
|
+
|
13
|
+
defaults:
|
14
|
+
site_url: http://localhost:3000
|
15
|
+
host_name: example.com
|
16
|
+
mail_from: noreply@example.com
|
17
|
+
site_name: example
|
18
|
+
admin_email: admin@example.com
|
19
|
+
|
20
|
+
production:
|
21
|
+
site_url: http://example.com
|
22
|
+
|
23
|
+
## Ruby 3.1 and 3.2
|
24
|
+
|
25
|
+
If you want to use Sekrets with these versions of Ruby you need to use this version until upstream gets updated.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem "sekrets",
|
29
|
+
github: "UnderpantsGnome/sekrets",
|
30
|
+
branch: "ruby-3-2-support"
|
31
|
+
```
|
32
|
+
|
33
|
+
## Sekrets
|
34
|
+
|
35
|
+
Includes Sekrets integration. See <https://github.com/ahoward/sekrets> for more
|
36
|
+
information.
|
37
|
+
|
38
|
+
The format of the sekrets file is the same as the regular file.
|
39
|
+
|
40
|
+
## Setup
|
41
|
+
|
42
|
+
class MyConfig < ConfigReader
|
43
|
+
configure do |config|
|
44
|
+
config.environment = Rails.env # (set this however you access the env in your app)
|
45
|
+
config.config_file = 'config/my_config.yml'
|
46
|
+
config.sekrets_file = 'config/my_config.yml.enc' # (default nil)
|
47
|
+
config.ignore_missing_keys = true # (default false, raises KeyError)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
## Usage
|
52
|
+
|
53
|
+
MyConfig.mail_from #=> noreply@example.com
|
54
|
+
MyConfig[:mail_from] #=> noreply@example.com
|
55
|
+
MyConfig['mail_from'] #=> noreply@example.com
|
56
|
+
|
57
|
+
## Note on Patches/Pull Requests
|
58
|
+
|
59
|
+
* Fork the project.
|
60
|
+
* Make your feature addition or bug fix.
|
61
|
+
* Add tests for it. This is important so I don't break it in a future
|
62
|
+
version unintentionally.
|
63
|
+
* Commit, do not mess with rakefile, version, or history. (if you want to
|
64
|
+
have your own version, that is fine but bump version in a commit by itself
|
65
|
+
I can ignore when I pull)
|
66
|
+
* Send me a pull request. Bonus points for topic branches.
|
67
|
+
|
68
|
+
## Copyright
|
69
|
+
|
70
|
+
Copyright (c) Michael Moen. See LICENSE for details.
|
@@ -0,0 +1 @@
|
|
1
|
+
ecd65abf0eef2de025cf028a84de56bddeb3f6fae18c78565e8f647a3bc79eec777b542c5de26bb74c1c5f7b5cf2ad6eaf71e8061f3729141ac6d38e48367aa7
|
data/config_reader.gemspec
CHANGED
@@ -16,9 +16,6 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
17
|
s.require_paths = ['lib']
|
18
18
|
|
19
|
-
# specify any dependencies here; for example:
|
20
|
-
s.add_dependency 'deep_merge'
|
21
|
-
|
22
19
|
s.add_development_dependency 'rake'
|
23
20
|
s.add_development_dependency 'rspec', '~> 3.9'
|
24
21
|
s.add_development_dependency 'sekrets', '~> 1'
|
@@ -2,15 +2,15 @@ class ConfigReader
|
|
2
2
|
class MagicHash < Hash
|
3
3
|
attr_accessor :ignore_missing_keys
|
4
4
|
|
5
|
-
def self.convert_hash(hash, ignore_missing_keys=false)
|
5
|
+
def self.convert_hash(hash, ignore_missing_keys = false)
|
6
6
|
magic_hash = new
|
7
7
|
magic_hash.ignore_missing_keys = ignore_missing_keys
|
8
8
|
|
9
9
|
hash.each_pair do |key, value|
|
10
|
-
if value.is_a?(Hash)
|
11
|
-
|
10
|
+
magic_hash[key.to_sym] = if value.is_a?(Hash)
|
11
|
+
convert_hash(value, ignore_missing_keys)
|
12
12
|
else
|
13
|
-
|
13
|
+
value
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -18,22 +18,19 @@ class ConfigReader
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def [](key)
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
raise e unless ignore_missing_keys
|
25
|
-
end
|
21
|
+
fetch(key.to_sym)
|
22
|
+
rescue KeyError => e
|
23
|
+
raise e unless ignore_missing_keys
|
26
24
|
end
|
27
25
|
|
28
26
|
def method_missing(key, *args, &block)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
super
|
33
|
-
rescue KeyError, NoMethodError => e
|
34
|
-
raise e unless ignore_missing_keys
|
35
|
-
end
|
27
|
+
key?(key) ? fetch(key) : super
|
28
|
+
rescue KeyError, NoMethodError => e
|
29
|
+
raise e unless ignore_missing_keys
|
36
30
|
end
|
37
31
|
|
32
|
+
def respond_to_missing?(m, *)
|
33
|
+
config.key?(m)
|
34
|
+
end
|
38
35
|
end
|
39
36
|
end
|
data/lib/config_reader.rb
CHANGED
@@ -1,23 +1,28 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require 'deep_merge'
|
1
|
+
require "config_reader/version"
|
2
|
+
require "config_reader/magic_hash"
|
3
|
+
require "yaml"
|
5
4
|
|
6
5
|
begin
|
7
|
-
require
|
6
|
+
require "erb"
|
8
7
|
rescue LoadError
|
9
8
|
puts "ERB not found, you won't be able to use ERB in your config"
|
10
9
|
end
|
11
10
|
|
12
11
|
class ConfigReader
|
13
12
|
class << self
|
14
|
-
|
13
|
+
attr_reader :configuration
|
15
14
|
|
16
15
|
def config
|
17
16
|
@config = nil unless defined?(@config)
|
18
17
|
@config ||= reload
|
19
18
|
end
|
20
19
|
|
20
|
+
def dig(*args)
|
21
|
+
args.map!(&:to_sym) if args.respond_to?(:map!)
|
22
|
+
|
23
|
+
config.dig(*args)
|
24
|
+
end
|
25
|
+
|
21
26
|
def reload
|
22
27
|
merge_configs(load_config, load_sekrets)
|
23
28
|
end
|
@@ -29,7 +34,7 @@ class ConfigReader
|
|
29
34
|
def find_config
|
30
35
|
return configuration.config_file if File.exist?(configuration.config_file)
|
31
36
|
|
32
|
-
%w
|
37
|
+
%w[. config].each do |dir|
|
33
38
|
config_file = File.join(dir, configuration.config_file)
|
34
39
|
return config_file if File.exist?(config_file)
|
35
40
|
end
|
@@ -38,27 +43,32 @@ class ConfigReader
|
|
38
43
|
end
|
39
44
|
|
40
45
|
def method_missing(key, *args, &block)
|
41
|
-
if key.to_s.end_with?(
|
42
|
-
raise ArgumentError.new(
|
46
|
+
if key.to_s.end_with?("=")
|
47
|
+
raise ArgumentError.new("ConfigReader is immutable")
|
43
48
|
end
|
44
49
|
|
45
50
|
config[key] || nil
|
46
51
|
end
|
47
52
|
|
53
|
+
def respond_to_missing?(m, *)
|
54
|
+
config.key?(m)
|
55
|
+
end
|
56
|
+
|
48
57
|
def inspect
|
49
58
|
puts config.inspect
|
50
59
|
end
|
51
60
|
|
52
61
|
def load_config
|
53
|
-
raise
|
62
|
+
raise "No config file set" unless find_config
|
54
63
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
64
|
+
conf =
|
65
|
+
if defined?(ERB)
|
66
|
+
YAML.load(ERB.new(File.read(find_config)).result)
|
67
|
+
else
|
68
|
+
YAML.load_file(File.read(find_config))
|
69
|
+
end
|
60
70
|
|
61
|
-
raise
|
71
|
+
raise "No config found" unless conf
|
62
72
|
|
63
73
|
conf
|
64
74
|
end
|
@@ -68,11 +78,11 @@ class ConfigReader
|
|
68
78
|
|
69
79
|
if configuration.sekrets_file
|
70
80
|
begin
|
71
|
-
require
|
81
|
+
require "sekrets"
|
72
82
|
sekrets = ::Sekrets.settings_for(configuration.sekrets_file)
|
73
|
-
raise
|
83
|
+
raise "No sekrets found" unless sekrets
|
74
84
|
rescue LoadError
|
75
|
-
|
85
|
+
warn "You specified a sekrets_file, but the sekrets gem isn't available."
|
76
86
|
end
|
77
87
|
end
|
78
88
|
|
@@ -82,12 +92,16 @@ class ConfigReader
|
|
82
92
|
def merge_configs(conf, sekrets)
|
83
93
|
env = configuration.environment
|
84
94
|
|
85
|
-
|
86
|
-
_conf.deep_merge!(sekrets['defaults']) if sekrets && sekrets['defaults']
|
87
|
-
_conf.deep_merge!(conf[env]) if conf[env]
|
88
|
-
_conf.deep_merge!(sekrets[env]) if sekrets && sekrets[env]
|
95
|
+
defaults = conf["defaults"]
|
89
96
|
|
90
|
-
|
97
|
+
if sekrets && sekrets["defaults"]
|
98
|
+
deep_merge!(defaults, sekrets["defaults"])
|
99
|
+
end
|
100
|
+
|
101
|
+
deep_merge!(defaults, conf[env]) if conf[env]
|
102
|
+
deep_merge!(defaults, sekrets[env]) if sekrets && sekrets[env]
|
103
|
+
|
104
|
+
MagicHash.convert_hash(defaults, configuration.ignore_missing_keys)
|
91
105
|
end
|
92
106
|
|
93
107
|
def configure
|
@@ -99,10 +113,22 @@ class ConfigReader
|
|
99
113
|
@configuration ||= Configuration.new
|
100
114
|
end
|
101
115
|
|
116
|
+
def deep_merge!(hash, other_hash)
|
117
|
+
hash.merge!(other_hash) do |key, this_val, other_val|
|
118
|
+
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
119
|
+
deep_merge!(this_val, other_val)
|
120
|
+
else
|
121
|
+
other_val
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
102
125
|
end
|
103
126
|
|
104
127
|
class Configuration
|
105
|
-
attr_accessor :config_file,
|
128
|
+
attr_accessor :config_file,
|
129
|
+
:sekrets_file,
|
130
|
+
:ignore_missing_keys,
|
131
|
+
:environment
|
106
132
|
|
107
133
|
def initialize
|
108
134
|
@config_file = nil
|
@@ -111,5 +137,4 @@ class ConfigReader
|
|
111
137
|
@environment = nil
|
112
138
|
end
|
113
139
|
end
|
114
|
-
|
115
140
|
end
|
data/package.json
ADDED
data/spec/config_reader_spec.rb
CHANGED
@@ -1,96 +1,97 @@
|
|
1
|
-
require
|
1
|
+
require "spec_helper"
|
2
2
|
|
3
3
|
describe "ConfigReader" do
|
4
4
|
describe "setting values" do
|
5
|
-
|
6
5
|
it "should fail with []=" do
|
7
|
-
expect {
|
8
|
-
|
9
|
-
|
6
|
+
expect { TestConfig[:app_name] = "test_app" }.to raise_error(
|
7
|
+
ArgumentError
|
8
|
+
)
|
10
9
|
end
|
11
10
|
|
12
11
|
it "should fail with #key =" do
|
13
|
-
expect {
|
14
|
-
TestConfig.app_name = 'test_app'
|
15
|
-
}.to raise_error(ArgumentError)
|
12
|
+
expect { TestConfig.app_name = "test_app" }.to raise_error(ArgumentError)
|
16
13
|
end
|
17
14
|
end
|
18
15
|
|
19
16
|
describe "default KeyNotFound behavior" do
|
20
17
|
it "should raise on missing key with [] accessor" do
|
21
|
-
expect {
|
22
|
-
TestConfig[:no_key]
|
23
|
-
}.to raise_error(KeyError)
|
18
|
+
expect { TestConfig[:no_key] }.to raise_error(KeyError)
|
24
19
|
end
|
25
20
|
|
26
21
|
it "should raise on missing key with #key accessor" do
|
27
|
-
expect {
|
28
|
-
TestConfig.no_key
|
29
|
-
}.to raise_error(KeyError)
|
22
|
+
expect { TestConfig.no_key }.to raise_error(KeyError)
|
30
23
|
end
|
31
24
|
|
32
25
|
it "should raise on missing nested key with [] accessor" do
|
33
|
-
expect {
|
34
|
-
TestConfig[:nested_key][:missing]
|
35
|
-
}.to raise_error(KeyError)
|
26
|
+
expect { TestConfig[:nested_key][:missing] }.to raise_error(KeyError)
|
36
27
|
end
|
37
28
|
|
38
29
|
it "should raise on missing nested key with #key accessor" do
|
39
|
-
expect {
|
40
|
-
|
41
|
-
|
30
|
+
expect { TestConfig.nested_key.missing }.to raise_error(NoMethodError)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#dig" do
|
35
|
+
it "finds values as symbols" do
|
36
|
+
expect(TestConfig.dig(*%i[nested_key value])).to eq("test")
|
37
|
+
end
|
38
|
+
|
39
|
+
it "finds values as strings" do
|
40
|
+
expect(TestConfig.dig(*%w[nested_key value])).to eq("test")
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
45
44
|
describe "ignoring KeyNotFound" do
|
46
45
|
it "should not raise on missing key with [] accessor" do
|
47
|
-
expect {
|
48
|
-
NoKeyNoErrorConfig[:no_key]
|
49
|
-
}.to_not raise_error
|
46
|
+
expect { NoKeyNoErrorConfig[:no_key] }.to_not raise_error
|
50
47
|
end
|
51
48
|
|
52
49
|
it "should not raise on missing key with #key accessor" do
|
53
|
-
expect {
|
54
|
-
NoKeyNoErrorConfig.no_key
|
55
|
-
}.to_not raise_error
|
50
|
+
expect { NoKeyNoErrorConfig.no_key }.to_not raise_error
|
56
51
|
end
|
57
52
|
|
58
53
|
it "should not raise on missing nested key with [] accessor" do
|
59
|
-
expect {
|
60
|
-
NoKeyNoErrorConfig[:nested_key][:missing]
|
61
|
-
}.to_not raise_error
|
54
|
+
expect { NoKeyNoErrorConfig[:nested_key][:missing] }.to_not raise_error
|
62
55
|
end
|
63
56
|
|
64
57
|
it "should not raise on missing nested key with #key accessor" do
|
65
|
-
expect {
|
66
|
-
|
67
|
-
|
58
|
+
expect { NoKeyNoErrorConfig.nested_key.missing }.to_not raise_error
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "method_missing" do
|
63
|
+
it "handles respond_to?" do
|
64
|
+
expect(TestConfig.respond_to?(:not_a_key)).to be_falsey
|
65
|
+
end
|
66
|
+
|
67
|
+
it "handles respond_to_missing?" do
|
68
|
+
expect(TestConfig.respond_to_missing?(:not_a_key)).to be_falsey
|
68
69
|
end
|
69
70
|
end
|
70
71
|
|
71
72
|
describe "parsing a YAML file" do
|
72
73
|
it "should find values with method_missing" do
|
73
|
-
expect(TestConfig.app_name).to eq(
|
74
|
+
expect(TestConfig.app_name).to eq("test_app")
|
74
75
|
end
|
75
76
|
|
76
77
|
it "should find values using [] and a string" do
|
77
|
-
expect(TestConfig[
|
78
|
+
expect(TestConfig["app_name"]).to eq("test_app")
|
78
79
|
end
|
79
80
|
|
80
81
|
it "should find values using [] and a symbol" do
|
81
|
-
expect(TestConfig[:app_name]).to eq(
|
82
|
+
expect(TestConfig[:app_name]).to eq("test_app")
|
82
83
|
end
|
83
84
|
|
84
85
|
it "should find nested values using method_missing" do
|
85
|
-
expect(TestConfig.nested_key.value).to eq(
|
86
|
+
expect(TestConfig.nested_key.value).to eq("test")
|
86
87
|
end
|
87
88
|
|
88
89
|
it "should find nested values using [] and a symbol" do
|
89
|
-
expect(TestConfig[:nested_key][:value]).to eq(
|
90
|
+
expect(TestConfig[:nested_key][:value]).to eq("test")
|
90
91
|
end
|
91
92
|
|
92
93
|
it "should find nested values using [] and a string" do
|
93
|
-
expect(TestConfig[
|
94
|
+
expect(TestConfig["nested_key"]["value"]).to eq("test")
|
94
95
|
end
|
95
96
|
|
96
97
|
it "should not find sekrets only nested values using method_missing" do
|
@@ -102,47 +103,46 @@ describe "ConfigReader" do
|
|
102
103
|
end
|
103
104
|
|
104
105
|
it "should not find sekrets only nested values using [] and a string" do
|
105
|
-
expect { TestConfig[
|
106
|
+
expect { TestConfig["sekrets_only"]["value"] }.to raise_error(KeyError)
|
106
107
|
end
|
107
|
-
|
108
108
|
end
|
109
109
|
|
110
|
-
context
|
110
|
+
context "using sekrets" do
|
111
111
|
describe "parsing a YAML file" do
|
112
112
|
it "should find values with method_missing" do
|
113
|
-
expect(SekretsConfig.app_name).to eq(
|
113
|
+
expect(SekretsConfig.app_name).to eq("test_app_sekret")
|
114
114
|
end
|
115
115
|
|
116
116
|
it "should find values using [] and a string" do
|
117
|
-
expect(SekretsConfig[
|
117
|
+
expect(SekretsConfig["app_name"]).to eq("test_app_sekret")
|
118
118
|
end
|
119
119
|
|
120
120
|
it "should find values using [] and a symbol" do
|
121
|
-
expect(SekretsConfig[:app_name]).to eq(
|
121
|
+
expect(SekretsConfig[:app_name]).to eq("test_app_sekret")
|
122
122
|
end
|
123
123
|
|
124
124
|
it "should find nested values using method_missing" do
|
125
|
-
expect(SekretsConfig.nested_key.value).to eq(
|
125
|
+
expect(SekretsConfig.nested_key.value).to eq("test_sekret")
|
126
126
|
end
|
127
127
|
|
128
128
|
it "should find nested values using [] and a symbol" do
|
129
|
-
expect(SekretsConfig[:nested_key][:value]).to eq(
|
129
|
+
expect(SekretsConfig[:nested_key][:value]).to eq("test_sekret")
|
130
130
|
end
|
131
131
|
|
132
132
|
it "should find nested values using [] and a string" do
|
133
|
-
expect(SekretsConfig[
|
133
|
+
expect(SekretsConfig["nested_key"]["value"]).to eq("test_sekret")
|
134
134
|
end
|
135
135
|
|
136
136
|
it "should find sekrets only nested values using method_missing" do
|
137
|
-
expect(SekretsConfig.nested_key.value).to eq(
|
137
|
+
expect(SekretsConfig.nested_key.value).to eq("test_sekret")
|
138
138
|
end
|
139
139
|
|
140
140
|
it "should find sekrets only nested values using [] and a symbol" do
|
141
|
-
expect(SekretsConfig[:nested_key][:value]).to eq(
|
141
|
+
expect(SekretsConfig[:nested_key][:value]).to eq("test_sekret")
|
142
142
|
end
|
143
143
|
|
144
144
|
it "should find sekrets only nested values using [] and a string" do
|
145
|
-
expect(SekretsConfig[
|
145
|
+
expect(SekretsConfig["nested_key"]["value"]).to eq("test_sekret")
|
146
146
|
end
|
147
147
|
|
148
148
|
it "shouldn't need to have all keys duplicated in the environment section" do
|
data/spec/sekrets_config.rb
CHANGED
@@ -1,7 +1,14 @@
|
|
1
1
|
class SekretsConfig < ConfigReader
|
2
|
+
secret_file =
|
3
|
+
if RUBY_VERSION > "3.0"
|
4
|
+
"spec/sekrets_ruby_3_2.yml.enc"
|
5
|
+
else
|
6
|
+
"spec/sekrets_config.yml.enc"
|
7
|
+
end
|
8
|
+
|
2
9
|
configure do |config|
|
3
|
-
config.environment =
|
4
|
-
config.config_file =
|
5
|
-
config.sekrets_file =
|
10
|
+
config.environment = "test"
|
11
|
+
config.config_file = "spec/test_config.yml"
|
12
|
+
config.sekrets_file = secret_file
|
6
13
|
end
|
7
14
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
3
3
|
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
4
|
+
require "config_reader"
|
5
|
+
require "test_config"
|
6
|
+
require "sekrets_config"
|
7
|
+
require "no_key_no_error_config"
|
8
8
|
|
9
|
-
ENV[
|
10
|
-
ENV[
|
9
|
+
ENV["RACK_ENV"] = "test"
|
10
|
+
ENV["SEKRETS_KEY"] = "ruby2.4_needs_longer_keys"
|
data/spec/test_config.rb
CHANGED
data/yarn.lock
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
2
|
+
# yarn lockfile v1
|
3
|
+
|
4
|
+
|
5
|
+
"@prettier/plugin-ruby@^4.0.4":
|
6
|
+
version "4.0.4"
|
7
|
+
resolved "https://registry.yarnpkg.com/@prettier/plugin-ruby/-/plugin-ruby-4.0.4.tgz#73d85fc2a1731a3f62b57ac3116cf1c234027cb6"
|
8
|
+
integrity sha512-lCpvfS/dQU5WrwN3AQ5vR8qrvj2h5gE41X08NNzAAXvHdM4zwwGRcP2sHSxfu6n6No+ljWCVx95NvJPFTTjCTg==
|
9
|
+
|
10
|
+
prettier@^3.2.5:
|
11
|
+
version "3.2.5"
|
12
|
+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.2.5.tgz#e52bc3090586e824964a8813b09aba6233b28368"
|
13
|
+
integrity sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==
|
metadata
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: config_reader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Moen
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-05-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: deep_merge
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rake
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
@@ -87,25 +73,37 @@ executables: []
|
|
87
73
|
extensions: []
|
88
74
|
extra_rdoc_files: []
|
89
75
|
files:
|
76
|
+
- ".deepsource.toml"
|
77
|
+
- ".github/workflows/ruby.yml"
|
90
78
|
- ".gitignore"
|
79
|
+
- ".nvmrc"
|
80
|
+
- ".prettierrc"
|
81
|
+
- ".rubocop.yml"
|
82
|
+
- ".standard.yml"
|
91
83
|
- ".travis.yml"
|
84
|
+
- ".vscode/settings.json"
|
92
85
|
- CHANGELOG.md
|
93
86
|
- Gemfile
|
94
87
|
- Gemfile.lock
|
95
88
|
- License.txt
|
96
|
-
- README.
|
89
|
+
- README.md
|
97
90
|
- Rakefile
|
91
|
+
- checksums/config_reader-2.0.2.gem.sha512
|
98
92
|
- config_reader.gemspec
|
99
93
|
- lib/config_reader.rb
|
100
94
|
- lib/config_reader/magic_hash.rb
|
101
95
|
- lib/config_reader/version.rb
|
96
|
+
- package.json
|
102
97
|
- spec/config_reader_spec.rb
|
103
98
|
- spec/no_key_no_error_config.rb
|
104
99
|
- spec/sekrets_config.rb
|
105
100
|
- spec/sekrets_config.yml.enc
|
101
|
+
- spec/sekrets_ruby_3_2.yml
|
102
|
+
- spec/sekrets_ruby_3_2.yml.enc
|
106
103
|
- spec/spec_helper.rb
|
107
104
|
- spec/test_config.rb
|
108
105
|
- spec/test_config.yml
|
106
|
+
- yarn.lock
|
109
107
|
homepage: https://github.com/UnderpantsGnome/config_reader-gem
|
110
108
|
licenses: []
|
111
109
|
metadata: {}
|
@@ -125,8 +123,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
123
|
- !ruby/object:Gem::Version
|
126
124
|
version: '0'
|
127
125
|
requirements: []
|
128
|
-
rubygems_version: 3.
|
129
|
-
signing_key:
|
126
|
+
rubygems_version: 3.3.26
|
127
|
+
signing_key:
|
130
128
|
specification_version: 4
|
131
129
|
summary: Provides a way to manage environment specific configuration settings.
|
132
130
|
test_files:
|
@@ -134,6 +132,8 @@ test_files:
|
|
134
132
|
- spec/no_key_no_error_config.rb
|
135
133
|
- spec/sekrets_config.rb
|
136
134
|
- spec/sekrets_config.yml.enc
|
135
|
+
- spec/sekrets_ruby_3_2.yml
|
136
|
+
- spec/sekrets_ruby_3_2.yml.enc
|
137
137
|
- spec/spec_helper.rb
|
138
138
|
- spec/test_config.rb
|
139
139
|
- spec/test_config.yml
|
data/README.rdoc
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
= ConfigReader
|
2
|
-
|
3
|
-
{<img src="https://codeclimate.com/github/UnderpantsGnome/config_reader-gem.png" />}[https://codeclimate.com/github/UnderpantsGnome/config_reader-gem] {<img src="https://travis-ci.org/UnderpantsGnome/config_reader-gem.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/UnderpantsGnome/config_reader-gem]
|
4
|
-
|
5
|
-
Provides a way to manage environment specific configuration settings. It will use the defaults for any environment and override any values you specify for an environment.
|
6
|
-
|
7
|
-
Example config file:
|
8
|
-
|
9
|
-
defaults:
|
10
|
-
site_url: http://localhost:3000
|
11
|
-
host_name: example.com
|
12
|
-
mail_from: noreply@example.com
|
13
|
-
site_name: example
|
14
|
-
admin_email: admin@example.com
|
15
|
-
blackbird: true
|
16
|
-
|
17
|
-
production:
|
18
|
-
site_url: http://example.com
|
19
|
-
blackbird: false
|
20
|
-
|
21
|
-
test:
|
22
|
-
blackbird: false
|
23
|
-
|
24
|
-
== Sekrets
|
25
|
-
|
26
|
-
Includes Sekrets integration. See https://github.com/ahoward/sekrets for more information.
|
27
|
-
|
28
|
-
The format of the sekrets file is the same as the regular file.
|
29
|
-
|
30
|
-
== Setup
|
31
|
-
|
32
|
-
class MyConfig < ConfigReader
|
33
|
-
configure do |config|
|
34
|
-
config.environment = Rails.env # (set this however you access the env in your app)
|
35
|
-
config.config_file = 'config/my_config.yml'
|
36
|
-
config.sekrets_file = 'config/my_config.yml.enc' # (default nil)
|
37
|
-
config.ignore_missing_keys = true # (default false, raises KeyError)
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
== Usage
|
42
|
-
|
43
|
-
MyConfig.mail_from #=> noreply@example.com
|
44
|
-
MyConfig[:mail_from] #=> noreply@example.com
|
45
|
-
MyConfig['mail_from'] #=> noreply@example.com
|
46
|
-
|
47
|
-
== Note on Patches/Pull Requests
|
48
|
-
|
49
|
-
* Fork the project.
|
50
|
-
* Make your feature addition or bug fix.
|
51
|
-
* Add tests for it. This is important so I don't break it in a
|
52
|
-
future version unintentionally.
|
53
|
-
* Commit, do not mess with rakefile, version, or history.
|
54
|
-
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
55
|
-
* Send me a pull request. Bonus points for topic branches.
|
56
|
-
|
57
|
-
== Copyright
|
58
|
-
|
59
|
-
Copyright (c) 2008-2018 Michael Moen. See LICENSE for details.
|