mixlib-config 2.0.0.rc.4 → 2.0.0.rc.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -185,6 +185,7 @@ module Mixlib
185
185
  def configurable(symbol, &block)
186
186
  unless configurables[symbol]
187
187
  configurables[symbol] = Configurable.new(symbol)
188
+ define_attr_accessor_methods(symbol)
188
189
  end
189
190
  if block
190
191
  block.call(configurables[symbol])
@@ -287,48 +288,30 @@ module Mixlib
287
288
  # === Raises
288
289
  # <UnknownConfigOptionError>:: If the config option does not exist and strict mode is on.
289
290
  def method_missing(method_symbol, *args)
290
- num_args = args.length
291
- # Setting
292
- if num_args > 0
293
- method_symbol = $1.to_sym if method_symbol.to_s =~ /(.+)=$/
294
- internal_set(method_symbol, num_args == 1 ? args[0] : args)
295
- end
296
-
297
- # Returning
298
- internal_get(method_symbol)
291
+ method_symbol = $1.to_sym if method_symbol.to_s =~ /(.+)=$/
292
+ internal_get_or_set(method_symbol, *args)
299
293
  end
300
294
 
301
- # Internal dispatch setter, calls the setter (def myvar=) if it is defined,
302
- # otherwise calls configurable(method_symbol).set(value)
303
- #
295
+ private
296
+
297
+ # Internal dispatch setter for config values.
304
298
  # === Parameters
305
- # method_symbol<Symbol>:: Name of the method (variable setter)
299
+ # symbol<Symbol>:: Name of the method (variable setter)
306
300
  # value<Object>:: Value to be set in config hash
307
301
  #
308
- def internal_set(method_symbol,value)
309
- # It would be nice not to have to
310
- method_name = method_symbol.id2name
311
-
312
- if self.respond_to?("#{method_name}=".to_sym)
313
- self.send("#{method_name}=", value)
302
+ def internal_set(symbol,value)
303
+ if configurables.has_key?(symbol)
304
+ configurables[symbol].set(self.configuration, value)
314
305
  else
315
- if configurables.has_key?(method_symbol)
316
- configurables[method_symbol].set(self.configuration, value)
317
- else
318
- if config_strict_mode == :warn
319
- Chef::Log.warn("Setting unsupported config value #{method_name}..")
320
- elsif config_strict_mode
321
- raise UnknownConfigOptionError, "Cannot set unsupported config value #{method_name}."
322
- end
323
- configuration[method_symbol] = value
306
+ if config_strict_mode == :warn
307
+ Chef::Log.warn("Setting unsupported config value #{method_name}..")
308
+ elsif config_strict_mode
309
+ raise UnknownConfigOptionError, "Cannot set unsupported config value #{method_name}."
324
310
  end
311
+ configuration[symbol] = value
325
312
  end
326
313
  end
327
314
 
328
- protected :internal_set
329
-
330
- private
331
-
332
315
  def internal_get(symbol)
333
316
  if configurables.has_key?(symbol)
334
317
  configurables[symbol].get(self.configuration)
@@ -341,5 +324,29 @@ module Mixlib
341
324
  configuration[symbol]
342
325
  end
343
326
  end
327
+
328
+ def internal_get_or_set(symbol,*args)
329
+ num_args = args.length
330
+ # Setting
331
+ if num_args > 0
332
+ internal_set(symbol, num_args == 1 ? args[0] : args)
333
+ end
334
+
335
+ # Returning
336
+ internal_get(symbol)
337
+ end
338
+
339
+ def define_attr_accessor_methods(symbol)
340
+ # When Ruby 1.8.7 is no longer supported, this stuff can be done with define_singleton_method!
341
+ meta = class << self; self; end
342
+ # Setter
343
+ meta.send :define_method, "#{symbol.to_s}=".to_sym do |value|
344
+ internal_set(symbol, value)
345
+ end
346
+ # Getter
347
+ meta.send :define_method, symbol do |*args|
348
+ internal_get_or_set(symbol, *args)
349
+ end
350
+ end
344
351
  end
345
352
  end
@@ -19,7 +19,7 @@
19
19
  module Mixlib
20
20
  module Config
21
21
 
22
- VERSION = "2.0.0.rc.4"
22
+ VERSION = "2.0.0.rc.5"
23
23
 
24
24
  end
25
25
  end
@@ -151,11 +151,11 @@ describe Mixlib::Config do
151
151
  before do
152
152
  @klass = Class.new
