cascading_configuration 1.0.0

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.
Files changed (43) hide show
  1. data/CHANGELOG.md +40 -0
  2. data/README.md +211 -0
  3. data/lib/cascading_configuration/array/sorted/unique.rb +110 -0
  4. data/lib/cascading_configuration/array/sorted.rb +106 -0
  5. data/lib/cascading_configuration/array/unique.rb +106 -0
  6. data/lib/cascading_configuration/array.rb +103 -0
  7. data/lib/cascading_configuration/core/enable_instance_support.rb +22 -0
  8. data/lib/cascading_configuration/core/enable_module_support.rb +24 -0
  9. data/lib/cascading_configuration/core/encapsulation.rb +343 -0
  10. data/lib/cascading_configuration/core/instance_controller/extension_module.rb +38 -0
  11. data/lib/cascading_configuration/core/instance_controller/support_module/instance_support_module.rb +21 -0
  12. data/lib/cascading_configuration/core/instance_controller/support_module/singleton_support_module.rb +21 -0
  13. data/lib/cascading_configuration/core/instance_controller/support_module.rb +253 -0
  14. data/lib/cascading_configuration/core/instance_controller.rb +840 -0
  15. data/lib/cascading_configuration/core/module/block_configurations/cascading_variables.rb +129 -0
  16. data/lib/cascading_configuration/core/module/block_configurations.rb +15 -0
  17. data/lib/cascading_configuration/core/module/extended_configurations/compositing_objects.rb +173 -0
  18. data/lib/cascading_configuration/core/module/extended_configurations.rb +65 -0
  19. data/lib/cascading_configuration/core/module/inheriting_values.rb +64 -0
  20. data/lib/cascading_configuration/core/module.rb +284 -0
  21. data/lib/cascading_configuration/core.rb +23 -0
  22. data/lib/cascading_configuration/hash.rb +103 -0
  23. data/lib/cascading_configuration/setting.rb +87 -0
  24. data/lib/cascading_configuration.rb +47 -0
  25. data/lib/namespaces.rb +9 -0
  26. data/lib/requires.rb +46 -0
  27. data/spec/cascading_configuration/array/sorted/unique_spec.rb +742 -0
  28. data/spec/cascading_configuration/array/sorted_spec.rb +741 -0
  29. data/spec/cascading_configuration/array/unique_spec.rb +746 -0
  30. data/spec/cascading_configuration/array_spec.rb +768 -0
  31. data/spec/cascading_configuration/core/encapsulation_spec.rb +208 -0
  32. data/spec/cascading_configuration/core/instance_controller/extension_module_spec.rb +26 -0
  33. data/spec/cascading_configuration/core/instance_controller/support_module_spec.rb +269 -0
  34. data/spec/cascading_configuration/core/instance_controller_spec.rb +273 -0
  35. data/spec/cascading_configuration/core/module/block_configurations/cascading_variables_spec.rb +17 -0
  36. data/spec/cascading_configuration/core/module/extended_configurations/compositing_objects_spec.rb +127 -0
  37. data/spec/cascading_configuration/core/module/extended_configurations_spec.rb +37 -0
  38. data/spec/cascading_configuration/core/module/inheriting_values_spec.rb +87 -0
  39. data/spec/cascading_configuration/core/module_spec.rb +491 -0
  40. data/spec/cascading_configuration/hash_spec.rb +826 -0
  41. data/spec/cascading_configuration/setting_spec.rb +687 -0
  42. data/spec/cascading_configuration_spec.rb +58 -0
  43. metadata +185 -0
