dry-configurable 0.1.7 → 0.7.0

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.
@@ -0,0 +1,186 @@
1
+ RSpec.describe Dry::Configurable::Config do
2
+ let(:klass) { Dry::Configurable::Config }
3
+ let(:config) { klass.create(settings) }
4
+ let(:settings) do
5
+ [
6
+ value_class.new(:db, 'sqlite', ->(v) { "#{v}:memory" }),
7
+ value_class.new(:user, 'root', ->(v) { v }),
8
+ value_class.new(:pass, none, ->(v) { v })
9
+ ]
10
+ end
11
+ let(:value_class) { Dry::Configurable::Config::Value }
12
+ let(:none) { value_class::NONE }
13
+
14
+ describe '.create' do
15
+ it 'creates a config subclass from the given settings' do
16
+ expect(config.class).to be < klass
17
+
18
+ expect(config.db).to eq('sqlite:memory')
19
+ expect(config.user).to eq('root')
20
+ expect(config.pass).to be(nil)
21
+
22
+ expect { config.db = 'ineedm0ar' }.to change(config, :db)
23
+ .from('sqlite:memory')
24
+ .to('ineedm0ar:memory')
25
+ expect { config.user = 'whoami' }.to change(config, :user)
26
+ .from('root')
27
+ .to('whoami')
28
+ expect { config.pass = 'h4xz0rz' }.to change(config, :pass)
29
+ .from(nil)
30
+ .to('h4xz0rz')
31
+ end
32
+ end
33
+
34
+ describe '#clone' do
35
+ subject!(:clone) { config.clone }
36
+
37
+ it 'clones and returns the config' do
38
+ expect(clone.db).to eq(config.db)
39
+ expect(clone.user).to eq(config.user)
40
+ expect(clone.pass).to eq(config.pass)
41
+ is_expected.to_not be(config)
42
+ end
43
+ end
44
+
45
+ describe '#dup' do
46
+ subject!(:dup) { config.dup }
47
+
48
+ it 'dups and returns the config' do
49
+ expect(dup.db).to eq(config.db)
50
+ expect(dup.user).to eq(config.user)
51
+ expect(dup.pass).to eq(config.pass)
52
+ is_expected.to_not be(config)
53
+ end
54
+ end
55
+
56
+ describe '#finalize!' do
57
+ subject!(:dup) { config.finalize! }
58
+
59
+ it 'freezes itself and the config' do
60
+ expect { config.user = 'whoami' }
61
+ .to raise_error(Dry::Configurable::FrozenConfig, 'Cannot modify frozen config')
62
+ end
63
+ end
64
+
65
+ describe '#to_h' do
66
+ subject! { config.to_h }
67
+
68
+ context 'without nesting' do
69
+ it 'returns a config hash' do
70
+ is_expected.to eq(
71
+ db: 'sqlite:memory',
72
+ user: 'root',
73
+ pass: nil
74
+ )
75
+ end
76
+ end
77
+
78
+ context 'with nesting' do
79
+ let(:nested_setting) do
80
+ ::Dry::Configurable::NestedConfig.new do
81
+ setting(:bar, 'baz') { |v| v }
82
+ end.tap(&:create_config)
83
+ end
84
+ let(:settings) do
85
+ [
86
+ value_class.new(:db, 'sqlite', ->(v) { "#{v}:memory" }),
87
+ value_class.new(:user, 'root', ->(v) { v }),
88
+ value_class.new(:pass, none, ->(v) { v }),
89
+ value_class.new(:foo, nested_setting, ->(v) { v })
90
+ ]
91
+ end
92
+ it 'returns a config hash' do
93
+ is_expected.to eq(
94
+ db: 'sqlite:memory',
95
+ user: 'root',
96
+ pass: nil,
97
+ foo: {
98
+ bar: 'baz'
99
+ }
100
+ )
101
+ end
102
+ end
103
+ end
104
+
105
+ describe '#to_hash' do
106
+ subject! { config.to_hash }
107
+
108
+ context 'without nesting' do
109
+ it 'returns a config hash' do
110
+ is_expected.to eq(
111
+ db: 'sqlite:memory',
112
+ user: 'root',
113
+ pass: nil
114
+ )
115
+ end
116
+ end
117
+
118
+ context 'with nesting' do
119
+ let(:nested_setting) do
120
+ klass.create([value_class.new(:bar, 'baz', ->(v) { v })])
121
+ end
122
+ let(:settings) do
123
+ [
124
+ value_class.new(:db, 'sqlite', ->(v) { "#{v}:memory" }),
125
+ value_class.new(:user, 'root', ->(v) { v }),
126
+ value_class.new(:pass, none, ->(v) { v }),
127
+ value_class.new(:foo, nested_setting, ->(v) { v })
128
+ ]
129
+ end
130
+ it 'returns a config hash' do
131
+ is_expected.to eq(
132
+ db: 'sqlite:memory',
133
+ user: 'root',
134
+ pass: nil,
135
+ foo: {
136
+ bar: 'baz'
137
+ }
138
+ )
139
+ end
140
+ end
141
+ end
142
+
143
+ describe '#[]' do
144
+ it 'returns given setting' do
145
+ expect(config[:db]).to eq('sqlite:memory')
146
+ expect(config[:user]).to eq('root')
147
+ expect(config[:pass]).to be(nil)
148
+ end
149
+
150
+ it 'raises an ArgumentError when setting does not exist' do
151
+ expect { config[:unknown] }.to raise_error(
152
+ ArgumentError, '+unknown+ is not a setting name'
153
+ )
154
+ end
155
+
156
+ it 'accepts setting name as a string' do
157
+ expect(config['user']).to eq('root')
158
+ end
159
+ end
160
+
161
+ describe '#[]=' do
162
+ it 'sets given setting' do
163
+ expect { config[:db] = 'ineedm0ar' }.to change(config, :db)
164
+ .from('sqlite:memory')
165
+ .to('ineedm0ar:memory')
166
+ expect { config[:user] = 'whoami' }.to change(config, :user)
167
+ .from('root')
168
+ .to('whoami')
169
+ expect { config[:pass] = 'h4xz0rz' }.to change(config, :pass)
170
+ .from(nil)
171
+ .to('h4xz0rz')
172
+ end
173
+
174
+ it 'raises an ArgumentError when setting does not exist' do
175
+ expect { config[:unknown] = 'unknown' }.to raise_error(
176
+ ArgumentError, '+unknown+ is not a setting name'
177
+ )
178
+ end
179
+
180
+ it 'accepts setting name as a string' do
181
+ expect { config['user'] = 'whoami' }.to change(config, :user)
182
+ .from('root')
183
+ .to('whoami')
184
+ end
185
+ end
186
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-configurable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Holland
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-20 00:00:00.000000000 Z
11
+ date: 2017-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -76,10 +76,9 @@ files:
76
76
  - .codeclimate.yml
