has_configuration 2.0.0 → 5.0.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.
- checksums.yaml +5 -5
- data/lib/has_configuration.rb +2 -0
- data/lib/has_configuration/configuration.rb +29 -24
- data/lib/version.rb +4 -2
- data/spec/lib/has_configuration/configuration_spec.rb +38 -34
- data/spec/lib/has_configuration_spec.rb +13 -13
- data/spec/spec_helper.rb +2 -0
- data/spec/support/rails_mock.rb +2 -0
- metadata +39 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9ef13678ebcf0232c7c0b7535487a70b25d0e245686ca4acb7a8683cbee59d4a
|
4
|
+
data.tar.gz: '04568b62a1c1259c4e14dca63438e3ac60032f1ca78e103a0c09f0d8d816b14f'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad96fe37a596f768234729a99597a4429d1893b4765ba6196e3f904f038e021b4a5395927ae58a5f0d4a102568d0cbe663b4d85bd0d675563881e1bdb6b1cac6
|
7
|
+
data.tar.gz: 12f7267b2de11e3c0fe91b77e222281cf6dafc010f6d817358cd78ac936f5457612535a995b2bffde62efd65923bc105f4d7e33ad2a96d3347ee2e59d7a0710a
|
data/lib/has_configuration.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support/core_ext/hash/indifferent_access'
|
2
4
|
require 'ostruct'
|
3
5
|
require 'yaml'
|
@@ -31,12 +33,16 @@ module HasConfiguration #:nodoc:
|
|
31
33
|
end
|
32
34
|
|
33
35
|
def load_file
|
34
|
-
@raw =
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
@raw = if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.0')
|
37
|
+
YAML.safe_load(ERB.new(raw_file(filename)).result, aliases: true)
|
38
|
+
else
|
39
|
+
YAML.safe_load(
|
40
|
+
ERB.new(raw_file(filename)).result,
|
41
|
+
[], # whitelist_classes
|
42
|
+
[], # whitelist_symbols
|
43
|
+
true # allow aliases
|
44
|
+
)
|
45
|
+
end
|
40
46
|
end
|
41
47
|
|
42
48
|
def init_hash
|
@@ -53,25 +59,28 @@ module HasConfiguration #:nodoc:
|
|
53
59
|
end
|
54
60
|
|
55
61
|
def filename
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
62
|
+
@options[:file] || determine_filename_from_class ||
|
63
|
+
raise(
|
64
|
+
ArgumentError,
|
65
|
+
'Unable to resolve filename, please add :file parameter to has_configuration'
|
66
|
+
)
|
67
|
+
end
|
68
|
+
|
69
|
+
def determine_filename_from_class
|
70
|
+
return unless @class_name
|
71
|
+
|
72
|
+
filename = "#{@class_name.downcase}.yml"
|
73
|
+
defined?(Rails) ? Rails.root.join('config', filename).to_s : filename
|
65
74
|
end
|
66
75
|
|
67
76
|
def environment
|
68
|
-
return @options[:env] if @options.
|
77
|
+
return @options[:env] if @options.key?(:env)
|
69
78
|
return Rails.env.to_s if defined?(Rails)
|
70
79
|
end
|
71
80
|
|
72
81
|
def deep_structify(hash)
|
73
82
|
hash ||= {}
|
74
|
-
result =
|
83
|
+
result = hash.transform_values { |v| v.is_a?(Hash) ? deep_structify(v) : v }
|
75
84
|
OpenStruct.new(result)
|
76
85
|
end
|
77
86
|
|
@@ -85,15 +94,11 @@ module HasConfiguration #:nodoc:
|
|
85
94
|
@deep_stringified_hash ||= deep_transform_keys(@hash, &:to_s)
|
86
95
|
end
|
87
96
|
|
88
|
-
# from Rails
|
97
|
+
# from Rails (/active_support/core_ext/hash/keys.rb)
|
89
98
|
def deep_transform_keys(hash, &block)
|
90
|
-
|
91
|
-
|
92
|
-
hash.each do |key, value|
|
93
|
-
result[yield(key)] = value.is_a?(Hash) ? deep_transform_keys(value, &block) : value
|
94
|
-
end
|
99
|
+
hash&.each_with_object({}) do |(key, value), result|
|
100
|
+
result[yield(key)] = value.is_a?(Hash) ? deep_transform_keys(value, &block) : value
|
95
101
|
end
|
96
|
-
result
|
97
102
|
end
|
98
103
|
end
|
99
104
|
end
|
data/lib/version.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'has_configuration'
|
2
4
|
|
3
5
|
require 'support/rails_mock'
|
@@ -6,7 +8,7 @@ RSpec.describe HasConfiguration::Configuration do
|
|
6
8
|
let(:klass) { Class }
|
7
9
|
|
8
10
|
before do
|
9
|
-
allow_any_instance_of(
|
11
|
+
allow_any_instance_of( # rubocop:disable RSpec/AnyInstance
|
10
12
|
Configuration
|
11
13
|
).to receive(:raw_file).and_return(File.read(fixture))
|
12
14
|
end
|
@@ -18,8 +20,8 @@ RSpec.describe HasConfiguration::Configuration do
|
|
18
20
|
let(:file) { '/RAILS_ROOT/config/class.yml' }
|
19
21
|
|
20
22
|
it 'loads default file' do
|
21
|
-
|
22
|
-
|
23
|
+
configuration = described_class.new(klass)
|
24
|
+
expect(configuration).to have_received(:raw_file).with(file)
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
@@ -27,8 +29,8 @@ RSpec.describe HasConfiguration::Configuration do
|
|
27
29
|
let(:file) { 'foo/bar.yml' }
|
28
30
|
|
29
31
|
it 'loads provided file' do
|
30
|
-
|
31
|
-
|
32
|
+
configuration = described_class.new(klass, file: file)
|
33
|
+
expect(configuration).to have_received(:raw_file).with(file)
|
32
34
|
end
|
33
35
|
end
|
34
36
|
end
|
@@ -36,32 +38,31 @@ RSpec.describe HasConfiguration::Configuration do
|
|
36
38
|
context 'when initialized' do
|
37
39
|
let(:environment) { nil }
|
38
40
|
|
39
|
-
context '
|
40
|
-
|
41
|
+
context 'without env option' do
|
42
|
+
subject(:hash) { described_class.new(klass).to_h }
|
41
43
|
|
42
|
-
|
43
|
-
subject(:hash) { HasConfiguration::Configuration.new(klass).to_h }
|
44
|
+
let(:fixture) { 'spec/fixtures/class.yml' }
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
end
|
46
|
+
it 'return the expected hash' do
|
47
|
+
expect(hash).to eq('env' => 'test')
|
48
48
|
end
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
51
|
+
context 'with env option' do
|
52
|
+
subject(:hash) { described_class.new(klass, env: environment).to_h }
|
52
53
|
|
53
|
-
|
54
|
+
let(:environment) { 'production' }
|
55
|
+
let(:fixture) { 'spec/fixtures/class.yml' }
|
54
56
|
|
55
|
-
|
56
|
-
|
57
|
-
end
|
57
|
+
it 'return the expected hash' do
|
58
|
+
expect(hash).to eq('env' => environment)
|
58
59
|
end
|
59
60
|
end
|
60
61
|
|
61
|
-
context 'yaml defaults' do
|
62
|
-
|
62
|
+
context 'with yaml defaults' do
|
63
|
+
subject(:hash) { described_class.new(klass).to_h }
|
63
64
|
|
64
|
-
|
65
|
+
let(:fixture) { 'spec/fixtures/with_defaults.yml' }
|
65
66
|
|
66
67
|
it 'return the expected hash' do
|
67
68
|
expect(hash).to eq('default' => 'default', 'env' => 'test')
|
@@ -69,9 +70,9 @@ RSpec.describe HasConfiguration::Configuration do
|
|
69
70
|
end
|
70
71
|
|
71
72
|
context 'with erb' do
|
72
|
-
|
73
|
+
subject(:hash) { described_class.new(klass).to_h }
|
73
74
|
|
74
|
-
|
75
|
+
let(:fixture) { 'spec/fixtures/with_erb.yml' }
|
75
76
|
|
76
77
|
it 'return the expected hash' do
|
77
78
|
expect(hash).to eq('erb' => Rails.env)
|
@@ -82,27 +83,30 @@ RSpec.describe HasConfiguration::Configuration do
|
|
82
83
|
describe '#to_h' do
|
83
84
|
let(:fixture) { 'spec/fixtures/with_nested_attributes.yml' }
|
84
85
|
|
85
|
-
context '
|
86
|
-
subject {
|
87
|
-
|
86
|
+
context 'without arguments' do
|
87
|
+
subject { described_class.new(klass).to_h }
|
88
|
+
|
89
|
+
it { is_expected.to be_a(HashWithIndifferentAccess) }
|
88
90
|
end
|
89
91
|
|
90
|
-
context '
|
91
|
-
subject {
|
92
|
-
|
92
|
+
context 'with :stringify' do
|
93
|
+
subject { described_class.new(klass).to_h(:stringify) }
|
94
|
+
|
95
|
+
it { is_expected.to eq('env' => 'test', 'nested' => { 'foo' => 'bar', 'baz' => true }) }
|
93
96
|
end
|
94
97
|
|
95
|
-
context '
|
96
|
-
subject {
|
97
|
-
|
98
|
+
context 'with :symbolized' do
|
99
|
+
subject { described_class.new(klass).to_h(:symbolized) }
|
100
|
+
|
101
|
+
it { is_expected.to eq(env: 'test', nested: { foo: 'bar', baz: true }) }
|
98
102
|
end
|
99
103
|
end
|
100
104
|
|
101
105
|
describe 'struct methods' do
|
102
|
-
let(:configuration) {
|
106
|
+
let(:configuration) { described_class.new(klass) }
|
103
107
|
let(:fixture) { 'spec/fixtures/with_nested_attributes.yml' }
|
104
108
|
|
105
|
-
it '
|
109
|
+
it 'supports multiple getter variants' do # rubocop:disable RSpec/MultipleExpectations
|
106
110
|
expect(configuration.to_h[:env]).to eql('test')
|
107
111
|
expect(configuration.env).to eql('test')
|
108
112
|
|
@@ -1,29 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Dummy
|
4
|
+
require 'has_configuration'
|
5
|
+
has_configuration file: 'spec/fixtures/class.yml'
|
6
|
+
end
|
7
|
+
|
1
8
|
RSpec.describe HasConfiguration do
|
2
9
|
context 'when declared' do
|
3
|
-
|
4
|
-
Dummy = Class.new do
|
5
|
-
require 'has_configuration'
|
6
|
-
has_configuration file: 'spec/fixtures/class.yml'
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
context 'the class' do
|
10
|
+
context 'with a class' do
|
11
11
|
subject(:dummy) { Dummy }
|
12
12
|
|
13
|
-
it {
|
13
|
+
it { is_expected.to respond_to(:configuration) }
|
14
14
|
|
15
15
|
it 'returns a configuration' do
|
16
|
-
expect(dummy.configuration).to
|
16
|
+
expect(dummy.configuration).to be_present
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
context 'an instance' do
|
20
|
+
context 'with an instance' do
|
21
21
|
subject(:dummy) { Dummy.new }
|
22
22
|
|
23
|
-
it {
|
23
|
+
it { is_expected.to respond_to(:configuration) }
|
24
24
|
|
25
25
|
it 'returns a configuration' do
|
26
|
-
expect(dummy.configuration).to
|
26
|
+
expect(dummy.configuration).to be_present
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/support/rails_mock.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_configuration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 5.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Spickermann
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,14 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 4.2.2
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 4.2.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: coveralls
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: rake
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -53,7 +67,7 @@ dependencies:
|
|
53
67
|
- !ruby/object:Gem::Version
|
54
68
|
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
70
|
+
name: rubocop
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
73
|
- - ">="
|
@@ -67,7 +81,21 @@ dependencies:
|
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
|
-
name: rubocop
|
84
|
+
name: rubocop-performance
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rspec
|
71
99
|
requirement: !ruby/object:Gem::Requirement
|
72
100
|
requirements:
|
73
101
|
- - ">="
|
@@ -105,7 +133,7 @@ homepage: https://github.com/spickermann/has_configuration
|
|
105
133
|
licenses:
|
106
134
|
- MIT
|
107
135
|
metadata: {}
|
108
|
-
post_install_message:
|
136
|
+
post_install_message:
|
109
137
|
rdoc_options: []
|
110
138
|
require_paths:
|
111
139
|
- lib
|
@@ -113,16 +141,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
113
141
|
requirements:
|
114
142
|
- - ">="
|
115
143
|
- !ruby/object:Gem::Version
|
116
|
-
version: 2.
|
144
|
+
version: 2.5.0
|
117
145
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
146
|
requirements:
|
119
147
|
- - ">="
|
120
148
|
- !ruby/object:Gem::Version
|
121
149
|
version: '0'
|
122
150
|
requirements: []
|
123
|
-
|
124
|
-
|
125
|
-
signing_key:
|
151
|
+
rubygems_version: 3.2.3
|
152
|
+
signing_key:
|
126
153
|
specification_version: 4
|
127
154
|
summary: Simple configuration handling
|
128
155
|
test_files: []
|