gem_config 0.3.0 → 0.3.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
2
  SHA1:
3
- metadata.gz: 1046dd50da7f348b21c6bfcc3e71903012f82897
4
- data.tar.gz: 44245b10f3f50bd9edde6414fb25f109bbd5378c
3
+ metadata.gz: 25092c71a9f45af628d36f7a87b85778ebce2981
4
+ data.tar.gz: 8f72f574e780277154637e2b3cb0417801c9ab3f
5
5
  SHA512:
6
- metadata.gz: 01e105eeb51018f58561cecaeb4816ba754449488401d58c1544bdbfa145124a4689231014c40fefd66e988b600dd35094cc634bb1c8009d8993801f0ea6c202
7
- data.tar.gz: 6402a8a2746c3319e2742db56c4f8999a032f18242f9f4b7187583471c8ec641f798e42d4f524926257c43955c6ad68132f9e4e6a7227420672832ab8220e832
6
+ metadata.gz: 217c0a4b1fc72bb96ca9e1f4e66e131289a39250c5d93f81028fbc151f90d1cea5ffe032d42546424194ed658c2c8e72c676a215c75bae2af33af270cd219cef
7
+ data.tar.gz: 98e921ae234bd2191c1d18606b1b84772d3eb8132dd83bbde80d720a438abd7ca6eedd7668d396bde28a6b57ca6b1aa2ac461b688924a82f269726e1833f8b97
@@ -0,0 +1,9 @@
1
+ guard 'rspec', cmd: 'bundle exec rspec', failed_mode: :none, all_after_pass: true, all_on_start: true do
2
+ # Specs
3
+ watch(%r(^spec/.+_spec\.rb$))
4
+ watch('spec/spec_helper.rb') { 'spec' }
5
+ watch(%r(^spec/support/(.+)\.rb$)) { 'spec' }
6
+
7
+ # Files
8
+ watch(%r(^lib/(.+)\.rb$)) { |m| "spec/#{m[1]}_spec.rb" }
9
+ end
@@ -20,5 +20,6 @@ Gem::Specification.new do |gem|
20
20
  gem.require_paths = ['lib']
21
21
 
22
22
  gem.add_development_dependency 'rake', '>= 0.9.0'
23
- gem.add_development_dependency 'rspec', '~> 2.13.0'
23
+ gem.add_development_dependency 'rspec', '~> 3.0.0.beta2'
24
+ gem.add_development_dependency 'guard-rspec', '~> 4.2'
24
25
  end
@@ -10,12 +10,16 @@ module GemConfig
10
10
  end
11
11
 
12
12
  def configuration
13
- @configuration ||= Configuration.new
13
+ @configuration ||= Configuration.new(self)
14
14
  end
15
15
 
16
16
  def with_configuration(&block)
17
17
  configuration.rules.instance_eval(&block)
18
18
  end
19
+
20
+ def after_configuration_change(&block)
21
+ @after_configuration_change = block
22
+ end
19
23
  end
20
24
  end
21
25
  end
@@ -1,5 +1,9 @@
1
1
  module GemConfig
2
2
  class Configuration
3
+ def initialize(parent = nil)
4
+ @parent = parent
5
+ end
6
+
3
7
  def rules
4
8
  @rules ||= Rules.new
5
9
  end
@@ -19,6 +23,7 @@ module GemConfig
19
23
  def unset(key)
20
24
  raise InvalidKeyError, "#{key} is not a valid key." unless self.rules.keys.include?(key.to_sym)
21
25
  remove_instance_variable "@#{key}" if instance_variable_defined?("@#{key}")
26
+ call_after_configuration_change
22
27
  end
23
28
 
24
29
  def method_missing(method, *args, &block)
@@ -28,7 +33,7 @@ module GemConfig
28
33
  when (match = method.to_s.match(/\A(?<key>\w+)=\z/)) && self.rules.keys.include?(match[:key].to_sym)
