canfig 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/canfig.rb +1 -1
- data/lib/canfig/config.rb +27 -14
- data/lib/canfig/{object.rb → instance.rb} +1 -1
- data/lib/canfig/open_config.rb +4 -0
- data/lib/canfig/version.rb +1 -1
- data/spec/canfig/config_spec.rb +222 -0
- data/spec/canfig/open_config_spec.rb +20 -0
- data/spec/canfig_spec.rb +4 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac2beb0c3075dc310f01561ffded475028a3bbb8
|
4
|
+
data.tar.gz: 235d02c3dd6a3cb91f65b34780d359f13c602d78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 373862b787b95c189ecee41b186a3e8ad64cab9cb64d0985f645e029c1a0ed1cf5b7fac8228bccd060d03e7306d330820432bd1fbe140d0922b24f0a8f4a9575
|
7
|
+
data.tar.gz: 4a65de2f0f811ac061e26079faaa4144a8dd735532d3f7192db926edf2063489619ecf0b8d903b1666bca818dded1774aa84ffa4a11c2612542e7d80ec0ef13b
|
data/lib/canfig.rb
CHANGED
data/lib/canfig/config.rb
CHANGED
@@ -2,7 +2,6 @@ module Canfig
|
|
2
2
|
class Config
|
3
3
|
|
4
4
|
def configure(argh={}, &block)
|
5
|
-
@options ||= {}
|
6
5
|
save_state! do
|
7
6
|
configure_with_args argh
|
8
7
|
configure_with_block &block
|
@@ -12,7 +11,7 @@ module Canfig
|
|
12
11
|
|
13
12
|
def configure_with_args(argh)
|
14
13
|
save_state! do
|
15
|
-
argh.symbolize_keys.each { |key,val|
|
14
|
+
argh.symbolize_keys.each { |key,val| set(key, val) }
|
16
15
|
end
|
17
16
|
end
|
18
17
|
|
@@ -22,19 +21,33 @@ module Canfig
|
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
25
|
-
def
|
26
|
-
raise
|
24
|
+
def set(key, val)
|
25
|
+
raise NoMethodError, "undefined method `#{key.to_s}=' for #{self.to_s}" unless allowed?(key)
|
27
26
|
|
28
27
|
save_state! do
|
29
|
-
@
|
30
|
-
@options[key] = val
|
28
|
+
@state[key] = val
|
31
29
|
end
|
32
30
|
end
|
33
31
|
|
32
|
+
def []=(key, val)
|
33
|
+
set key, val
|
34
|
+
end
|
35
|
+
|
36
|
+
def get(key)
|
37
|
+
raise NoMethodError, "undefined method `#{key.to_s}' for #{self.to_s}" unless allowed?(key)
|
38
|
+
@state[key]
|
39
|
+
end
|
40
|
+
|
41
|
+
def [](key)
|
42
|
+
get key
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_h
|
46
|
+
Hash[@allowed.map { |key| [key, @state[key]] }]
|
47
|
+
end
|
48
|
+
|
34
49
|
def changed
|
35
|
-
@
|
36
|
-
@options.each { |key,val| @changed[key] = [@saved_state[key], val] if @saved_state[key] != val }
|
37
|
-
@changed
|
50
|
+
Hash[@state.map { |key,val| [key, [@saved_state[key], val]] if @saved_state[key] != val }.compact]
|
38
51
|
end
|
39
52
|
|
40
53
|
def changed?(key)
|
@@ -43,8 +56,7 @@ module Canfig
|
|
43
56
|
|
44
57
|
def save_state!(&block)
|
45
58
|
if save_state?
|
46
|
-
@saved_state =
|
47
|
-
@changed = {}
|
59
|
+
@saved_state = to_h
|
48
60
|
|
49
61
|
if block_given?
|
50
62
|
disable_state_saves!
|
@@ -78,9 +90,9 @@ module Canfig
|
|
78
90
|
def method_missing(meth, *args, &block)
|
79
91
|
if meth.to_s.match(/=\Z/)
|
80
92
|
opt = meth.to_s.gsub(/=/,'').to_sym
|
81
|
-
return
|
93
|
+
return set(opt, args.first) if allowed?(opt)
|
82
94
|
else
|
83
|
-
return @
|
95
|
+
return @state[meth] if allowed?(meth)
|
84
96
|
end
|
85
97
|
|
86
98
|
super
|
@@ -90,7 +102,8 @@ module Canfig
|
|
90
102
|
|
91
103
|
def initialize(*args, &block)
|
92
104
|
options = args.extract_options!
|
93
|
-
@allowed = options.symbolize_keys.keys
|
105
|
+
@allowed = (args + options.symbolize_keys.keys).uniq.map(&:to_sym)
|
106
|
+
@state = {}
|
94
107
|
enable_state_saves!
|
95
108
|
configure options, &block
|
96
109
|
end
|
data/lib/canfig/open_config.rb
CHANGED
data/lib/canfig/version.rb
CHANGED
@@ -0,0 +1,222 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Canfig::Config do
|
4
|
+
describe '#initialize' do
|
5
|
+
context 'when passed a list of keys' do
|
6
|
+
subject { Canfig::Config.new(:foo, :bar, :baz) }
|
7
|
+
|
8
|
+
it 'adds those keys to the allowed keys' do
|
9
|
+
expect(subject.allowed?(:foo)).to be_true
|
10
|
+
expect(subject.allowed?(:bar)).to be_true
|
11
|
+
expect(subject.allowed?(:baz)).to be_true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when passed a hash of keys and values' do
|
16
|
+
subject { Canfig::Config.new(foo: 'foo', bar: 'bar', baz: 'baz') }
|
17
|
+
|
18
|
+
it 'adds those keys to the allowed keys' do
|
19
|
+
expect(subject.allowed?(:foo)).to be_true
|
20
|
+
expect(subject.allowed?(:bar)).to be_true
|
21
|
+
expect(subject.allowed?(:baz)).to be_true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when passed a list of keys and a hash of keys and values' do
|
26
|
+
subject { Canfig::Config.new(:foo, :bar, baz: 'baz') }
|
27
|
+
|
28
|
+
it 'adds those keys to the allowed keys' do
|
29
|
+
expect(subject.allowed?(:foo)).to be_true
|
30
|
+
expect(subject.allowed?(:bar)).to be_true
|
31
|
+
expect(subject.allowed?(:baz)).to be_true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#set' do
|
37
|
+
context 'when the key is allowed' do
|
38
|
+
subject { Canfig::Config.new(:foo) }
|
39
|
+
|
40
|
+
it 'sets the key/value pair' do
|
41
|
+
subject.set(:foo, 'bar')
|
42
|
+
expect(subject.get(:foo)).to eql('bar')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'when the key is not allowed' do
|
47
|
+
subject { Canfig::Config.new(:baz) }
|
48
|
+
|
49
|
+
it 'raises a NoMethodError' do
|
50
|
+
expect { subject.set(:foo, 'bar') }.to raise_exception(NoMethodError)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe '#get' do
|
56
|
+
context 'when the key is allowed' do
|
57
|
+
subject { Canfig::Config.new(:baz, foo: 'bar') }
|
58
|
+
|
59
|
+
it 'returns the corresponding value' do
|
60
|
+
expect(subject.get(:foo)).to eql('bar')
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'when the key is not set' do
|
64
|
+
it 'returns nil' do
|
65
|
+
expect(subject.get(:baz)).to be_nil
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'when the key is not allowed' do
|
71
|
+
subject { Canfig::Config.new(:baz) }
|
72
|
+
|
73
|
+
it 'raises a NoMethodError' do
|
74
|
+
expect { subject.get(:foo) }.to raise_exception(NoMethodError)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe '#to_h' do
|
80
|
+
subject { Canfig::Config.new(:foo, bar: 'bar', baz: 'baz') }
|
81
|
+
|
82
|
+
it 'returns a hash' do
|
83
|
+
expect(subject.to_h).to be_an_instance_of(Hash)
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'contain all allowed keys' do
|
87
|
+
[:foo, :bar, :baz].each do |key|
|
88
|
+
expect(subject.to_h.keys).to include(key)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe '#configure_with_args' do
|
94
|
+
subject { Canfig::Config.new(:foo, bar: 'bar', baz: 'baz') }
|
95
|
+
|
96
|
+
context 'when the passed keys are allowed' do
|
97
|
+
it 'sets the key/value pairs passed the hash' do
|
98
|
+
subject.configure_with_args({foo: 1, bar: 2})
|
99
|
+
expect(subject.get(:foo)).to eql(1)
|
100
|
+
expect(subject.get(:bar)).to eql(2)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'when the passed keys are not allowed' do
|
105
|
+
it 'raises a NoMethodError' do
|
106
|
+
expect { subject.configure_with_args({abc: '123'}) }.to raise_exception(NoMethodError)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe '#configure_with_block' do
|
112
|
+
subject { Canfig::Config.new(:foo, bar: 'bar', baz: 'baz') }
|
113
|
+
|
114
|
+
it 'accepts a block' do
|
115
|
+
expect { subject.configure_with_block { |config| } }.to_not raise_exception
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'executes the block with instance_eval' do
|
119
|
+
conf = nil
|
120
|
+
subject.configure_with_block { |config| conf = config }
|
121
|
+
expect(conf).to eql(subject)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe '#configure' do
|
126
|
+
subject { Canfig::Config.new(:foo, bar: 'bar', baz: 'baz') }
|
127
|
+
|
128
|
+
it 'returns self' do
|
129
|
+
expect(subject.configure).to eql(subject)
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'accepts a hash and a block' do
|
133
|
+
subject.configure foo: 'foo' do |config|
|
134
|
+
config.bar = 'baz'
|
135
|
+
config.baz = 'bar'
|
136
|
+
end
|
137
|
+
|
138
|
+
expect(subject.get(:foo)).to eql('foo')
|
139
|
+
expect(subject.get(:bar)).to eql('baz')
|
140
|
+
expect(subject.get(:baz)).to eql('bar')
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'when referencing option keys' do
|
145
|
+
subject { Canfig::Config.new(:foo, bar: 'bar', baz: 'baz') }
|
146
|
+
|
147
|
+
context 'that are allowed' do
|
148
|
+
context 'using getter methods' do
|
149
|
+
it 'returns the value' do
|
150
|
+
expect(subject.bar).to eql('bar')
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
context 'using setter methods' do
|
155
|
+
it 'sets the passed value to the key' do
|
156
|
+
subject.bar = 'foo'
|
157
|
+
expect(subject.bar).to eql('foo')
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context 'that are not allowed' do
|
163
|
+
context 'using getter methods' do
|
164
|
+
it 'returns the value' do
|
165
|
+
expect { subject.abc }.to raise_exception(NoMethodError)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
context 'using setter methods' do
|
170
|
+
it 'sets the passed value to the key' do
|
171
|
+
expect { subject.abc = 123 }.to raise_exception(NoMethodError)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context 'when saving state' do
|
178
|
+
subject { Canfig::Config.new(:foo, bar: 'bar', baz: 'baz') }
|
179
|
+
|
180
|
+
describe '#changed' do
|
181
|
+
it 'returns a hash' do
|
182
|
+
subject.configure do |config|
|
183
|
+
config.foo = 'test'
|
184
|
+
config.bar = 1
|
185
|
+
end
|
186
|
+
|
187
|
+
expect(subject.changed).to be_an_instance_of(Hash)
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'contains the changed keys' do
|
191
|
+
subject.configure do |config|
|
192
|
+
config.foo = 'test'
|
193
|
+
config.bar = 1
|
194
|
+
end
|
195
|
+
|
196
|
+
[:foo, :bar].each do |key|
|
197
|
+
expect(subject.changed.keys).to include(key)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'contains only the changed keys' do
|
202
|
+
subject.configure do |config|
|
203
|
+
config.foo = 'test'
|
204
|
+
config.bar = 1
|
205
|
+
end
|
206
|
+
|
207
|
+
expect(subject.changed.keys).to_not include(:baz)
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'contains the old and new values' do
|
211
|
+
subject.configure do |config|
|
212
|
+
config.foo = 'test'
|
213
|
+
config.bar = 1
|
214
|
+
end
|
215
|
+
|
216
|
+
expect(subject.changed[:foo]).to eql([nil,'test'])
|
217
|
+
expect(subject.changed[:bar]).to eql(['bar',1])
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Canfig::OpenConfig do
|
4
|
+
describe '#to_h' do
|
5
|
+
subject { Canfig::OpenConfig.new() }
|
6
|
+
|
7
|
+
it 'returns a hash' do
|
8
|
+
expect(subject.to_h).to be_an_instance_of(Hash)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'contain all set keys' do
|
12
|
+
subject.set :foo, 'foo'
|
13
|
+
subject.set :bar, 'bar'
|
14
|
+
subject.set :baz, 'baz'
|
15
|
+
[:foo, :bar, :baz].each do |key|
|
16
|
+
expect(subject.to_h.keys).to include(key)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/canfig_spec.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canfig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mark Rebec
|
@@ -62,10 +62,13 @@ files:
|
|
62
62
|
- lib/canfig.rb
|
63
63
|
- lib/canfig/class.rb
|
64
64
|
- lib/canfig/config.rb
|
65
|
+
- lib/canfig/instance.rb
|
65
66
|
- lib/canfig/module.rb
|
66
|
-
- lib/canfig/object.rb
|
67
67
|
- lib/canfig/open_config.rb
|
68
68
|
- lib/canfig/version.rb
|
69
|
+
- spec/canfig/config_spec.rb
|
70
|
+
- spec/canfig/open_config_spec.rb
|
71
|
+
- spec/canfig_spec.rb
|
69
72
|
- spec/spec_helper.rb
|
70
73
|
homepage: http://github.com/markrebec/canfig
|
71
74
|
licenses: []
|
@@ -91,4 +94,7 @@ signing_key:
|
|
91
94
|
specification_version: 4
|
92
95
|
summary: Dead simple canned configuration for gems or whatever
|
93
96
|
test_files:
|
97
|
+
- spec/canfig/config_spec.rb
|
98
|
+
- spec/canfig/open_config_spec.rb
|
99
|
+
- spec/canfig_spec.rb
|
94
100
|
- spec/spec_helper.rb
|