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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 943269b9166513da09c866d6d4bae9e8bdb7f79c
4
- data.tar.gz: d1988d15d10df3182c134075461db5640f0bd595
3
+ metadata.gz: ac2beb0c3075dc310f01561ffded475028a3bbb8
4
+ data.tar.gz: 235d02c3dd6a3cb91f65b34780d359f13c602d78
5
5
  SHA512:
6
- metadata.gz: 23d2195e6ae6b208e3a411b2de8904bef726a15c8dc5a4bf06f787fede78161485e3c24404655e63adf450d7fb0a8c7302453f87ee659d87ed171b2f7b39c2b0
7
- data.tar.gz: d04944fa211c31db6f20df83f45824bf486c22de4568ce92b38860f875526edf197915d7619b6b07530948534c3449f747c423c6265c08e475eb90ff27de6444
6
+ metadata.gz: 373862b787b95c189ecee41b186a3e8ad64cab9cb64d0985f645e029c1a0ed1cf5b7fac8228bccd060d03e7306d330820432bd1fbe140d0922b24f0a8f4a9575
7
+ data.tar.gz: 4a65de2f0f811ac061e26079faaa4144a8dd735532d3f7192db926edf2063489619ecf0b8d903b1666bca818dded1774aa84ffa4a11c2612542e7d80ec0ef13b
data/lib/canfig.rb CHANGED
@@ -5,7 +5,7 @@ require 'canfig/config'
5
5
  require 'canfig/open_config'
6
6
  require 'canfig/module'
7
7
  require 'canfig/class'
8
- require 'canfig/object'
8
+ require 'canfig/instance'
9
9
 
10
10
  module Canfig
11
11
  end
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| configure_option(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 configure_option(key, val)
26
- raise ArgumentError, "#{key} is not an allowed configuration option" unless allowed?(key)
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
- @changed[key] = [@options[key], val]
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
- @changed = {}
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 = @options.dup
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 configure_option(opt, args.first) if allowed?(meth)
93
+ return set(opt, args.first) if allowed?(opt)
82
94
  else
83
- return @options[meth] if allowed?(meth)
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
@@ -1,5 +1,5 @@
1
1
  module Canfig
2
- module Object
2
+ module Instance
3
3
  def configuration
4
4
  @configuration ||= Canfig::OpenConfig.new
5
5
  end
@@ -3,5 +3,9 @@ module Canfig
3
3
  def allowed?(opt)
4
4
  true
5
5
  end
6
+
7
+ def to_h
8
+ @state.to_h
9
+ end
6
10
  end
7
11
  end
@@ -1,3 +1,3 @@
1
1
  module Canfig
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -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
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Canfig do
4
+ end
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.3
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