29
34
  set match[:key], args.first
30
35
  else
31
- super method, *args, block
36
+ super
32
37
  end
33
38
  end
34
39
 
@@ -37,6 +42,7 @@ module GemConfig
37
42
  def set(key, value)
38
43
  self.rules.check(key, value)
39
44
  instance_variable_set "@#{key}", value
45
+ call_after_configuration_change
40
46
  end
41
47
 
42
48
  def get(key)
@@ -46,5 +52,12 @@ module GemConfig
46
52
  self.rules[key.to_sym][:default]
47
53
  end
48
54
  end
55
+
56
+ def call_after_configuration_change
57
+ unless @parent.nil?
58
+ after_configuration_change = @parent.instance_variable_get(:@after_configuration_change)
59
+ after_configuration_change.call unless after_configuration_change.nil?
60
+ end
61
+ end
49
62
  end
50
63
  end
@@ -1,3 +1,3 @@
1
1
  module GemConfig
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe GemConfig::Base do
4
+ it 'provides a class method `with_configuration` when included' do
5
+ m = Module.new do
6
+ include GemConfig::Base
7
+ end
8
+
9
+ expect(m).to respond_to(:with_configuration)
10
+ end
11
+
12
+ it 'provides a class method `after_configuration_change` when included' do
13
+ m = Module.new do
14
+ include GemConfig::Base
15
+ end
16
+
17
+ expect(m).to respond_to(:after_configuration_change)
18
+ end
19
+ end
@@ -0,0 +1,168 @@
1
+ require 'spec_helper'
2
+
3
+ AfterConfigurationChangeReceived = Class.new(StandardError)
4
+
5
+ describe GemConfig::Configuration do
6
+ let(:parent) {
7
+ Module.new do
8
+ include GemConfig::Base
9
+ end
10
+ }
11
+ subject do
12
+ GemConfig::Configuration.new(parent).tap do |configuration|
13
+ configuration.rules.has :all_allowed
14
+ configuration.rules.has :foo_bar_allowed, values: %w(foo bar)
15
+ configuration.rules.has :string_allowed, classes: String
16
+ configuration.rules.has :foo, default: 'bar'
17
+ configuration.rules.has :bar
18
+ configuration.rules.has :count
19
+ configuration.rules.has :api_key, default: 'foobarbaz'
20
+
21
+ configuration.foo = 'pelle'
22
+ configuration.count = 123
23
+
24
+ configuration
25
+ end
26
+ end
27
+
28
+ def set_after_configuration_change_received
29
+ parent.class_eval do
30
+ after_configuration_change do
31
+ raise AfterConfigurationChangeReceived
32
+ end
33
+ end
34
+ end
35
+
36
+ describe '#rules' do
37
+ it 'returns a Rules object' do
38
+ subject.rules.is_a?(GemConfig::Rules)
39
+ end
40
+ end
41
+
42
+ describe '#current' do
43
+ it 'returns the current configuration' do
44
+ expect(subject.current).to eq(
45
+ all_allowed: nil,
46
+ foo_bar_allowed: nil,
47
+ string_allowed: nil,
48
+ foo: 'pelle',
49
+ bar: nil,
50
+ count: 123,
51
+ api_key: 'foobarbaz'
52
+ )
53
+ end
54
+ end
55
+
56
+ describe '#reset' do
57
+ it 'resets the configuration' do
58
+ expect { subject.reset }.to change { [subject.foo, subject.count] }.from(['pelle', 123]).to(['bar', nil])
59
+ end
60
+ end
61
+
62
+ describe '#unset' do
63
+ context 'with an existing key' do
64
+ context 'when that key has not been set' do
65
+ it 'doesnt change anything' do
66
+ expect { subject.unset(:bar) }.to_not change { subject.bar }
67
+ end
68
+ end
69
+
70
+ context 'when that key has been set' do
71
+ it 'unsets the key' do
72
+ expect { subject.unset(:count) }.to change { subject.count }.from(123).to(nil)
73
+ end
74
+ end
75
+ end
76
+
77
+ context 'with a non-existing key' do
78
+ it 'raises an exception' do
79
+ expect { subject.unset(:pelle) }.to raise_error(GemConfig::InvalidKeyError)
80
+ end
81
+ end
82
+
83
+ context 'when a after_configuration_change block is present' do
84
+ before do
85
+ set_after_configuration_change_received
86
+ end
87
+
88
+ it 'calls that block after setting a new value' do
89
+ expect { subject.unset :count }.to raise_error(AfterConfigurationChangeReceived)
90
+ end
91
+ end
92
+ end
93
+
94
+ describe 'setting a configuration option' do
95
+ context 'when all values and classes are allowed' do
96
+ it 'sets the configuration option' do
97
+ expect { subject.all_allowed = 'foo' }.to change { subject.all_allowed }.from(nil).to('foo')
98
+ expect { subject.all_allowed = 'bar' }.to change { subject.all_allowed }.from('foo').to('bar')
99
+ end
100
+ end
101
+
102
+ context 'when only certain values are allowed' do
103
+ it 'sets the configuration option to those values' do
104
+ expect { subject.foo_bar_allowed = 'foo' }.to change { subject.foo_bar_allowed }.from(nil).to('foo')
105
+ expect { subject.foo_bar_allowed = 'bar' }.to change { subject.foo_bar_allowed }.from('foo').to('bar')
106
+ end
107
+
108
+ it 'raises an error when for other values' do
109
+ expect { subject.foo_bar_allowed = 'pelle' }.to raise_error(GemConfig::InvalidKeyError)
110
+ end
111
+
112
+ it "doesn't set the configuration option for other values" do
113
+ expect do
114
+ begin
115
+ subject.foo_bar_allowed = 'pelle'
116
+ rescue GemConfig::InvalidKeyError
117
+ end
118
+ end.to_not change { subject.foo_bar_allowed }
119
+ end
120
+ end
121
+
122
+ context 'when only certain classes are allowed' do
123
+ it 'sets the configuration option to those classes' do
124
+ expect { subject.string_allowed = 'foo' }.to change { subject.string_allowed }.from(nil).to('foo')
125
+ expect { subject.string_allowed = 'bar' }.to change { subject.string_allowed }.from('foo').to('bar')
126
+ end
127
+
128
+ it 'raises an error when for other values' do
129
+ expect { subject.string_allowed = 1 }.to raise_error(GemConfig::InvalidKeyError)
130
+ end
131
+
132
+ it "doesn't set the configuration option for other values" do
133
+ expect do
134
+ begin
135
+ subject.string_allowed = 1
136
+ rescue GemConfig::InvalidKeyError
137
+ end
138
+ end.to_not change { subject.string_allowed }
139
+ end
140
+ end
141
+
142
+ context 'when a after_configuration_change block is present' do
143
+ before do
144
+ set_after_configuration_change_received
145
+ end
146
+
147
+ it 'calls that block after setting a new value' do
148
+ expect { subject.all_allowed = 'foo' }.to raise_error(AfterConfigurationChangeReceived)
149
+ end
150
+ end
151
+ end
152
+
153
+ describe 'getting a configuration option' do
154
+ it 'returns the value if it is set' do
155
+ subject.foo = 'bar'
156
+ expect(subject.foo).to eq('bar')
157
+ end
158
+
159
+ it 'also accepts nil as a value' do
160
+ subject.foo = nil
161
+ expect(subject.foo).to be_nil
162
+ end
163
+
164
+ it 'returns the default if no value but a default is set' do
165
+ expect(subject.api_key).to eq('foobarbaz')
166
+ end
167
+ end
168
+ end
@@ -6,27 +6,27 @@ describe GemConfig::Rules do
6
6
  describe '#has' do