153
153
  @klass.extend(::Mixlib::Config)
154
- @klass.class_eval(<<-EVAL)
154
+ @klass.class_eval do
155
155
  config_attr_writer :test_method do |blah|
156
156
  blah.is_a?(Integer) ? blah * 1000 : blah
157
157
  end
158
- EVAL
158
+ end
159
159
  end
160
160
 
161
161
  it "should multiply an integer by 1000" do
@@ -186,6 +186,64 @@ describe Mixlib::Config do
186
186
 
187
187
  end
188
188
 
189
+ describe "When a configurable exists" do
190
+ before :each do
191
+ @klass = Class.new
192
+ @klass.extend(::Mixlib::Config)
193
+ @klass.class_eval do
194
+ configurable :daemonizeme
195
+ default :a, 1
196
+ config_attr_writer(:b) { |v| v }
197
+ config_context(:c)
198
+ end
199
+ end
200
+
201
+ it "Getter methods are created for the configurable" do
202
+ @klass.respond_to?(:daemonizeme).should == true
203
+ @klass.respond_to?(:a).should == true
204
+ @klass.respond_to?(:b).should == true
205
+ @klass.respond_to?(:c).should == true
206
+ @klass.respond_to?(:z).should == false
207
+ end
208
+
209
+ it "Setter methods are created for the configurable" do
210
+ @klass.respond_to?("daemonizeme=".to_sym).should == true
211
+ @klass.respond_to?("a=".to_sym).should == true
212
+ @klass.respond_to?("b=".to_sym).should == true
213
+ @klass.respond_to?("c=".to_sym).should == true
214
+ @klass.respond_to?("z=".to_sym).should == false
215
+ end
216
+
217
+ describe "and extra methods have been dumped into Object" do
218
+ class NopeError < StandardError
219
+ end
220
+ before :each do
221
+ Object.send :define_method, :daemonizeme do
222
+ raise NopeError, "NOPE"
223
+ end
224
+ Object.send :define_method, "daemonizeme=".to_sym do
225
+ raise NopeError, "NOPE"
226
+ end
227
+ end
228
+
229
+ it 'Normal classes call the extra method' do
230
+ normal_class = Class.new
231
+ normal_class.extend(::Mixlib::Config)
232
+ lambda { normal_class.daemonizeme }.should raise_error(NopeError)
233
+ end
234
+
235
+ it 'Configurables with the same name as the extra method can be set' do
236
+ @klass.daemonizeme = 10
237
+ @klass[:daemonizeme].should == 10
238
+ end
239
+
240
+ it 'Configurables with the same name as the extra method can be retrieved' do
241
+ @klass[:daemonizeme] = 10
242
+ @klass.daemonizeme.should == 10
243
+ end
244
+ end
245
+ end
246
+
189
247
  describe "When config has a default value" do
190
248
  before :each do
191
249
  @klass = Class.new
@@ -219,6 +277,48 @@ describe Mixlib::Config do
219
277
  end
220
278
  end
221
279
 
280
+ describe "When config has a default value block" do
281
+ before :each do
282
+ @klass = Class.new
283
+ @klass.extend(::Mixlib::Config)
284
+ @klass.class_eval do
285
+ default :x, 4
286
+ default(:attr) { x*2}
287
+ end
288
+ end
289
+
290
+ it "should default to that value" do
291
+ @klass.attr.should == 8
292
+ end
293
+
294
+ it "should be recalculated each time it is retrieved" do
295
+ @klass.attr.should == 8
296
+ @klass.x = 2
297
+ @klass.attr.should == 4
298
+ end
299
+
300
+ it "should default to that value when retrieved as a hash" do
301
+ @klass[:attr].should == 8
302
+ end
303
+
304
+ it "should be settable to another value" do
305
+ @klass.attr 5
306
+ @klass.attr.should == 5
307
+ end
308
+
309
+ it "should still default to that value after delete" do
310
+ @klass.attr 5
311
+ @klass.delete(:attr)
312
+ @klass.attr.should == 8
313
+ end
314
+
315
+ it "should still default to that value after reset" do
316
+ @klass.attr 5
317
+ @klass.reset
318
+ @klass.attr.should == 8
319
+ end
320
+ end
321
+
222
322
  describe "When config has an array default value" do
223
323
  before :each do
224
324
  @klass = Class.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixlib-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.rc.4
4
+ version: 2.0.0.rc.5
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-16 00:00:00.000000000 Z
12
+ date: 2013-09-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake