rails-options_config 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +93 -0
- data/Rakefile +8 -0
- data/lib/rails/options_config/application.rb +75 -0
- data/lib/rails/options_config/engine.rb +11 -0
- data/lib/rails/options_config/key_override_error.rb +6 -0
- data/lib/rails/options_config/version.rb +5 -0
- data/lib/rails/options_config/yaml/env_visitor.rb +13 -0
- data/lib/rails/options_config.rb +3 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b279dd643c9c6b7d0fc3145b7a6eb377be0c9c34be871046b07d9e2066010027
|
4
|
+
data.tar.gz: 61c959dee4601f5912a4f9e392755630013f3e92bc9da9682bc18285dd0bb1d5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 283b34dfcf65bf90501089299200ae5399b89491f56b027d91759aab879864ae099091eb8893c2e33b5b0cf632ce103dfec1ddf0625293890f65f3fa5f140069
|
7
|
+
data.tar.gz: 932a06cdc8996134b8cbcf690a0a79164793393d30869041ac3e46a06f3fcfe9342759b39b0aa1afcc7b910887622b3966042d6191f83edfb5b494379edcb2ee
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# Rails Options
|
2
|
+
|
3
|
+
A uniform interface to multiple option YAML files.
|
4
|
+
|
5
|
+
As a project's size increases, its credentials file tends to become unmanageable. With Rails Options you can split the credentials into smaller separate YAML files.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'rails-options', '~> 1.0'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
$ bundle
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Create any YAML (`.yml` or `.yaml`) file inside the `config/options` directory. You will find everything in `Rails.application.options`, including the credentials:
|
24
|
+
|
25
|
+
```yaml
|
26
|
+
# config/options/pokemon.yml
|
27
|
+
pokemon_api:
|
28
|
+
base_url: https://pokeapi.co/api/v2
|
29
|
+
```
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
Rails.application.options
|
33
|
+
# => {pokemon_api: {base_url: "https://pokeapi.co/api/v2"}}
|
34
|
+
```
|
35
|
+
|
36
|
+
`options` actually returns an `ActiveSupport::OrderedOptions`, which lets you access options as methods, as well as nested hash keys:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
Rails.application.options.dig(:pokemon_api, :base_url)
|
40
|
+
Rails.application.options.pokemon_api.base_url
|
41
|
+
```
|
42
|
+
|
43
|
+
The YAML files can be encrypted: if the filename ends in `.enc`, the content will be assumed to have been encrypted with `rails encrypted:edit`. Notice that the command will *not* automatically append the `.enc` extension.
|
44
|
+
|
45
|
+
If a filename contains an environment tag, the content will be considered only in the respective environment: `config/options/pokemon.development.yml` will only be loaded in the `development` environment.
|
46
|
+
|
47
|
+
Files with the same root name will overwrite each other: environment specific files will overwrite generic ones, and encrypted files will overwrite clear text ones. This way you can provide sensible defaults in the generic files, with encryption if necessary, and override them in specific environments (for example using a `.development` gitignored file to use a local mocked server instead of the real thing).
|
48
|
+
|
49
|
+
Options can also be picked up from environment variables. Any `!env/VAR` YAML tag will be substituted for the value of the `VAR` variable, anywhere in the file (even in keys). For example, setting `VAR=variable`:
|
50
|
+
|
51
|
+
```yaml
|
52
|
+
a_key: !env/VAR
|
53
|
+
```
|
54
|
+
|
55
|
+
will become:
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
{
|
59
|
+
a_key: 'variable'
|
60
|
+
}
|
61
|
+
```
|
62
|
+
|
63
|
+
### Options
|
64
|
+
|
65
|
+
You can set these options in `application.rb` or in any of the `environments/*.rb` files.
|
66
|
+
|
67
|
+
- `config.options.roots` : the directories, relative to `Rails.root`, in which to look for options files. You can either add to the existing array with `<<` or assign a new array. Default: `['config']`.
|
68
|
+
- `config.options.paths` : the patterns (in the format of `Dir.glob`) corresponding to the options files. They will be looked for in all the `roots` directories. Default: `['options.{yml,yaml}{.enc,}', 'options/**/*.{yml,yaml}{.enc,}']`.
|
69
|
+
- `config.options.raise_on_override` : set this to `true` if you want an exception to be raised if any key would be set by multiple files. If this is `false` and a "conflict" happens, one key will overwrite the other, but the order is undefined. This only applies to files with a different `path/to/file`, not to conflicts between the base and any environment-specific version. Default: `false`.
|
70
|
+
|
71
|
+
## Version numbers
|
72
|
+
|
73
|
+
Rails Options loosely follows [Semantic Versioning](https://semver.org/), with a hard guarantee that breaking changes to the public API will always coincide with an increase to the `MAJOR` number.
|
74
|
+
|
75
|
+
Version numbers are in three parts: `MAJOR.MINOR.PATCH`.
|
76
|
+
|
77
|
+
- Breaking changes to the public API increment the `MAJOR`. There may also be changes that would otherwise increase the `MINOR` or the `PATCH`.
|
78
|
+
- Additions, deprecations, and "big" non breaking changes to the public API increment the `MINOR`. There may also be changes that would otherwise increase the `PATCH`.
|
79
|
+
- Bug fixes and "small" non breaking changes to the public API increment the `PATCH`.
|
80
|
+
|
81
|
+
Notice that any feature deprecated by a minor release can be expected to be removed by the next major release.
|
82
|
+
|
83
|
+
## Changelog
|
84
|
+
|
85
|
+
Full list of changes in [CHANGELOG.md](CHANGELOG.md)
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/moku-io/rails-options.
|
90
|
+
|
91
|
+
## License
|
92
|
+
|
93
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require_relative 'yaml/env_visitor'
|
2
|
+
require_relative 'key_override_error'
|
3
|
+
|
4
|
+
module Rails
|
5
|
+
class Application
|
6
|
+
def options
|
7
|
+
@options ||= begin
|
8
|
+
yaml_visitor = OptionsConfig::EnvVisitor.create symbolize_names: true
|
9
|
+
Array(config.options.roots)
|
10
|
+
.flat_map do |root|
|
11
|
+
Dir
|
12
|
+
.glob(Array(config.options.paths), base: root)
|
13
|
+
.map do |path|
|
14
|
+
path.match %r{^(?<filename>.*?)(?<env>\..*?)?(?<extension>\.ya?ml)(?<enc>\.enc)?$} do |md|
|
15
|
+
full_path = Pathname(root).join(path)
|
16
|
+
encrypted = md[:enc].present?
|
17
|
+
content = if encrypted
|
18
|
+
yaml_visitor.accept YAML.parse(encrypted(full_path).read)
|
19
|
+
else
|
20
|
+
yaml_visitor.accept YAML.parse_file(full_path)
|
21
|
+
end
|
22
|
+
|
23
|
+
{
|
24
|
+
path: full_path,
|
25
|
+
filename: md[:filename],
|
26
|
+
content: content,
|
27
|
+
encrypted: encrypted
|
28
|
+
}
|
29
|
+
.tap do |hash|
|
30
|
+
hash[:environment] = md[:env].delete_prefix('.').to_sym if md[:env].present?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
.group_by { |file| file[:filename] }
|
36
|
+
.map do |_, files|
|
37
|
+
# Specifics overwrite bases, and encrypted overwrite cleartexts for each.
|
38
|
+
bases = files
|
39
|
+
.reject { |file| file.key? :environment }
|
40
|
+
.sort_by { |file| file[:encrypted] ? 1 : 0 }
|
41
|
+
specifics = files
|
42
|
+
.filter { |file| file[:environment] == Rails.env.to_sym }
|
43
|
+
.sort_by { |file| file[:encrypted] ? 1 : 0 }
|
44
|
+
|
45
|
+
(bases + specifics)
|
46
|
+
.reduce({}) { |acc, override| acc.deep_merge override[:content] }
|
47
|
+
end
|
48
|
+
.then do |hashes|
|
49
|
+
if config.options.raise_on_override
|
50
|
+
hashes.reduce credentials.config do |acc, hash|
|
51
|
+
acc.deep_merge hash do |key, value1, value2|
|
52
|
+
raise Options::KeyOverrideError,
|
53
|
+
'Key override while loading options: ' \
|
54
|
+
"trying to set `#{key}' to #{value2.inspect}:#{value2.class}, " \
|
55
|
+
"but it is already set to #{value1.inspect}:#{value1.class}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
else
|
59
|
+
hashes.reduce(credentials.config, &:deep_merge)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
.then do |hash|
|
63
|
+
deep_transform = proc do |value|
|
64
|
+
if value.is_a? Hash
|
65
|
+
ActiveSupport::OrderedOptions[value.transform_values(&deep_transform)]
|
66
|
+
else
|
67
|
+
value
|
68
|
+
end
|
69
|
+
end
|
70
|
+
deep_transform.call hash
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Rails
|
2
|
+
module OptionsConfig
|
3
|
+
class Engine < ::Rails::Engine
|
4
|
+
config.options = ActiveSupport::OrderedOptions.new
|
5
|
+
|
6
|
+
config.options.roots = ['config']
|
7
|
+
config.options.paths = ['options{,.*}.{yml,yaml}{.enc,}', 'options/**/*.{yml,yaml}{.enc,}']
|
8
|
+
config.options.raise_on_override = false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-options_config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.3.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Moku S.r.l.
|
8
|
+
- Riccardo Agatea
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2024-03-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: railties
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '7.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '7.0'
|
28
|
+
description: As a project's size increases, its credentials file tends to become unmanageable.
|
29
|
+
With Rails Options Config you can split the credentials into smaller separate YAML
|
30
|
+
files.
|
31
|
+
email:
|
32
|
+
- info@moku.io
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- lib/rails/options_config.rb
|
40
|
+
- lib/rails/options_config/application.rb
|
41
|
+
- lib/rails/options_config/engine.rb
|
42
|
+
- lib/rails/options_config/key_override_error.rb
|
43
|
+
- lib/rails/options_config/version.rb
|
44
|
+
- lib/rails/options_config/yaml/env_visitor.rb
|
45
|
+
homepage: https://bitbucket.org/moku_team/rails-options
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata:
|
49
|
+
homepage_uri: https://bitbucket.org/moku_team/rails-options
|
50
|
+
source_code_uri: https://bitbucket.org/moku_team/rails-options
|
51
|
+
changelog_uri: https://github.com/moku-io/rails-options/blob/master/CHANGELOG.md
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 2.6.0
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.4.21
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: A uniform interface to multiple option YAML files.
|
71
|
+
test_files: []
|