7
7
  it 'sets a rule' do
8
8
  subject.has :foo
9
- subject.should have_key(:foo)
9
+ expect(subject).to have_key(:foo)
10
10
  end
11
11
 
12
12
  it 'sets the parameters of a rule' do
13
13
  params = { classes: String, values: %w(foo bar), default: 'foo' }
14
14
  subject.has :foo, params
15
- subject[:foo].should eq(params)
15
+ expect(subject[:foo]).to eq(params)
16
16
  end
17
17
 
18
18
  it 'only allows certain parameters' do
19
19
  expect do
20
20
  subject.has :foo, classes: String
21
- end.to_not raise_error(ArgumentError)
21
+ end.to_not raise_error
22
22
 
23
23
  expect do
24
24
  subject.has :foo, values: 'bar'
25
- end.to_not raise_error(ArgumentError)
25
+ end.to_not raise_error
26
26
 
27
27
  expect do
28
28
  subject.has :foo, default: 'bar'
29
- end.to_not raise_error(ArgumentError)
29
+ end.to_not raise_error
30
30
 
31
31
  expect do
32
32
  subject.has :foo, foo: 'bar'
@@ -44,11 +44,11 @@ describe GemConfig::Rules do
44
44
  it 'only allows a class or an array of classes as the value' do
