configurability 3.0.0.pre20161123172826 → 3.0.0.pre20161130162622
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.
- checksums.yaml +4 -4
- data/lib/configurability.rb +78 -8
- data/spec/configurability_spec.rb +188 -47
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec7bd9ddc5f5378d2ae449b87206d9c3c2c957ef
|
4
|
+
data.tar.gz: 1f9f7e27cb176bef33f426d0cb62fe1e99ad216f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f0370cd0b0a9bf748f5d4fd5c358008723e130e6f4183c7a03fbafdf7be0e1f33610da76fc874c439f88eb32d21fbfbae347cdf72dfca98c2885c661c7af4884
|
7
|
+
data.tar.gz: 2d5a72fe466eedbd356742ff970c076b93569b0a2c221bd92b6616f58feead5f4c81df2c4f2541493a6684ef55f6fb383120b4cbb6ea590b0c7578e9b807b020
|
data/lib/configurability.rb
CHANGED
@@ -18,14 +18,15 @@ module Configurability
|
|
18
18
|
|
19
19
|
|
20
20
|
# Library version constant
|
21
|
-
VERSION = '
|
21
|
+
VERSION = '2.99.0'
|
22
22
|
|
23
23
|
# Version-control revision constant
|
24
|
-
REVISION = %q$Revision:
|
24
|
+
REVISION = %q$Revision: 5c1ffea885e8 $
|
25
25
|
|
26
26
|
require 'configurability/deferredconfig'
|
27
27
|
|
28
28
|
autoload :Config, 'configurability/config'
|
29
|
+
autoload :DSL, 'configurability/dsl'
|
29
30
|
|
30
31
|
|
31
32
|
### The objects that have had Configurability added to them
|
@@ -233,9 +234,48 @@ module Configurability
|
|
233
234
|
end
|
234
235
|
|
235
236
|
|
236
|
-
### Default configuration method.
|
237
|
-
|
237
|
+
### Default configuration method. This will merge the provided +config+ with the defaults
|
238
|
+
### if there are any and the +config+ responds to <tt>#to_h</tt>. If the +config+ responds to
|
239
|
+
### <tt>#each_pair</tt>, any writable attributes of the calling object with the same name
|
240
|
+
### as a key of the +config+ will be called with the corresponding value. E.g.,
|
241
|
+
###
|
242
|
+
### class MyClass
|
243
|
+
### extend Configurability
|
244
|
+
### CONFIG_DEFAULTS = { environment: 'develop', apikey: 'testing-key' }
|
245
|
+
### config_key :my_class
|
246
|
+
### class << self
|
247
|
+
### attr_accessor :environment, :apikey
|
248
|
+
### end
|
249
|
+
### end
|
250
|
+
###
|
251
|
+
### config = { my_class: {apikey: 'demo-key'} }
|
252
|
+
### Configurability.configure_objects( config )
|
253
|
+
###
|
254
|
+
### MyClass.apikey
|
255
|
+
### # => 'demo-key'
|
256
|
+
### MyClass.environment
|
257
|
+
### # => 'develop'
|
258
|
+
###
|
259
|
+
def configure( config=nil )
|
260
|
+
config = self.defaults( {} ).merge( config.to_h || {} ) if
|
261
|
+
config.nil? || config.respond_to?( :to_h )
|
262
|
+
|
238
263
|
@config = config
|
264
|
+
|
265
|
+
if @config.respond_to?( :each_pair )
|
266
|
+
@config.each_pair do |key, value|
|
267
|
+
Configurability.log.debug "Looking for %p config attribute" % [ key ]
|
268
|
+
next unless self.respond_to?( "#{key}=" )
|
269
|
+
Configurability.log.debug " setting %p to %p via attr_writer" %
|
270
|
+
[ key, value ]
|
271
|
+
self.public_send( "#{key}=", value )
|
272
|
+
end
|
273
|
+
else
|
274
|
+
Configurability.log.
|
275
|
+
debug "config object (%p) isn't iterable; skipping config attributes" % [ @config ]
|
276
|
+
end
|
277
|
+
|
278
|
+
return @config
|
239
279
|
end
|
240
280
|
|
241
281
|
|
@@ -245,6 +285,36 @@ module Configurability
|
|
245
285
|
end
|
246
286
|
|
247
287
|
|
288
|
+
#
|
289
|
+
# :section: Configuration settings block
|
290
|
+
#
|
291
|
+
|
292
|
+
### Declare configuration settings and defaults. In the provided +block+, you can create
|
293
|
+
### a configuration setting using the following syntax:
|
294
|
+
###
|
295
|
+
### configurability( :my_config_key ) do
|
296
|
+
### # Declare a setting with a `nil` default
|
297
|
+
### setting :a_config_key
|
298
|
+
### # Declare one with a default value
|
299
|
+
### setting :another_config_key, default: 18
|
300
|
+
### end
|
301
|
+
###
|
302
|
+
def configurability( config_key=nil, &block )
|
303
|
+
self.config_key = config_key if config_key
|
304
|
+
|
305
|
+
if block
|
306
|
+
Configurability.log.debug "Applying config declaration block using DSL::Generator"
|
307
|
+
generator = Configurability::DSL::Generator.new( self )
|
308
|
+
generator.instance_eval( &block )
|
309
|
+
end
|
310
|
+
|
311
|
+
if (( config = Configurability.loaded_config ))
|
312
|
+
Configurability.install_config( config, self )
|
313
|
+
end
|
314
|
+
|
315
|
+
end
|
316
|
+
|
317
|
+
|
248
318
|
#
|
249
319
|
# :section: Configuration Defaults API
|
250
320
|
#
|
@@ -252,10 +322,10 @@ module Configurability
|
|
252
322
|
### The default implementation of the method called by ::gather_defaults when
|
253
323
|
### gathering configuration defaults. This method expects either a
|
254
324
|
### +DEFAULT_CONFIG+ or a +CONFIG_DEFAULTS+ constant to contain the configuration
|
255
|
-
### defaults, and will just return +
|
256
|
-
def defaults
|
325
|
+
### defaults, and will just return the +fallback+ value if neither exists.
|
326
|
+
def defaults( fallback=nil )
|
257
327
|
|
258
|
-
return
|
328
|
+
return fallback unless respond_to?( :const_defined? )
|
259
329
|
|
260
330
|
Configurability.log.debug "Looking for defaults in %p's constants." % [ self ]
|
261
331
|
if self.const_defined?( :DEFAULT_CONFIG, false )
|
@@ -266,7 +336,7 @@ module Configurability
|
|
266
336
|
return self.const_get( :CONFIG_DEFAULTS, false ).dup
|
267
337
|
else
|
268
338
|
Configurability.log.debug " no default constants."
|
269
|
-
return
|
339
|
+
return fallback
|
270
340
|
end
|
271
341
|
end
|
272
342
|
|
@@ -141,10 +141,7 @@ describe Configurability do
|
|
141
141
|
config_key :testconfig
|
142
142
|
end
|
143
143
|
|
144
|
-
config =
|
145
|
-
expect( config ).to receive( :respond_to? ).with( :testconfig ).and_return( false )
|
146
|
-
expect( config ).to receive( :respond_to? ).with( :[] ).and_return( false )
|
147
|
-
|
144
|
+
config = Object.new
|
148
145
|
expect( klass ).to receive( :configure ).with( nil )
|
149
146
|
|
150
147
|
Configurability.configure_objects( config )
|
@@ -157,15 +154,7 @@ describe Configurability do
|
|
157
154
|
config_key :testconfig
|
158
155
|
end
|
159
156
|
|
160
|
-
config =
|
161
|
-
expect( config ).to receive( :respond_to? ).with( :testconfig ).and_return( false )
|
162
|
-
expect( config ).to receive( :respond_to? ).with( :key? ).and_return( true )
|
163
|
-
expect( config ).to receive( :respond_to? ).with( :[] ).and_return( true )
|
164
|
-
expect( config ).to receive( :key? ).with( :testconfig ).and_return( false )
|
165
|
-
expect( config ).to receive( :key? ).with( 'testconfig' ).and_return( true )
|
166
|
-
expect( config ).to receive( :[] ).with( :testconfig ).and_return( nil )
|
167
|
-
expect( config ).to receive( :[] ).with( 'testconfig' ).and_return( :a_config_section )
|
168
|
-
|
157
|
+
config = { 'testconfig' => :a_config_section }
|
169
158
|
expect( klass ).to receive( :configure ).with( :a_config_section )
|
170
159
|
|
171
160
|
Configurability.configure_objects( config )
|
@@ -178,11 +167,9 @@ describe Configurability do
|
|
178
167
|
config_key :testconfig
|
179
168
|
end
|
180
169
|
|
181
|
-
config =
|
182
|
-
expect( config ).to receive( :respond_to? ).with( :testconfig ).and_return( true )
|
183
|
-
expect( config ).to receive( :testconfig ).and_return( :a_config_section )
|
184
|
-
|
170
|
+
config = OpenStruct.new( testconfig: :a_config_section )
|
185
171
|
expect( klass ).to receive( :configure ).with( :a_config_section )
|
172
|
+
|
186
173
|
Configurability.configure_objects( config )
|
187
174
|
end
|
188
175
|
|
@@ -192,10 +179,7 @@ describe Configurability do
|
|
192
179
|
object.extend( Configurability )
|
193
180
|
object.config_key = :testobjconfig
|
194
181
|
|
195
|
-
config =
|
196
|
-
expect( config ).to receive( :respond_to? ).with( :testobjconfig ).and_return( true )
|
197
|
-
expect( config ).to receive( :testobjconfig ).and_return( :a_config_section )
|
198
|
-
|
182
|
+
config = OpenStruct.new( testobjconfig: :a_config_section )
|
199
183
|
expect( object ).to receive( :configure ).with( :a_config_section )
|
200
184
|
|
201
185
|
Configurability.configure_objects( config )
|
@@ -211,12 +195,7 @@ describe Configurability do
|
|
211
195
|
config_key :subconfig
|
212
196
|
end
|
213
197
|
|
214
|
-
config =
|
215
|
-
expect( config ).to receive( :respond_to? ).with( :testconfig ).and_return( true )
|
216
|
-
expect( config ).to receive( :testconfig ).and_return( :a_config_section )
|
217
|
-
expect( config ).to receive( :respond_to? ).with( :subconfig ).and_return( true )
|
218
|
-
expect( config ).to receive( :subconfig ).and_return( :a_sub_config_section )
|
219
|
-
|
198
|
+
config = OpenStruct.new( testconfig: :a_config_section, subconfig: :a_sub_config_section )
|
220
199
|
expect( subclass ).to receive( :configure ).with( :a_sub_config_section )
|
221
200
|
|
222
201
|
Configurability.configure_objects( config )
|
@@ -229,10 +208,7 @@ describe Configurability do
|
|
229
208
|
def object.name; "testobjconfig"; end
|
230
209
|
object.extend( Configurability )
|
231
210
|
|
232
|
-
config =
|
233
|
-
expect( config ).to receive( :respond_to? ).with( :testobjconfig ).and_return( true )
|
234
|
-
expect( config ).to receive( :testobjconfig ).and_return( :a_config_section )
|
235
|
-
|
211
|
+
config = OpenStruct.new( testobjconfig: :a_config_section )
|
236
212
|
expect( object ).to receive( :configure ).with( :a_config_section )
|
237
213
|
|
238
214
|
Configurability.configure_objects( config )
|
@@ -244,10 +220,7 @@ describe Configurability do
|
|
244
220
|
def object.name; "Test Obj-Config"; end
|
245
221
|
object.extend( Configurability )
|
246
222
|
|
247
|
-
config =
|
248
|
-
expect( config ).to receive( :respond_to? ).with( :test_obj_config ).and_return( true )
|
249
|
-
expect( config ).to receive( :test_obj_config ).and_return( :a_config_section )
|
250
|
-
|
223
|
+
config = OpenStruct.new( test_obj_config: :a_config_section )
|
251
224
|
expect( object ).to receive( :configure ).with( :a_config_section )
|
252
225
|
|
253
226
|
Configurability.configure_objects( config )
|
@@ -259,10 +232,7 @@ describe Configurability do
|
|
259
232
|
object = Object.new
|
260
233
|
object.extend( Configurability )
|
261
234
|
|
262
|
-
config =
|
263
|
-
expect( config ).to receive( :respond_to? ).with( :object ).and_return( true )
|
264
|
-
expect( config ).to receive( :object ).and_return( :a_config_section )
|
265
|
-
|
235
|
+
config = OpenStruct.new( object: :a_config_section )
|
266
236
|
expect( object ).to receive( :configure ).with( :a_config_section )
|
267
237
|
|
268
238
|
Configurability.configure_objects( config )
|
@@ -276,10 +246,7 @@ describe Configurability do
|
|
276
246
|
end
|
277
247
|
end
|
278
248
|
|
279
|
-
config =
|
280
|
-
expect( config ).to receive( :respond_to? ).with( :dbobject ).and_return( true )
|
281
|
-
expect( config ).to receive( :dbobject ).and_return( :a_config_section )
|
282
|
-
|
249
|
+
config = OpenStruct.new( dbobject: :a_config_section )
|
283
250
|
expect( My::DbObject ).to receive( :configure ).with( :a_config_section )
|
284
251
|
|
285
252
|
Configurability.configure_objects( config )
|
@@ -292,10 +259,7 @@ describe Configurability do
|
|
292
259
|
object = objectclass.new
|
293
260
|
object.extend( Configurability )
|
294
261
|
|
295
|
-
config =
|
296
|
-
expect( config ).to receive( :respond_to? ).with( :anonymous ).and_return( true )
|
297
|
-
expect( config ).to receive( :anonymous ).and_return( :a_config_section )
|
298
|
-
|
262
|
+
config = OpenStruct.new( anonymous: :a_config_section )
|
299
263
|
expect( object ).to receive( :configure ).with( :a_config_section )
|
300
264
|
|
301
265
|
Configurability.configure_objects( config )
|
@@ -489,6 +453,183 @@ describe Configurability do
|
|
489
453
|
expect( config.spanishconfig.uno ).to eq( 1 )
|
490
454
|
end
|
491
455
|
|
456
|
+
|
457
|
+
it "responds with the provided result if the object with Configurabilty has no defaults" do
|
458
|
+
klass = Class.new do
|
459
|
+
extend Configurability
|
460
|
+
config_key :testconfig
|
461
|
+
end
|
462
|
+
|
463
|
+
expect( klass.defaults({}) ).to eq( {} )
|
464
|
+
end
|
465
|
+
|
466
|
+
end
|
467
|
+
|
468
|
+
|
469
|
+
describe "default configure method" do
|
470
|
+
|
471
|
+
let!( :configurable_class ) do
|
472
|
+
Class.new do
|
473
|
+
include Configurability
|
474
|
+
config_key :testconfig
|
475
|
+
class << self
|
476
|
+
attr_accessor :apikey, :environment
|
477
|
+
end
|
478
|
+
end
|
479
|
+
end
|
480
|
+
|
481
|
+
|
482
|
+
it "sets attributes of the configured object that correspond with the keys of the config" do
|
483
|
+
config = OpenStruct.new( apikey: 'the-api-key', environment: 'production' )
|
484
|
+
|
485
|
+
configurable_class.configure( config )
|
486
|
+
|
487
|
+
expect( configurable_class.apikey ).to eq( 'the-api-key' )
|
488
|
+
expect( configurable_class.environment ).to eq( 'production' )
|
489
|
+
end
|
490
|
+
|
491
|
+
|
492
|
+
it "merges in defaults if they exist" do
|
493
|
+
configurable_class::DEFAULT_CONFIG = { environment: 'development' }
|
494
|
+
config = OpenStruct.new( apikey: 'the-api-key' )
|
495
|
+
|
496
|
+
configurable_class.configure( config )
|
497
|
+
|
498
|
+
expect( configurable_class.apikey ).to eq( 'the-api-key' )
|
499
|
+
expect( configurable_class.environment ).to eq( 'development' )
|
500
|
+
end
|
501
|
+
|
502
|
+
|
503
|
+
it "merges in defaults even if configured with `nil`" do
|
504
|
+
configurable_class::DEFAULT_CONFIG = { environment: 'development' }
|
505
|
+
|
506
|
+
configurable_class.configure
|
507
|
+
|
508
|
+
expect( configurable_class.apikey ).to eq( nil )
|
509
|
+
expect( configurable_class.environment ).to eq( 'development' )
|
510
|
+
end
|
511
|
+
|
512
|
+
|
513
|
+
it "returns the merged config for overrides" do
|
514
|
+
configurable_class::DEFAULT_CONFIG = { environment: 'development' }
|
515
|
+
|
516
|
+
rval = configurable_class.configure( apikey: 'the-api-key' )
|
517
|
+
|
518
|
+
expect( rval[:apikey] ).to eq( 'the-api-key' )
|
519
|
+
expect( rval[:environment] ).to eq( 'development' )
|
520
|
+
end
|
521
|
+
|
522
|
+
end
|
523
|
+
|
524
|
+
|
525
|
+
describe "configuration DSL" do
|
526
|
+
|
527
|
+
let( :mod ) do
|
528
|
+
mod = Module.new
|
529
|
+
mod.extend( Configurability )
|
530
|
+
mod
|
531
|
+
end
|
532
|
+
|
533
|
+
let( :instance ) do
|
534
|
+
instance = Object.new
|
535
|
+
instance.extend( Configurability )
|
536
|
+
instance
|
537
|
+
end
|
538
|
+
|
539
|
+
|
540
|
+
it "just sets the config key if called with a name" do
|
541
|
+
mod.configurability( :testconfig )
|
542
|
+
expect( mod.config_key ).to eq( :testconfig )
|
543
|
+
end
|
544
|
+
|
545
|
+
|
546
|
+
it "allows the declaration of one or more config options" do
|
547
|
+
mod.configurability( :testconfig ) do
|
548
|
+
setting :environment
|
549
|
+
setting :apikey
|
550
|
+
end
|
551
|
+
|
552
|
+
expect {
|
553
|
+
mod.environment = 'development'
|
554
|
+
}.to change { mod.environment }.to( 'development' )
|
555
|
+
expect {
|
556
|
+
mod.apikey = 'jofRtkw&QzCoukGwAWDMjyTkQzWnCXhhgEs'
|
557
|
+
}.to change { mod.apikey }.to( 'jofRtkw&QzCoukGwAWDMjyTkQzWnCXhhgEs' )
|
558
|
+
end
|
559
|
+
|
560
|
+
|
561
|
+
it "works for object instances with Configurability as well" do
|
562
|
+
instance.configurability( :testconfig ) do
|
563
|
+
setting :environment
|
564
|
+
setting :apikey
|
565
|
+
end
|
566
|
+
|
567
|
+
expect {
|
568
|
+
instance.environment = 'development'
|
569
|
+
}.to change { instance.environment }.to( 'development' )
|
570
|
+
expect {
|
571
|
+
instance.apikey = 'jofRtkw&QzCoukGwAWDMjyTkQzWnCXhhgEs'
|
572
|
+
}.to change { instance.apikey }.to( 'jofRtkw&QzCoukGwAWDMjyTkQzWnCXhhgEs' )
|
573
|
+
end
|
574
|
+
|
575
|
+
|
576
|
+
it "allow the declaration of one or more config options with an inferred config key" do
|
577
|
+
mod.configurability do
|
578
|
+
setting :environment
|
579
|
+
setting :apikey
|
580
|
+
end
|
581
|
+
|
582
|
+
expect {
|
583
|
+
mod.environment = 'development'
|
584
|
+
}.to change { mod.environment }.to( 'development' )
|
585
|
+
expect {
|
586
|
+
mod.apikey = 'jofRtkw&QzCoukGwAWDMjyTkQzWnCXhhgEs'
|
587
|
+
}.to change { mod.apikey }.to( 'jofRtkw&QzCoukGwAWDMjyTkQzWnCXhhgEs' )
|
588
|
+
end
|
589
|
+
|
590
|
+
|
591
|
+
it "allow the declaration of one or more config options with defaults" do
|
592
|
+
mod.configurability( :testconfig ) do
|
593
|
+
setting :environment, default: 'development'
|
594
|
+
setting :apikey, default: '<none set>'
|
595
|
+
end
|
596
|
+
|
597
|
+
expect {
|
598
|
+
mod.environment = 'production'
|
599
|
+
}.to change { mod.environment }.
|
600
|
+
from( 'development' ).
|
601
|
+
to( 'production' )
|
602
|
+
|
603
|
+
expect {
|
604
|
+
mod.apikey = 'jofRtkw&QzCoukGwAWDMjyTkQzWnCXhhgEs'
|
605
|
+
}.to change { mod.apikey }.
|
606
|
+
from( '<none set>' ).
|
607
|
+
to( 'jofRtkw&QzCoukGwAWDMjyTkQzWnCXhhgEs' )
|
608
|
+
|
609
|
+
expect( mod.defaults ).to include(
|
610
|
+
environment: 'development',
|
611
|
+
apikey: '<none set>'
|
612
|
+
)
|
613
|
+
end
|
614
|
+
|
615
|
+
|
616
|
+
it "installs the current config after its done if it's already been loaded" do
|
617
|
+
config = OpenStruct.new( testconfig: {
|
618
|
+
environment: 'production',
|
619
|
+
apikey: 'jofRtkw&QzCoukGwAWDMjyTkQzWnCXhhgEs'
|
620
|
+
})
|
621
|
+
|
622
|
+
Configurability.configure_objects( config )
|
623
|
+
|
624
|
+
mod.configurability( :testconfig ) do
|
625
|
+
setting :environment, default: 'development'
|
626
|
+
setting :apikey, default: '<none set>'
|
627
|
+
end
|
628
|
+
|
629
|
+
expect( mod.environment ).to eq( 'production' )
|
630
|
+
expect( mod.apikey ).to eq( 'jofRtkw&QzCoukGwAWDMjyTkQzWnCXhhgEs' )
|
631
|
+
end
|
632
|
+
|
492
633
|
end
|
493
634
|
|
494
635
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configurability
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0.
|
4
|
+
version: 3.0.0.pre20161130162622
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Granger
|
@@ -36,7 +36,7 @@ cert_chain:
|
|
36
36
|
w8aNA5re5+Rt/Vvjxj5AcEnZnZiz5x959NaddQocX32Z1unHw44pzRNUur1GInfW
|
37
37
|
p4vpx2kUSFSAGjtCbDGTNV2AH8w9OU4xEmNz8c5lyoA=
|
38
38
|
-----END CERTIFICATE-----
|
39
|
-
date: 2016-
|
39
|
+
date: 2016-12-01 00:00:00.000000000 Z
|
40
40
|
dependencies:
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: loggability
|