has_configuration 2.0.0 → 5.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4e711006bd79831f88942f6a8f00c73adda21e06
4
- data.tar.gz: cae116f13df37c98c094537f77528acb37f0c5bb
2
+ SHA256:
3
+ metadata.gz: 9ef13678ebcf0232c7c0b7535487a70b25d0e245686ca4acb7a8683cbee59d4a
4
+ data.tar.gz: '04568b62a1c1259c4e14dca63438e3ac60032f1ca78e103a0c09f0d8d816b14f'
5
5
  SHA512:
6
- metadata.gz: f431a7d22c52c614fad01600be733548ec56bc5fdb21c7dcf6ec43ef6a9f89799d53936287529cab904666defff2abc6ab75ccdf94272087a206246a7287b75f
7
- data.tar.gz: aba4ca638e262bf0c625e56ad9f49386d8c269de18e77d94b8717147c00633a2e236e093e98820d39caf0b0d6707993acc2dc863dc2f3fb1a5e7d6af3641ca50
6
+ metadata.gz: ad96fe37a596f768234729a99597a4429d1893b4765ba6196e3f904f038e021b4a5395927ae58a5f0d4a102568d0cbe663b4d85bd0d675563881e1bdb6b1cac6
7
+ data.tar.gz: 12f7267b2de11e3c0fe91b77e222281cf6dafc010f6d817358cd78ac936f5457612535a995b2bffde62efd65923bc105f4d7e33ad2a96d3347ee2e59d7a0710a
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'has_configuration/configuration'
2
4
 
3
5
  module HasConfiguration #:nodoc:
@@ -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 = YAML.safe_load(
35
- ERB.new(raw_file(filename)).result,
36
- [], # whitelist_classes
37
- [], # whitelist_symbols
38
- true # allow aliases
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
- if @options[:file]
57
- @options[:file]
58
- elsif @class_name
59
- filename = "#{@class_name.downcase}.yml"
60
- defined?(Rails) ? Rails.root.join('config', filename).to_s : filename
61
- else
62
- raise ArgumentError,
63
- 'Unable to resolve filename, please add :file parameter to has_configuration'
64
- end
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.keys.include?(:env)
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 = Hash[hash.map { |k, v| [k, v.is_a?(Hash) ? deep_structify(v) : v] }]
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 4.0 (/active_support/core_ext/hash/keys.rb)
97
+ # from Rails (/active_support/core_ext/hash/keys.rb)
89
98
  def deep_transform_keys(hash, &block)
90
- result = {}
91
- if hash
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
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module HasConfiguration #:nodoc:
2
4
  module VERSION #:nodoc:
3
- MAJOR = 2
5
+ MAJOR = 5
4
6
  MINOR = 0
5
- BUILD = 0
7
+ BUILD = 1
6
8
 
7
9
  STRING = [MAJOR, MINOR, BUILD].join('.')
8
10
  end
@@ -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
- expect_any_instance_of(Configuration).to receive(:raw_file).with(file)
22
- HasConfiguration::Configuration.new(klass)
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
- expect_any_instance_of(Configuration).to receive(:raw_file).with(file)
31
- HasConfiguration::Configuration.new(klass, file: file)
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 'environment' do
40
- let(:fixture) { 'spec/fixtures/class.yml' }
41
+ context 'without env option' do
42
+ subject(:hash) { described_class.new(klass).to_h }
41
43
 
42
- context 'without env option' do
43
- subject(:hash) { HasConfiguration::Configuration.new(klass).to_h }
44
+ let(:fixture) { 'spec/fixtures/class.yml' }
44
45
 
45
- it 'return the expected hash' do
46
- expect(hash).to eq('env' => 'test')
47
- end
46
+ it 'return the expected hash' do
47
+ expect(hash).to eq('env' => 'test')
48
48
  end
49
+ end
49
50
 
50
- context 'with env option' do
51
- let(:environment) { 'production' }
51
+ context 'with env option' do
52
+ subject(:hash) { described_class.new(klass, env: environment).to_h }
52
53
 
53
- subject(:hash) { HasConfiguration::Configuration.new(klass, env: environment).to_h }
54
+ let(:environment) { 'production' }
55
+ let(:fixture) { 'spec/fixtures/class.yml' }
54
56
 
55
- it 'return the expected hash' do
56
- expect(hash).to eq('env' => environment)
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
- let(:fixture) { 'spec/fixtures/with_defaults.yml' }
62
+ context 'with yaml defaults' do
63
+ subject(:hash) { described_class.new(klass).to_h }
63
64
 
64
- subject(:hash) { HasConfiguration::Configuration.new(klass).to_h }
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
- let(:fixture) { 'spec/fixtures/with_erb.yml' }
73
+ subject(:hash) { described_class.new(klass).to_h }
73
74
 
74
- subject(:hash) { HasConfiguration::Configuration.new(klass).to_h }
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 '#to_h' do
86
- subject { HasConfiguration::Configuration.new(klass).to_h }
87
- it { should be_a(HashWithIndifferentAccess) }
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 '#to_h(:stringify)' do
91
- subject { HasConfiguration::Configuration.new(klass).to_h(:stringify) }
92
- it { should eq('env' => 'test', 'nested' => { 'foo' => 'bar', 'baz' => true }) }
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 '#to_h(:symbolized)' do
96
- subject { HasConfiguration::Configuration.new(klass).to_h(:symbolized) }
97
- it { should eq(env: 'test', nested: { foo: 'bar', baz: true }) }
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) { HasConfiguration::Configuration.new(klass) }
106
+ let(:configuration) { described_class.new(klass) }
103
107
  let(:fixture) { 'spec/fixtures/with_nested_attributes.yml' }
104
108
 
105
- it 'is structified' do
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
- before(:all) do
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 { should respond_to(:configuration) }
13
+ it { is_expected.to respond_to(:configuration) }
14
14
 
15
15
  it 'returns a configuration' do
16
- expect(dummy.configuration).to be
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 { should respond_to(:configuration) }
23
+ it { is_expected.to respond_to(:configuration) }
24
24
 
25
25
  it 'returns a configuration' do
26
- expect(dummy.configuration).to be
26
+ expect(dummy.configuration).to be_present
27
27
  end
28
28
  end
29
29
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'coveralls'
2
4
  Coveralls.wear!
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Mocks Rails Environment
2
4
  Rails = Class.new do
3
5
  def self.root
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: 2.0.0
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: 2017-01-16 00:00:00.000000000 Z
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: 3.2.0
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: 3.2.0
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: coveralls
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.1.0
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
- rubyforge_project:
124
- rubygems_version: 2.6.8
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: []