45
45
  expect do
46
46
  subject.has :foo, classes: String
47
- end.to_not raise_error(ArgumentError)
47
+ end.to_not raise_error
48
48
 
49
49
  expect do
50
50
  subject.has :foo, classes: [String, Numeric]
51
- end.to_not raise_error(ArgumentError)
51
+ end.to_not raise_error
52
52
 
53
53
  expect do
54
54
  subject.has :foo, classes: 'foo'
@@ -61,15 +61,15 @@ describe GemConfig::Rules do
61
61
  it 'only allows values of one of the specified classes' do
62
62
  expect do
63
63
  subject.has :foo, classes: String, values: 'foo'
64
- end.to_not raise_error(ArgumentError)
64
+ end.to_not raise_error
65
65
 
66
66
  expect do
67
67
  subject.has :foo, classes: Numeric, values: 1
68
- end.to_not raise_error(ArgumentError)
68
+ end.to_not raise_error
69
69
 
70
70
  expect do
71
71
  subject.has :foo, classes: [String, Numeric], values: ['foo', 1]
72
- end.to_not raise_error(ArgumentError)
72
+ end.to_not raise_error
73
73
 
74
74
  expect do
75
75
  subject.has :foo, classes: String, values: ['foo', 1]
@@ -83,15 +83,15 @@ describe GemConfig::Rules do
83
83
  it 'only allows a value of one of the specified classes' do
84
84
  expect do
85
85
  subject.has :foo, classes: String, default: 'foo'
86
- end.to_not raise_error(ArgumentError)
86
+ end.to_not raise_error
87
87
 
88
88
  expect do
89
89
  subject.has :foo, classes: Numeric, default: 1
90
- end.to_not raise_error(ArgumentError)
90
+ end.to_not raise_error
91
91
 
92
92
  expect do
93
93
  subject.has :foo, classes: [String, Numeric], default: 'foo'
94
- end.to_not raise_error(ArgumentError)
94
+ end.to_not raise_error
95
95
 
96
96
  expect do
97
97
  subject.has :foo, classes: Numeric, default: 'foo'
@@ -103,11 +103,11 @@ describe GemConfig::Rules do
103
103
  it 'only allows one of the specified values' do
104
104
  expect do
105
105
  subject.has :foo, values: 'foo', default: 'foo'
106
- end.to_not raise_error(ArgumentError)
106
+ end.to_not raise_error
107
107
 
108
108
  expect do
109
109
  subject.has :foo, values: ['foo', 1], default: 1