@@ -0,0 +1,840 @@
1
+
2
+ class ::CascadingConfiguration::Core::InstanceController < ::Module
3
+
4
+ include ::ParallelAncestry::Inheritance
5
+
6
+ @instance_controller = { }
7
+
8
+ #####################################
9
+ # self.create_instance_controller #
10
+ #####################################
11
+
12
+ def self.create_instance_controller( instance,
13
+ default_encapsulation_or_name = ::CascadingConfiguration::Core::
14
+ Module::DefaultEncapsulation,
15
+ constant = :Controller,
16
+ extending = false )
17
+
18
+ instance_controller = nil
19
+
20
+ unless instance_controller = @instance_controller[ instance ]
21
+ instance_controller = new( instance, default_encapsulation_or_name, constant, extending )
22
+ end
23
+
24
+ return instance_controller
25
+
26
+ end
27
+
28
+ ##############################
29
+ # self.instance_controller #
30
+ ##############################
31
+
32
+ def self.instance_controller( instance, ensure_exists = false )
33
+
34
+ instance_controller_instance = nil
35
+
36
+ unless instance_controller_instance = @instance_controller[ instance ]
37
+ if ensure_exists
38
+ exception_string = 'No module controller defined for :' << instance.to_s
39
+ exception_string << '.'
40
+ raise ::ArgumentError, exception_string
41
+ end
42
+ end
43
+
44
+ return instance_controller_instance
45
+
46
+ end
47
+
48
+ ######################################
49
+ # self.nearest_instance_controller #
50
+ ######################################
51
+
52
+ def self.nearest_instance_controller( encapsulation, instance, name )
53
+
54
+ instance_controller = nil
55
+
56
+ this_parent = instance
57
+
58
+ begin
59
+
60
+ break if instance_controller = instance_controller( this_parent )
61
+
62
+ end while this_parent = encapsulation.parent_for_configuration( this_parent, name )
63
+
64
+ return instance_controller
65
+
66
+ end
67
+
68
+ ################
69
+ # initialize #
70
+ ################
71
+
72
+ def initialize( instance,
73
+ default_encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation,
74
+ constant = :Controller,
75
+ extending = false )
76
+
77
+ # Call to super is necessary for ParallelAncestry support.
78
+ super()
79
+
80
+ @instance = instance.extend( self )
81
+
82
+ if @instance.is_a?( ::Module )
83
+ @instance.const_set( constant, self )
84
+ unless extending
85
+ initialize_inheritance( @instance )
86
+ end
87
+ else
88
+ hex_id_string = '0x%x' % ( @instance.__id__ << 1 )
89
+ constant = 'ID_' << hex_id_string
90
+ self.class.const_set( constant, self )
91
+ end
92
+
93
+ # We manage reference to self in singleton from here to avoid duplicative efforts.
94
+ reference_to_self = self
95
+ self.class.class_eval do
96
+ @instance_controller[ instance ] = reference_to_self
97
+ end
98
+
99
+ # We need an encapsulation to manage automatic inheritance relations.
100
+ @default_encapsulation = ::CascadingConfiguration::Core::
101
+ Encapsulation.encapsulation( default_encapsulation_or_name )
102
+
103
+ # We also support arbitrary additional encapsulations.
104
+ @encapsulations = { }
105
+
106
+ @cascade_includes = ::UniqueArray.new( self )
107
+ @cascade_extends = ::UniqueArray.new( self )
108
+
109
+ @support_modules = { }
110
+
111
+ @extension_modules = { }
112
+
113
+ end
114
+
115
+ ####################################
116
+ # initialize_inheriting_instance #
117
+ ####################################
118
+
119
+ def initialize_inheriting_instance( parent_instance, instance, for_subclass = false, is_extending = false )
120
+
121
+ super
122
+
123
+ initialize_encapsulation_for_inheriting_instance( @default_encapsulation, parent_instance, instance )
124
+ @encapsulations.each do |this_encapsulation_name, this_encapsulation|
125
+ initialize_encapsulations_for_inheriting_instance( this_encapsulation, parent_instance, instance )
126
+ end
127
+
128
+ # subclass eigenclass inheritance is automatic; re-extending will only mess it up
129
+ unless for_subclass
130
+ unless is_extending or @cascade_extends.empty?
131
+ # We collect cascade extends in accumulating order (oldest => youngest), which means we need to reverse
132
+ # prior to including/extending (we need youngest => oldest).
133
+ instance.extend( *@cascade_extends.reverse )
134
+ end
135
+ unless @cascade_includes.empty?
136
+ if is_extending
137
+ @cascade_includes.each do |this_include|
138
+ unless instance.ancestors.include?( this_include )
139
+ include.extend( this_include )
140
+ end
141
+ end
142
+ elsif instance.is_a?( ::Module )
143
+ cascade_includes = @cascade_includes
144
+ instance.module_eval do
145
+ # We collect cascade includes in accumulating order (oldest => youngest), which means we need to reverse
146
+ # prior to including/extending (we need youngest => oldest).
147
+ include( *cascade_includes.reverse )
148
+ end
149
+ end
150
+ end
151
+ end
152
+
153
+ end
154
+
155
+ ######################################################
156
+ # initialize_encapsulation_for_inheriting_instance #
157
+ ######################################################
158
+
159
+ def initialize_encapsulation_for_inheriting_instance( encapsulation, parent_instance, instance )
160
+
161
+ encapsulation.register_child_for_parent( instance, parent_instance )
162
+
163
+ end
164
+
165
+ ###########################
166
+ # default_encapsulation #
167
+ ###########################
168
+
169
+ attr_reader :default_encapsulation
170
+
171
+ ##############
172
+ # instance #
173
+ ##############
174
+
175
+ attr_reader :instance
176
+
177
+ ######################
178
+ # cascade_includes #
179
+ ######################
180
+
181
+ attr_reader :cascade_includes
182
+
183
+ #####################
184
+ # cascade_extends #
185
+ #####################
186
+
187
+ attr_reader :cascade_extends
188
+
189
+ ###########################
190
+ # add_extension_modules #
191
+ ###########################
192
+
193
+ def add_extension_modules( name,
194
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation,
195
+ *extension_modules,
196
+ & definer_block )
197
+
198
+ encapsulation = ::CascadingConfiguration::Core::Encapsulation.encapsulation( encapsulation_or_name )
199
+
200
+ if block_given?
201
+
202
+ new_module = self.class::ExtensionModule.new( self, encapsulation, name, & definer_block )
203
+
204
+ constant_name = encapsulation.encapsulation_name.to_s.to_camel_case + '_' << name.to_s
205
+ const_set( constant_name, new_module )
206
+
207
+ extension_modules.push( new_module )
208
+
209
+ end
210
+
211
+ extension_modules.reverse!
212
+
213
+ if extension_modules_array = @extension_modules[ name ]
214
+ extension_modules_array.concat( extension_modules )
215
+ else
216
+ @extension_modules[ name ] = extension_modules_array = extension_modules
217
+ end
218
+
219
+ return extension_modules_array
220
+
221
+ end
222
+
223
+ #######################
224
+ # extension_modules #
225
+ #######################
226
+
227
+ def extension_modules( name = nil,
228
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
229
+
230
+ encapsulation = ::CascadingConfiguration::Core::Encapsulation.encapsulation( encapsulation_or_name )
231
+
232
+ extension_modules = nil
233
+
234
+ if name
235
+ extension_modules = @extension_modules[ name ]
236
+ else
237
+ extension_modules = @extension_modules
238
+ end
239
+
240
+ return extension_modules
241
+
242
+ end
243
+
244
+ ##############################
245
+ # extension_modules_upward #
246
+ ##############################
247
+
248
+ def extension_modules_upward( name,
249
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
250
+
251
+ extension_modules = [ ]
252
+
253
+ encapsulation = ::CascadingConfiguration::Core::Encapsulation.encapsulation( encapsulation_or_name )
254
+
255
+ this_ancestor = @instance
256
+
257
+ begin
258
+
259
+ if ancestor_controller = self.class.instance_controller( this_ancestor ) and
260
+ these_modules = ancestor_controller.extension_modules( name, encapsulation )
261
+
262
+ extension_modules.concat( these_modules )
263
+
264
+ end
265
+
266
+ end while this_ancestor = encapsulation.parent_for_configuration( this_ancestor, name )
267
+
268
+ return extension_modules
269
+
270
+ end
271
+
272
+ ####################
273
+ # create_support #
274
+ ####################
275
+
276
+ def create_support( module_type_name,
277
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation,
278
+ support_module_class = ::CascadingConfiguration::Core::InstanceController::SupportModule,
279
+ should_include = false,
280
+ should_extend = false,
281
+ should_cascade_includes = false,
282
+ should_cascade_extends = false,
283
+ module_constant_name = module_type_name.to_s.to_camel_case )
284
+
285
+ encapsulation = ::CascadingConfiguration::Core::Encapsulation.encapsulation( encapsulation_or_name )
286
+
287
+ # permit nil for support_module_class to default
288
+ support_module_class ||= ::CascadingConfiguration::Core::InstanceController::SupportModule
289
+
290
+ unless encapsulation_supports_hash = @support_modules[ encapsulation ]
291
+ encapsulation_supports_hash = { }
292
+ @support_modules[ encapsulation ] = encapsulation_supports_hash
293
+ end
294
+
295
+ unless support_module_instance = encapsulation_supports_hash[ module_type_name ]
296
+
297
+ # New Instance
298
+
299
+ support_module_instance = support_module_class.new( self, encapsulation, module_type_name )
300
+
301
+ const_set( module_constant_name, support_module_instance )
302
+
303
+ encapsulation_supports_hash[ module_type_name ] = support_module_instance
304
+
305
+ # Cascades
306
+
307
+ if should_cascade_includes
308
+ @cascade_includes.push( support_module_instance )
309
+ end
310
+
311
+ if should_cascade_extends
312
+ @cascade_extends.push( support_module_instance )
313
+ end
314
+
315
+ # Includes/Extends
316
+
317
+ if should_include
318
+ case @instance
319
+ # we can only include in modules
320
+ when ::Module
321
+ @instance.module_eval do
322
+ include support_module_instance
323
+ end
324
+ # but we might be told to create instance support on instances, in which case we need to extend
325
+ else
326
+ @instance.extend( support_module_instance )
327
+ end
328
+ end
329
+
330
+ if should_extend
331
+ @instance.extend( support_module_instance )
332
+ end
333
+
334
+ end
335
+
336
+ return support_module_instance
337
+
338
+ end
339
+
340
+ #############
341
+ # support #
342
+ #############
343
+
344
+ def support( module_type_name, encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
345
+
346
+ support_instance = nil
347
+
348
+ encapsulation = ::CascadingConfiguration::Core::Encapsulation.encapsulation( encapsulation_or_name )
349
+
350
+ if encapsulation_supports_hash = @support_modules[ encapsulation ]
351
+ support_instance = encapsulation_supports_hash[ module_type_name ]
352
+ end
353
+
354
+ return support_instance
355
+
356
+ end
357
+
358
+ ##############################
359
+ # create_singleton_support #
360
+ ##############################
361
+
362
+ def create_singleton_support( encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
363
+
364
+ return create_support( :singleton,
365
+ encapsulation_or_name,
366
+ self.class::SupportModule::SingletonSupportModule,
367
+ false,
368
+ true,
369
+ false,
370
+ true )
371
+
372
+ end
373
+
374
+ #######################
375
+ # singleton_support #
376
+ #######################
377
+
378
+ def singleton_support( encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
379
+
380
+ return support( :singleton, encapsulation_or_name )
381
+
382
+ end
383
+
384
+ #############################
385
+ # create_instance_support #
386
+ #############################
387
+
388
+ def create_instance_support( encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
389
+
390
+ return create_support( :instance,
391
+ encapsulation_or_name,
392
+ self.class::SupportModule::InstanceSupportModule,
393
+ true,
394
+ false,
395
+ true,
396
+ false )
397
+
398
+ end
399
+
400
+ ######################
401
+ # instance_support #
402
+ ######################
403
+
404
+ def instance_support( encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
405
+
406
+ return support( :instance, encapsulation_or_name )
407
+
408
+ end
409
+
410
+ ###################################
411
+ # create_local_instance_support #
412
+ ###################################
413
+
414
+ def create_local_instance_support( encapsulation_or_name = ::CascadingConfiguration::Core::
415
+ Module::DefaultEncapsulation )
416
+
417
+ return create_support( :local_instance, encapsulation_or_name, nil, false, true, false, false )
418
+
419
+ end
420
+
421
+ ############################
422
+ # local_instance_support #
423
+ ############################
424
+
425
+ def local_instance_support( encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
426
+
427
+ return support( :local_instance, encapsulation_or_name )
428
+
429
+ end
430
+
431
+ ##################################
432
+ # define_configuration_methods #
433
+ ##################################
434
+
435
+ def define_configuration_methods( ccm, encapsulation_or_name, method_types, names, & definer_block )
436
+
437
+ encapsulation = ::CascadingConfiguration::Core::Encapsulation.encapsulation( encapsulation_or_name )
438
+
439
+ accessors = parse_names_for_accessors( *names )
440
+
441
+ accessors.each do |this_name, this_write_name|
442
+ define_getter( ccm, this_name, this_name, method_types, encapsulation )
443
+ define_setter( ccm, this_name, this_write_name, method_types, encapsulation )
444
+ end
445
+
446
+ return accessors
447
+
448
+ end
449
+
450
+ #############################
451
+ # define_singleton_method #
452
+ #############################
453
+
454
+ def define_singleton_method( name,
455
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation,
456
+ & method_proc )
457
+
458
+ return create_singleton_support( encapsulation_or_name ).define_method( name, & method_proc )
459
+
460
+ end
461
+
462
+ #########################
463
+ # alias_module_method #
464
+ #########################
465
+
466
+ def alias_module_method( alias_name,
467
+ name,
468
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
469
+
470
+ aliased_method = false
471
+
472
+ if singleton_support = singleton_support( encapsulation_or_name )
473
+ aliased_method = singleton_support.alias_method( alias_name, name )
474
+ end
475
+
476
+ return aliased_method
477
+
478
+ end
479
+
480
+ ##########################
481
+ # remove_module_method #
482
+ ##########################
483
+
484
+ def remove_module_method( name, encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
485
+
486
+ removed_method = false
487
+
488
+ if singleton_support = singleton_support( encapsulation_or_name )
489
+ removed_method = singleton_support.remove_method( name )
490
+ end
491
+
492
+ return removed_method
493
+
494
+ end
495
+
496
+ #########################
497
+ # undef_module_method #
498
+ #########################
499
+
500
+ def undef_module_method( name, encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
501
+
502
+ undefined_method = false
503
+
504
+ if singleton_support = singleton_support( encapsulation_or_name )
505
+ undefined_method = singleton_support.undef_method( name )
506
+ end
507
+
508
+ return undefined_method
509
+
510
+ end
511
+
512
+ ############################
513
+ # define_instance_method #
514
+ ############################
515
+
516
+ def define_instance_method( name,
517
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation,
518
+ & method_proc )
519
+
520
+ return create_instance_support( encapsulation_or_name ).define_method( name, & method_proc )
521
+
522
+ end
523
+
524
+ ############################
525
+ # remove_instance_method #
526
+ ############################
527
+
528
+ def remove_instance_method( name,
529
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
530
+
531
+ removed_method = false
532
+
533
+ if instance_support = instance_support( encapsulation_or_name )
534
+ removed_method = instance_support.remove_method( name )
535
+ end
536
+
537
+ return removed_method
538
+
539
+ end
540
+
541
+ ###########################
542
+ # undef_instance_method #
543
+ ###########################
544
+
545
+ def undef_instance_method( name,
546
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
547
+
548
+ undefined_method = false
549
+
550
+ if instance_support = instance_support( encapsulation_or_name )
551
+ undefined_method = instance_support.undef_method( name )
552
+ end
553
+
554
+ return undefined_method
555
+
556
+ end
557
+
558
+ #######################################
559
+ # define_instance_method_if_support #
560
+ #######################################
561
+
562
+ def define_instance_method_if_support( name,
563
+ encapsulation_or_name = ::CascadingConfiguration::Core::
564
+ Module::DefaultEncapsulation,
565
+ & method_proc )
566
+
567
+ if instance_support = instance_support( encapsulation_or_name )
568
+ instance_support.define_method( name, & method_proc )
569
+ end
570
+
571
+ return self
572
+
573
+ end
574
+
575
+ ###########################
576
+ # alias_instance_method #
577
+ ###########################
578
+
579
+ def alias_instance_method( alias_name,
580
+ name,
581
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
582
+
583
+ aliased_method = false
584
+
585
+ if instance_support = instance_support( encapsulation_or_name )
586
+ aliased_method = instance_support.alias_method( alias_name, name )
587
+ end
588
+
589
+ return aliased_method
590
+
591
+ end
592
+
593
+ ##################################
594
+ # define_local_instance_method #
595
+ ##################################
596
+
597
+ def define_local_instance_method( name,
598
+ encapsulation_or_name = ::CascadingConfiguration::Core::
599
+ Module::DefaultEncapsulation,
600
+ & method_proc )
601
+
602
+ return create_local_instance_support( encapsulation_or_name ).define_method( name, & method_proc )
603
+
604
+ end
605
+
606
+ #################################
607
+ # alias_local_instance_method #
608
+ #################################
609
+
610
+ def alias_local_instance_method( alias_name,
611
+ name,
612
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
613
+
614
+ aliased_method = false
615
+
616
+ if local_instance_support = local_instance_support( encapsulation_or_name )
617
+ aliased_method = local_instance_support.alias_method( alias_name, name, encapsulation_or_name )
618
+ end
619
+
620
+ return aliased_method
621
+
622
+ end
623
+
624
+ ##################################
625
+ # remove_local_instance_method #
626
+ ##################################
627
+
628
+ def remove_local_instance_method( name,
629
+ encapsulation_or_name = ::CascadingConfiguration::Core::
630
+ Module::DefaultEncapsulation )
631
+
632
+ removed_method = false
633
+
634
+ if local_instance_support = local_instance_support( encapsulation_or_name )
635
+ removed_method = local_instance_support.remove_method( name )
636
+ end
637
+
638
+ return removed_method
639
+
640
+ end
641
+
642
+ #################################
643
+ # undef_local_instance_method #
644
+ #################################
645
+
646
+ def undef_local_instance_method( name,
647
+ encapsulation_or_name = ::CascadingConfiguration::Core::
648
+ Module::DefaultEncapsulation )
649
+
650
+ undefined_method = false
651
+
652
+ if local_instance_support = local_instance_support( encapsulation_or_name )
653
+ undefined_method = local_instance_support.undef_method( name )
654
+ end
655
+
656
+ return undefined_method
657
+
658
+ end
659
+
660
+ ###########################################
661
+ # define_singleton_and_instance_methods #
662
+ ###########################################
663
+
664
+ def define_singleton_and_instance_methods( name, encapsulation_or_name, & method_proc )
665
+
666
+ define_singleton_method( name, encapsulation_or_name, & method_proc )
667
+ define_instance_method( name, encapsulation_or_name, & method_proc )
668
+
669
+ end
670
+
671
+ ############################################################
672
+ # define_singleton_method_and_instance_method_if_support #
673
+ ############################################################
674
+
675
+ def define_singleton_method_and_instance_method_if_support( name, encapsulation_or_name, & method_proc )
676
+
677
+ define_singleton_method( name, encapsulation_or_name, & method_proc )
678
+ define_instance_method_if_support( name, encapsulation_or_name, & method_proc )
679
+
680
+ end
681
+
682
+ #######################################
683
+ # alias_module_and_instance_methods #
684
+ #######################################
685
+
686
+ def alias_module_and_instance_methods( alias_name,
687
+ name,
688
+ encapsulation_or_name = ::CascadingConfiguration::Core::
689
+ Module::DefaultEncapsulation )
690
+
691
+ alias_module_method( alias_name, name, encapsulation_or_name )
692
+ alias_instance_method( alias_name, name, encapsulation_or_name )
693
+
694
+ end
695
+
696
+ ##################################################################################################
697
+ private ######################################################################################
698
+ ##################################################################################################
699
+
700
+ ###################
701
+ # define_getter #
702
+ ###################
703
+
704
+ def define_getter( ccm,
705
+ name,
706
+ accessor_name,
707
+ method_types,
708
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
709
+
710
+ encapsulation = ::CascadingConfiguration::Core::Encapsulation.encapsulation( encapsulation_or_name )
711
+
712
+ module_proc = ::Proc.new do
713
+ return ccm.getter( encapsulation, self, name )
714
+ end
715
+
716
+ instance_proc = ::Proc.new do
717
+ return ccm.instance_getter( encapsulation, self, name )
718
+ end
719
+
720
+ define_accessor( accessor_name, module_proc, instance_proc, method_types, encapsulation )
721
+
722
+ return self
723
+
724
+ end
725
+
726
+ ###################
727
+ # define_setter #
728
+ ###################
729
+
730
+ def define_setter( ccm,
731
+ name,
732
+ accessor_name,
733
+ method_types,
734
+ encapsulation_or_name = ::CascadingConfiguration::Core::Module::DefaultEncapsulation )
735
+
736
+ encapsulation = ::CascadingConfiguration::Core::Encapsulation.encapsulation( encapsulation_or_name )
737
+
738
+ module_proc = ::Proc.new do |value|
739
+ return ccm.setter( encapsulation, self, name, value )
740
+ end
741
+
742
+ instance_proc = ::Proc.new do |value|
743
+ return ccm.instance_setter( encapsulation, self, name, value )
744
+ end
745
+
746
+ define_accessor( accessor_name, module_proc, instance_proc, method_types, encapsulation )
747
+
748
+ return self
749
+
750
+ end
751
+
752
+ ###############################
753
+ # parse_names_for_accessors #
754
+ ###############################
755
+
756
+ def parse_names_for_accessors( *names )
757
+
758
+ accessors = { }
759
+
760
+ names.each do |this_name|
761
+
762
+ case this_name
763
+
764
+ when ::Hash
765
+
766
+ this_name.each do |this_accessor_name, this_write_accessor_name|
767
+ accessors[ this_accessor_name ] = this_write_accessor_name
768
+ end
769
+
770
+ else
771
+
772
+ accessors[ this_name.accessor_name ] = this_name.write_accessor_name
773
+
774
+ end
775
+
776
+ end
777
+
778
+ return accessors
779
+
780
+ end
781
+
782
+ #####################
783
+ # define_accessor #
784
+ #####################
785
+
786
+ def define_accessor( accessor_name, module_proc, instance_proc, method_types, encapsulation )
787
+
788
+ # Procs have already been defined by this point - they require the encapsulation,
789
+ # so we require encapsulation here, expecting it already to have been looked up.
790
+
791
+ method_types.each do |this_method_type|
792
+
793
+ case this_method_type
794
+
795
+ # Cascades through all includes, module and instance methods
796
+ when :all
797
+
798
+ define_singleton_method( accessor_name, encapsulation, & module_proc )
799
+ define_instance_method_if_support( accessor_name, encapsulation, & instance_proc )
800
+
801
+ # Module methods only
802
+ when :module, :class
803
+
804
+ define_singleton_method( accessor_name, encapsulation, & module_proc )
805
+
806
+ # Instance methods only
807
+ when :instance
808
+
809
+ define_instance_method( accessor_name, encapsulation, & instance_proc )
810
+
811
+ # Methods local to this instance and instances of it only
812
+ when :local_instance
813
+
814
+ case @instance
815
+ when ::Module
816
+ define_local_instance_method( accessor_name, encapsulation, & module_proc )
817
+ else
818
+ define_local_instance_method( accessor_name, encapsulation, & instance_proc )
819
+ end
820
+ define_instance_method_if_support( accessor_name, encapsulation, & instance_proc )
821
+
822
+ # Methods local to this instance only
823
+ when :object
824
+
825
+ case @instance
826
+ when ::Module
827
+ define_local_instance_method( accessor_name, encapsulation, & module_proc )
828
+ else
829
+ define_local_instance_method( accessor_name, encapsulation, & instance_proc )
830
+ end
831
+
832
+ end
833
+
834
+ end
835
+
836
+ return self
837
+
838
+ end
839
+
840
+ end