77
77
  - .gitignore
78
78
  - .rspec
79
- - .rubocop.yml
80
- - .rubocop_todo.yml
81
79
  - .ruby-version
82
80
  - .travis.yml
81
+ - CHANGELOG.md
83
82
  - Gemfile
84
83
  - LICENSE
85
84
  - README.md
@@ -87,12 +86,20 @@ files:
87
86
  - dry-configurable.gemspec
88
87
  - lib/dry-configurable.rb
89
88
  - lib/dry/configurable.rb
89
+ - lib/dry/configurable/argument_parser.rb
90
90
  - lib/dry/configurable/config.rb
91
+ - lib/dry/configurable/config/value.rb
92
+ - lib/dry/configurable/error.rb
93
+ - lib/dry/configurable/nested_config.rb
94
+ - lib/dry/configurable/test_interface.rb
91
95
  - lib/dry/configurable/version.rb
92
96
  - rakelib/rubocop.rake
93
97
  - spec/integration/configurable_spec.rb
94
98
  - spec/spec_helper.rb
95
99
  - spec/support/shared_examples/configurable.rb
100
+ - spec/unit/dry/configurable/argument_parser_spec.rb
101
+ - spec/unit/dry/configurable/config/value_spec.rb
102
+ - spec/unit/dry/configurable/config_spec.rb
96
103
  homepage: https://github.com/dryrb/dry-configurable
97
104
  licenses:
98
105
  - MIT
@@ -121,3 +128,6 @@ test_files:
121
128
  - spec/integration/configurable_spec.rb
122
129
  - spec/spec_helper.rb
123
130
  - spec/support/shared_examples/configurable.rb
131
+ - spec/unit/dry/configurable/argument_parser_spec.rb
132
+ - spec/unit/dry/configurable/config/value_spec.rb
133
+ - spec/unit/dry/configurable/config_spec.rb
data/.rubocop.yml DELETED
@@ -1,16 +0,0 @@
1
- # Generated by `rubocop --auto-gen-config`
2
- inherit_from: .rubocop_todo.yml
3
-
4
- Metrics/LineLength:
5
- Max: 120
6
-
7
- Style/Documentation:
8
- Enabled: false
9
-
10
- Lint/HandleExceptions:
11
- Exclude:
12
- - rakelib/*.rake
13
-
14
- Style/FileName:
15
- Exclude:
16
- - lib/dry-configurable.rb
data/.rubocop_todo.yml DELETED
@@ -1,6 +0,0 @@
1
- # This configuration was generated by `rubocop --auto-gen-config`
2
- # on 2015-06-10 18:54:14 +0100 using RuboCop version 0.26.0.
3
- # The point is for the user to remove these configuration records
4
- # one by one as the offenses are removed from the code base.
5
- # Note that changes in the inspected code, or installation of new
6
- # versions of RuboCop, may require this file to be generated again.