110
- end.to_not raise_error(ArgumentError)
110
+ end.to_not raise_error
111
111
 
112
112
  expect do
113
113
  subject.has :foo, values: ['foo', 1], default: 'bar'
@@ -143,14 +143,14 @@ describe GemConfig::Rules do
143
143
  subject.has :foo, classes: [String, Numeric]
144
144
  expect do
145
145
  subject.check :foo, 1
146
- end.to_not raise_error(GemConfig::InvalidKeyError)
146
+ end.to_not raise_error
147
147
  end
148
148
 
149
149
  it 'does not raise an error if :values are defined the the value is included in them' do
150
150
  subject.has :foo, values: ['foo', 'bar']
151
151
  expect do
152
152
  subject.check :foo, 'foo'
153
- end.to_not raise_error(GemConfig::InvalidKeyError)
153
+ end.to_not raise_error
154
154
  end
155
155
  end
156
156
  end
@@ -1,13 +1,7 @@
1
1
  require 'gem_config'
2
2
 
3
3
  RSpec.configure do |config|
4
- config.treat_symbols_as_metadata_keys_with_true_values = true
5
4
  config.run_all_when_everything_filtered = true
6
5
  config.filter_run :focus
7
-
8
- # Run specs in random order to surface order dependencies. If you find an
9
- # order dependency and want to debug it, you can fix the order by providing
10
- # the seed, which is printed after each run.
11
- # --seed 1234
12
6
  config.order = 'random'
13
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gem_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Meurer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-14 00:00:00.000000000 Z
11
+ date: 2014-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -30,14 +30,28 @@ dependencies:
30
30
  requirements:
31
31
  - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: 2.13.0
33
+ version: 3.0.0.beta2
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: 2.13.0
40
+ version: 3.0.0.beta2
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
41
55
  description: A nifty way to make your gem configurable.
42
56
  email: manuel@krautcomputing.com
43
57
  executables: []
@@ -46,6 +60,7 @@ extra_rdoc_files: []
46
60
  files:
47
61
  - .travis.yml
48
62
  - Gemfile
63
+ - Guardfile
49
64
  - LICENSE.txt
50
65
  - README.md
51
66
  - Rakefile
@@ -55,10 +70,9 @@ files:
55
70
  - lib/gem_config/configuration.rb
56
71
  - lib/gem_config/rules.rb
57
72
  - lib/gem_config/version.rb
58
- - spec/features/configuration_spec.rb
59
- - spec/lib/base_spec.rb
60
- - spec/lib/configuration_spec.rb
61
- - spec/lib/rules_spec.rb
73
+ - spec/gem_config/base_spec.rb
74
+ - spec/gem_config/configuration_spec.rb
75
+ - spec/gem_config/rules_spec.rb
62
76
  - spec/spec_helper.rb
63
77
  homepage: https://github.com/krautcomputing/gem_config
64
78
  licenses:
@@ -80,14 +94,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
94
  version: '0'
81
95
  requirements: []
82
96
  rubyforge_project:
83
- rubygems_version: 2.2.0
97
+ rubygems_version: 2.2.2
84
98
  signing_key:
85
99
  specification_version: 4
86
100
  summary: A nifty way to make your gem configurable.
87
101
  test_files:
88
- - spec/features/configuration_spec.rb
89
- - spec/lib/base_spec.rb
90
- - spec/lib/configuration_spec.rb
91
- - spec/lib/rules_spec.rb
102
+ - spec/gem_config/base_spec.rb
103
+ - spec/gem_config/configuration_spec.rb
104
+ - spec/gem_config/rules_spec.rb
92
105
  - spec/spec_helper.rb
