anyway_config 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -1
- data/.travis.yml +1 -1
- data/CHANGELOG.md +13 -0
- data/README.md +21 -1
- data/Rakefile +1 -1
- data/anyway_config.gemspec +2 -2
- data/lib/anyway/config.rb +24 -12
- data/lib/anyway/env.rb +9 -23
- data/lib/anyway/ext/hash.rb +1 -1
- data/lib/anyway/version.rb +1 -1
- data/spec/config_spec.rb +26 -6
- data/spec/env_spec.rb +1 -1
- data/spec/spec_helper.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cdd74aa415435157e394ef1433722c7a579ff015e72923b095f9f7b25d02990e
|
4
|
+
data.tar.gz: d10f5c7bfc2a8f7908ae1b9a43efcc16da7682974aef1519772af482d75c6cd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6ac9bd78a2dbd84ae21124a3ad21556204fd6ebfe05dbb55276ac8ef8a94f5ae09ad070e51cf8e97fc3aca8e8f8126171b53e58ff4c81b012abdbbcc28a2779
|
7
|
+
data.tar.gz: 7da007e994e49579dba02fedf82814367c469be4872bba8aafec1b19f57eb3bd6b46bee03e5e98e60e14bbb28e26b3ba76bf16aa40006ae36438ed0dcae409eb
|
data/.rubocop.yml
CHANGED
@@ -13,7 +13,7 @@ AllCops:
|
|
13
13
|
- 'vendor/**/*'
|
14
14
|
DisplayCopNames: true
|
15
15
|
StyleGuideCopsOnly: false
|
16
|
-
TargetRubyVersion: 2.
|
16
|
+
TargetRubyVersion: 2.3
|
17
17
|
|
18
18
|
Style/Documentation:
|
19
19
|
Exclude:
|
@@ -42,3 +42,6 @@ Bundler/OrderedGems:
|
|
42
42
|
|
43
43
|
Style/MutableConstant:
|
44
44
|
Enabled: false
|
45
|
+
|
46
|
+
Naming/MemoizedInstanceVariableName:
|
47
|
+
Enabled: false
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## master
|
4
|
+
|
5
|
+
## 1.3.0 (2018-06-15)
|
6
|
+
|
7
|
+
- Ruby 2.2 is no longer supported.
|
8
|
+
|
9
|
+
- `Anyway::Config.env_prefix` method is introduced. ([@charlie-wasp][])
|
10
|
+
|
11
|
+
## 1.2.0 (2018-02-19)
|
12
|
+
|
13
|
+
Now works on JRuby 9.1+.
|
14
|
+
|
3
15
|
## 1.1.3 (2017-12-20)
|
4
16
|
|
5
17
|
- Allow to pass raw hash with explicit values to `Config.new`. ([@dsalahutdinov][])
|
@@ -54,3 +66,4 @@ Initial version.
|
|
54
66
|
[@palkan]: https://github.com/palkan
|
55
67
|
[@onemanstartup]: https://github.com/onemanstartup
|
56
68
|
[@dsalahutdinov]: https://github.com/dsalahutdinov
|
69
|
+
[@charlie-wasp]: https://github.com/charlie-wasp
|
data/README.md
CHANGED
@@ -89,6 +89,26 @@ module MyCoolGem
|
|
89
89
|
end
|
90
90
|
```
|
91
91
|
|
92
|
+
#### Customize env variable names prefix
|
93
|
+
|
94
|
+
By default, Anyway Config will use config name with stripped underscores as a prefix for env variable names (e.g.
|
95
|
+
`config_name :my_app` will result to parsing `MYAPP_HOST` variable, not `MY_APP_HOST`). You can set env prefix
|
96
|
+
explicitly, and it will be used as is:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
module MyCoolGem
|
100
|
+
class Config < Anyway::Config
|
101
|
+
config_name :cool_gem
|
102
|
+
env_prefix :really_cool # now variables, starting wih `REALLY_COOL_`, will be parsed
|
103
|
+
attr_config user: 'root', password: 'root', host: 'localhost', options: {}
|
104
|
+
end
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
**DEPRECATION WARNING** In the 1.4 version no stripping will be applied on config_names by default, so if you use explicit config names with
|
109
|
+
underscores and use env variables, your app will be broken. In this case it is recommended to start using `env_prefix`
|
110
|
+
now.
|
111
|
+
|
92
112
|
#### Provide explicit values
|
93
113
|
|
94
114
|
Sometimes it's useful to set some parameters explicitly during config initialization.
|
@@ -176,7 +196,7 @@ Examples:
|
|
176
196
|
- `"123"` to 123 and `"3.14"` to 3.14.
|
177
197
|
|
178
198
|
*Anyway Config* supports nested (_hashed_) env variables. Just separate keys with double-underscore.
|
179
|
-
For example, "MYCOOLGEM_OPTIONS__VERBOSE" is parsed as `config.options
|
199
|
+
For example, "MYCOOLGEM_OPTIONS__VERBOSE" is parsed as `config.options["verbose"]`.
|
180
200
|
|
181
201
|
Array values are also supported:
|
182
202
|
|
data/Rakefile
CHANGED
data/anyway_config.gemspec
CHANGED
@@ -20,9 +20,9 @@ Gem::Specification.new do |s|
|
|
20
20
|
|
21
21
|
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
22
22
|
s.require_paths = ["lib"]
|
23
|
-
s.required_ruby_version = '>= 2.
|
23
|
+
s.required_ruby_version = '>= 2.3'
|
24
24
|
|
25
25
|
s.add_development_dependency "rspec", "~> 3.5"
|
26
|
-
s.add_development_dependency "rubocop", "~> 0.
|
26
|
+
s.add_development_dependency "rubocop", "~> 0.57.2"
|
27
27
|
s.add_development_dependency "simplecov", ">= 0.3.8"
|
28
28
|
end
|
data/lib/anyway/config.rb
CHANGED
@@ -35,6 +35,11 @@ module Anyway # :nodoc:
|
|
35
35
|
@config_name
|
36
36
|
end
|
37
37
|
|
38
|
+
def env_prefix(val = nil)
|
39
|
+
return (@env_prefix = val.to_s) unless val.nil?
|
40
|
+
@env_prefix
|
41
|
+
end
|
42
|
+
|
38
43
|
# Load config as Hash by any name
|
39
44
|
#
|
40
45
|
# Example:
|
@@ -56,7 +61,7 @@ module Anyway # :nodoc:
|
|
56
61
|
end
|
57
62
|
end
|
58
63
|
|
59
|
-
attr_reader :config_name
|
64
|
+
attr_reader :config_name, :env_prefix
|
60
65
|
|
61
66
|
# Instantiate config with specified name, loads the data and applies overrides
|
62
67
|
#
|
@@ -64,19 +69,26 @@ module Anyway # :nodoc:
|
|
64
69
|
#
|
65
70
|
# my_config = Anyway::Config.new(name: :my_app, load: true, overrides: { some: :value })
|
66
71
|
#
|
67
|
-
# rubocop:disable Metrics/
|
68
|
-
def initialize(
|
69
|
-
unless config_name.nil? && do_load.nil?
|
70
|
-
warn "[Deprecated] Positional arguments for Anyway::Config#initialize will be removed in 1.2.0. Use keyword arguments instead: initialize(name:, load:, overrides:)"
|
71
|
-
end
|
72
|
-
name = config_name unless config_name.nil?
|
73
|
-
load = do_load unless do_load.nil?
|
74
|
-
|
72
|
+
# rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/LineLength,Metrics/CyclomaticComplexity
|
73
|
+
def initialize(name: nil, load: true, overrides: {})
|
75
74
|
@config_name = name || self.class.config_name
|
75
|
+
|
76
76
|
raise ArgumentError, "Config name is missing" unless @config_name
|
77
|
+
|
78
|
+
if @config_name.to_s.include?('_') && @env_prefix.nil?
|
79
|
+
warn "[Deprecated] As your config_name is #{@config_name}, " \
|
80
|
+
"the prefix `#{@config_name.to_s.delete('_').upcase}` " \
|
81
|
+
"will be used to parse env variables. " \
|
82
|
+
"This behavior is about to change in 1.4.0 (no more deleting underscores). " \
|
83
|
+
"Env prefix can be set explicitly with `env_prefix` method now already " \
|
84
|
+
"(check out the docs), and it will be used as is."
|
85
|
+
end
|
86
|
+
|
87
|
+
@env_prefix = self.class.env_prefix || @config_name.to_s&.delete('_')
|
88
|
+
|
77
89
|
self.load(overrides) if load
|
78
90
|
end
|
79
|
-
# rubocop:enable Metrics/
|
91
|
+
# rubocop:enable Metrics/MethodLength,Metrics/AbcSize,Metrics/LineLength,Metrics/CyclomaticComplexity
|
80
92
|
|
81
93
|
def reload(overrides = {})
|
82
94
|
clear
|
@@ -108,14 +120,14 @@ module Anyway # :nodoc:
|
|
108
120
|
end
|
109
121
|
|
110
122
|
def load_from_file(config)
|
111
|
-
config_path = Anyway.env.fetch(
|
123
|
+
config_path = Anyway.env.fetch(env_prefix).delete('conf') ||
|
112
124
|
"./config/#{config_name}.yml"
|
113
125
|
config.deep_merge!(parse_yml(config_path) || {}) if config_path && File.file?(config_path)
|
114
126
|
config
|
115
127
|
end
|
116
128
|
|
117
129
|
def load_from_env(config)
|
118
|
-
config.deep_merge!(Anyway.env.fetch(
|
130
|
+
config.deep_merge!(Anyway.env.fetch(env_prefix))
|
119
131
|
config
|
120
132
|
end
|
121
133
|
|
data/lib/anyway/env.rb
CHANGED
@@ -21,34 +21,20 @@ module Anyway
|
|
21
21
|
@data.clear
|
22
22
|
end
|
23
23
|
|
24
|
-
def fetch(
|
25
|
-
@data[
|
26
|
-
@data[
|
24
|
+
def fetch(prefix)
|
25
|
+
@data[prefix] ||= parse_env(prefix.to_s.upcase)
|
26
|
+
@data[prefix].deep_dup
|
27
27
|
end
|
28
28
|
|
29
29
|
private
|
30
30
|
|
31
|
-
def parse_env(
|
32
|
-
|
33
|
-
|
34
|
-
data = {}
|
35
|
-
ENV.each_pair do |key, val|
|
36
|
-
if key.start_with?(config_env_name)
|
37
|
-
_mod, path = extract_module_path(key)
|
38
|
-
set_by_path(data, path, serialize_val(val))
|
39
|
-
end
|
40
|
-
end
|
41
|
-
data
|
42
|
-
end
|
31
|
+
def parse_env(prefix)
|
32
|
+
ENV.each_pair.with_object({}) do |(key, val), data|
|
33
|
+
next unless key.start_with?(prefix)
|
43
34
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
def extract_module_path(key)
|
49
|
-
_, mod, path = key.split(/^([^\_]+)/)
|
50
|
-
path.sub!(/^[\_]+/, '')
|
51
|
-
[mod.downcase, path.downcase]
|
35
|
+
path = key.sub(/^#{prefix}_/, '').downcase
|
36
|
+
set_by_path(data, path, serialize_val(val))
|
37
|
+
end
|
52
38
|
end
|
53
39
|
|
54
40
|
def set_by_path(to, path, val)
|
data/lib/anyway/ext/hash.rb
CHANGED
data/lib/anyway/version.rb
CHANGED
data/spec/config_spec.rb
CHANGED
@@ -72,12 +72,32 @@ describe Anyway::Config do
|
|
72
72
|
end
|
73
73
|
|
74
74
|
describe "load from env" do
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
75
|
+
context "when env_prefix is not specified" do
|
76
|
+
it "uses config_name as a prefix to load variables" do
|
77
|
+
ENV['COOL_PORT'] = '80'
|
78
|
+
ENV['COOL_USER__NAME'] = 'john'
|
79
|
+
Anyway.env.clear
|
80
|
+
expect(conf.port).to eq 80
|
81
|
+
expect(conf.user[:name]).to eq 'john'
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "when env_prefix is specified" do
|
86
|
+
let(:conf) do
|
87
|
+
klass = CoolConfig.dup
|
88
|
+
klass.class_eval { env_prefix(:cool_env) }
|
89
|
+
klass.new
|
90
|
+
end
|
91
|
+
|
92
|
+
it "uses env_prefix value as a prefix to load variables" do
|
93
|
+
ENV['COOL_PORT'] = '80'
|
94
|
+
ENV['COOL_ENV_PORT'] = '8888'
|
95
|
+
ENV['COOL_USER__NAME'] = 'john'
|
96
|
+
ENV['COOL_ENV_USER__NAME'] = 'bill'
|
97
|
+
Anyway.env.clear
|
98
|
+
expect(conf.port).to eq 8888
|
99
|
+
expect(conf.user[:name]).to eq 'bill'
|
100
|
+
end
|
81
101
|
end
|
82
102
|
|
83
103
|
it "handle ENV in YML thru ERB" do
|
data/spec/env_spec.rb
CHANGED
@@ -7,7 +7,7 @@ describe Anyway::Env do
|
|
7
7
|
|
8
8
|
it "loads simple key/values by module", :aggregate_failures do
|
9
9
|
ENV['TESTO_KEY'] = 'a'
|
10
|
-
ENV['
|
10
|
+
ENV['MY_TEST_KEY'] = 'b'
|
11
11
|
expect(env.fetch('testo')['key']).to eq 'a'
|
12
12
|
expect(env.fetch('my_test')['key']).to eq 'b'
|
13
13
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anyway_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Dementyev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 0.57.2
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 0.57.2
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: simplecov
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -117,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
117
117
|
requirements:
|
118
118
|
- - ">="
|
119
119
|
- !ruby/object:Gem::Version
|
120
|
-
version: '2.
|
120
|
+
version: '2.3'
|
121
121
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
122
|
requirements:
|
123
123
|
- - ">="
|
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
125
|
version: '0'
|
126
126
|
requirements: []
|
127
127
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.7.
|
128
|
+
rubygems_version: 2.7.6
|
129
129
|
signing_key:
|
130
130
|
specification_version: 4
|
131
131
|
summary: Configuration DSL for Ruby libraries and applications
|