93
- has_rdoc:
@@ -1,27 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe 'a module with configuration rules' do
4
- subject do
5
- Module.new do
6
- include GemConfig::Base
7
-
8
- with_configuration do
9
- has :foo, classes: String
10
- has :bar, classes: Numeric, default: 1
11
- has :baz, values: %w(lorem ipsum dolor), default: 'lorem'
12
- end
13
- end
14
- end
15
-
16
- it 'can be configured' do
17
- expect do
18
- subject.configure do |config|
19
- config.foo = 'bar'
20
- config.baz = 'ipsum'
21
- end
22
- end.to_not raise_error
23
-
24
- subject.configuration.foo.should eq('bar')
25
- subject.configuration.baz.should eq('ipsum')
26
- end
27
- end
@@ -1,11 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe GemConfig::Base do
4
- it 'provides a class method `with_configuration` when included' do
5
- m = Module.new do
6
- include GemConfig::Base
7
- end
8
-
9
- m.should respond_to(:with_configuration)
10
- end
11
- end
@@ -1,95 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe GemConfig::Configuration do
4
- subject do
5
- GemConfig::Configuration.new.tap do |configuration|
6
- configuration.rules.has :foo, default: 'bar'
7
- configuration.rules.has :bar
8
- configuration.rules.has :count
9
- configuration.rules.has :api_key, default: 'foobarbaz'
10
- configuration.foo = 'pelle'
11
- configuration.count = 123
12
- configuration
13
- end
14
- end
15
-
16
- describe '#rules' do
17
- it 'returns a Rules object' do
18
- subject.rules.is_a?(GemConfig::Rules)
19
- end
20
- end
21
-
22
- describe '#current' do
23
- it 'returns the current configuration' do
24
- subject.current.should eq(foo: 'pelle', count: 123, api_key: 'foobarbaz')
25
- end
26
- end
27
-
28
- describe '#reset' do
29
- it 'resets the configuration' do
30
- subject.tap(&:reset).current.should eq(foo: 'bar', count: nil, api_key: 'foobarbaz')
31
- end
32
- end
33
-
34
- describe '#unset' do
35
- context 'with an existing key' do
36
- context 'when that key has not been set' do
37
- it 'doesnt change anything' do
38
- expect { subject.unset(:bar) }.to_not change { subject.bar }
39
- end
40
- end
41
-
42
- context 'when that key has been set' do
43
- it 'unsets the key' do
44
- expect { subject.unset(:count) }.to change { subject.count }.from(123).to(nil)
45
- end
46
- end
47
- end
48
-
49
- context 'with a non-existing key' do
50
- it 'raises an exception' do
51
- expect { subject.unset(:pelle) }.to raise_error(GemConfig::InvalidKeyError)
52
- end
53
- end
54
- end
55
-
56
- describe 'setting a configuration option' do
57
- it 'checks if the value is allowed' do
58
- subject.rules.should_receive :check, with: [:foo, 'bar']
59
- subject.foo = 'bar'
60
- end
61
-
62
- it 'sets the configuration option if the value is allowed' do
63
- subject.rules.stub(:check, with: [:foo, 'bar'])
64
- expect do
65
- subject.foo = 'bar'
66
- end.to change { subject.foo }.from('pelle').to('bar')
67
- end
68
-
69
- it 'does not set the configuration option if the value is not allowed' do
70
- subject.rules.stub(:check, with: [:foo, 'bar']).and_raise(GemConfig::InvalidKeyError)
71
- expect do
72
- begin
73
- subject.foo = 'bar'
74
- rescue GemConfig::InvalidKeyError
75
- end
76
- end.to_not change { subject.foo }.from('bar')
77
- end
78
- end
79
-
80
- describe 'getting a configuration option' do
81
- it 'returns the value if it is set' do
82
- subject.foo = 'bar'
83
- subject.foo.should eq('bar')
84
- end
85
-
86
- it 'also accepts nil as a value' do
87
- subject.foo = nil
88
- subject.foo.should be_nil
89
- end
90
-
91
- it 'returns the default if no value but a default is set' do
92
- subject.api_key.should eq('foobarbaz')
93
- end
94
- end
95
- end