compositing-array-unique 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+
2
+ == 3/17/12
3
+
4
+ Initial release.
5
+
6
+ == 3/18/12
7
+
8
+ Added hooks for subclassing.
9
+
10
+ == 3/24/12
11
+
12
+ Added :include? implementation using @unique_keys instead of linear search.
13
+
14
+ == 6/1/12
15
+
16
+ Added :configuration_instance parameter to :initialize.
@@ -0,0 +1,70 @@
1
+ # Unique Compositing Array #
2
+
3
+ http://rubygems.org/gems/compositing-array-unique
4
+
5
+ # Description #
6
+
7
+ Provides CompositingArray::Unique.
8
+
9
+ # Summary #
10
+
11
+ An implementation of Array that permits chaining, where children inherit changes to parent and where parent settings can be overridden in children, and that ensures inserted values are unique.
12
+
13
+ # Install #
14
+
15
+ * sudo gem install compositing-array-unique
16
+
17
+ # Usage #
18
+
19
+ ```ruby
20
+ compositing_array = CompositingArray::Unique.new
21
+ sub_compositing_array = CompositingArray::Unique.new( compositing_array )
22
+
23
+ compositing_array.push( :A )
24
+ # compositing_array
25
+ # => [ :A ]
26
+ # sub_compositing_array
27
+ # => [ :A ]
28
+ compositing_array.push( :A )
29
+ # compositing_array
30
+ # => [ :A ]
31
+ # sub_compositing_array
32
+ # => [ :A ]
33
+
34
+ compositing_array.delete_at( 0 )
35
+ # compositing_array
36
+ # => [ ]
37
+ # sub_compositing_array
38
+ # => [ ]
39
+
40
+ sub_compositing_array.push( :A )
41
+ # compositing_array
42
+ # => [ ]
43
+ # sub_compositing_array
44
+ # => [ :A ]
45
+ ```
46
+
47
+ # License #
48
+
49
+ (The MIT License)
50
+
51
+ Copyright (c) Asher
52
+
53
+ Permission is hereby granted, free of charge, to any person obtaining
54
+ a copy of this software and associated documentation files (the
55
+ 'Software'), to deal in the Software without restriction, including
56
+ without limitation the rights to use, copy, modify, merge, publish,
57
+ distribute, sublicense, and/or sell copies of the Software, and to
58
+ permit persons to whom the Software is furnished to do so, subject to
59
+ the following conditions:
60
+
61
+ The above copyright notice and this permission notice shall be
62
+ included in all copies or substantial portions of the Software.
63
+
64
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
65
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
66
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
67
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
68
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
69
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
70
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
File without changes
@@ -0,0 +1,27 @@
1
+
2
+ require 'unique-array'
3
+
4
+ if $__compositing_array__spec__development
5
+ require_relative '../../compositing-array/lib/compositing-array.rb'
6
+ else
7
+ require 'compositing-array'
8
+ end
9
+
10
+ class ::CompositingArray
11
+ end
12
+ class ::CompositingArray::Unique < ::CompositingArray
13
+ end
14
+
15
+ basepath = 'compositing-array-unique/CompositingArray/Unique'
16
+
17
+ files = [
18
+
19
+ 'Interface'
20
+
21
+ ]
22
+
23
+ files.each do |this_file|
24
+ require_relative( File.join( basepath, this_file ) + '.rb' )
25
+ end
26
+
27
+ require_relative( basepath + '.rb' )
@@ -0,0 +1,6 @@
1
+
2
+ class ::CompositingArray::Unique < ::CompositingArray
3
+
4
+ include ::CompositingArray::Unique::Interface
5
+
6
+ end
@@ -0,0 +1,48 @@
1
+
2
+ module ::CompositingArray::Unique::Interface
3
+
4
+ include ::UniqueArray::Interface
5
+
6
+ instances_identify_as!( ::CompositingArray::Unique )
7
+
8
+ ##################################################################################################
9
+ private ######################################################################################
10
+ ##################################################################################################
11
+
12
+ ########################################
13
+ # update_as_sub_array_for_parent_set #
14
+ ########################################
15
+
16
+ def update_as_sub_array_for_parent_set( index, object )
17
+
18
+ super unless @unique_keys.has_key?( object )
19
+
20
+ end
21
+
22
+ ###########################################
23
+ # update_as_sub_array_for_parent_insert #
24
+ ###########################################
25
+
26
+ def update_as_sub_array_for_parent_insert( index, *objects )
27
+
28
+ # new parent indexes have been inserted at index in parent
29
+
30
+ # get rid of objects already inserted
31
+ indexes_to_delete = [ ]
32
+ objects.each_with_index do |this_object, index|
33
+ if @unique_keys.has_key?( this_object )
34
+ indexes_to_delete.push( index )
35
+ else
36
+ @unique_keys[ this_object ] = true
37
+ end
38
+ end
39
+ indexes_to_delete.sort.reverse.each do |this_index|
40
+ objects.delete_at( this_index )
41
+ end
42
+ return false if objects.empty?
43
+
44
+ super
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,1229 @@
1
+
2
+ if $__compositing_array__spec__development
3
+ require_relative '../../lib/compositing-array-unique.rb'
4
+ else
5
+ require 'compositing-array-unique'
6
+ end
7
+
8
+ describe ::CompositingArray::Unique do
9
+
10
+ ################
11
+ # initialize #
12
+ ################
13
+
14
+ it 'can add initialize with an ancestor, inheriting its values and linking to it as a child' do
15
+
16
+ cascading_composite_array = ::CompositingArray::Unique.new
17
+
18
+ cascading_composite_array.instance_variable_get( :@parent_composite_object ).should == nil
19
+ cascading_composite_array.should == []
20
+ cascading_composite_array.push( :A, :B, :C, :D )
21
+
22
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
23
+ sub_cascading_composite_array.instance_variable_get( :@parent_composite_object ).should == cascading_composite_array
24
+ sub_cascading_composite_array.should == [ :A, :B, :C, :D ]
25
+
26
+ end
27
+
28
+ ##################################################################################################
29
+ # private #####################################################################################
30
+ ##################################################################################################
31
+
32
+ ##################################################
33
+ # update_corresponding_index_for_parent_change #
34
+ ##################################################
35
+
36
+ it 'can update tracked parent indices for parent insert/delete' do
37
+
38
+ cascading_composite_array = ::CompositingArray::Unique.new
39
+ cascading_composite_array.push( :A, :B, :B, :B, :A )
40
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
41
+ sub_cascading_composite_array.instance_eval do
42
+ @local_index_for_parent_index[ 0 ].should == 0
43
+ @local_index_for_parent_index[ 1 ].should == 1
44
+ @local_index_for_parent_index[ 2 ].should == nil
45
+ # insert 1 in parent before parent-1
46
+ update_corresponding_index_for_parent_change( 1, 1 )
47
+ @local_index_for_parent_index[ 0 ].should == 0
48
+ # no longer a parent-1 index (has to be set separately)
49
+ @local_index_for_parent_index[ 1 ].should == nil
50
+ # parent-1 is now parent-2
51
+ @local_index_for_parent_index[ 2 ].should == 2
52
+ @parent_and_interpolated_object_count.should == 3
53
+ end
54
+
55
+ end
56
+
57
+ #################################################
58
+ # update_corresponding_index_for_local_change #
59
+ #################################################
60
+
61
+ it 'can update tracked parent indices for local insert/delete' do
62
+
63
+ cascading_composite_array = ::CompositingArray::Unique.new
64
+ cascading_composite_array.push( :A, :B )
65
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
66
+
67
+ sub_cascading_composite_array.instance_eval do
68
+ @local_index_for_parent_index[ 0 ].should == 0
69
+ @local_index_for_parent_index[ 1 ].should == 1
70
+ @local_index_for_parent_index[ 2 ].should == nil
71
+ # insert 1 before parent-1
72
+ update_corresponding_index_for_local_change( 1, 1 )
73
+ @local_index_for_parent_index[ 0 ].should == 0
74
+ # new index for parent-1 is 2
75
+ @local_index_for_parent_index[ 1 ].should == 2
76
+ @local_index_for_parent_index[ 2 ].should == nil
77
+ @parent_and_interpolated_object_count.should == 3
78
+ end
79
+
80
+ end
81
+
82
+ ###########################################
83
+ # update_as_sub_array_for_parent_insert #
84
+ ###########################################
85
+
86
+ it 'can handle updating itself as a sub-array when told an insert has occurred in parent' do
87
+
88
+ cascading_composite_array = ::CompositingArray::Unique.new
89
+ cascading_composite_array.push( :A, :B )
90
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
91
+
92
+ sub_cascading_composite_array.instance_eval do
93
+ @local_index_for_parent_index[ 0 ].should == 0
94
+ @local_index_for_parent_index[ 1 ].should == 1
95
+ @local_index_for_parent_index[ 2 ].should == nil
96
+ # insert 1 before parent-1
97
+ update_as_sub_array_for_parent_insert( 1, :C )
98
+ @local_index_for_parent_index[ 0 ].should == 0
99
+ # new parent index parent-1 inserted for :C
100
+ @local_index_for_parent_index[ 1 ].should == 1
101
+ # new index for parent-1 is parent-2
102
+ @local_index_for_parent_index[ 2 ].should == 2
103
+ @parent_and_interpolated_object_count.should == 3
104
+ end
105
+
106
+ end
107
+
108
+ ########################################
109
+ # update_as_sub_array_for_parent_set #
110
+ ########################################
111
+
112
+ it 'can handle updating itself as a sub-array when told a set has occurred in parent' do
113
+
114
+ cascading_composite_array = ::CompositingArray::Unique.new
115
+ cascading_composite_array.push( :A, :B )
116
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
117
+
118
+ sub_cascading_composite_array.instance_eval do
119
+ @local_index_for_parent_index[ 0 ].should == 0
120
+ @local_index_for_parent_index[ 1 ].should == 1
121
+ @local_index_for_parent_index[ 2 ].should == nil
122
+ # set for parent-1
123
+ update_as_sub_array_for_parent_set( 1, :C )
124
+ @local_index_for_parent_index[ 0 ].should == 0
125
+ @local_index_for_parent_index[ 1 ].should == 1
126
+ @local_index_for_parent_index[ 2 ].should == nil
127
+ @parent_and_interpolated_object_count.should == 2
128
+ end
129
+
130
+ end
131
+
132
+ ###########################################
133
+ # update_as_sub_array_for_parent_delete #
134
+ ###########################################
135
+
136
+ it 'can handle updating itself as a sub-array when told a delete has occurred in parent' do
137
+
138
+ cascading_composite_array = ::CompositingArray::Unique.new
139
+ cascading_composite_array.push( :A, :B )
140
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
141
+
142
+ sub_cascading_composite_array.instance_eval do
143
+ @local_index_for_parent_index[ 0 ].should == 0
144
+ @local_index_for_parent_index[ 1 ].should == 1
145
+ @local_index_for_parent_index[ 2 ].should == nil
146
+ # delete parent-1
147
+ update_as_sub_array_for_parent_delete( 1 )
148
+ @local_index_for_parent_index[ 0 ].should == 0
149
+ @local_index_for_parent_index[ 1 ].should == 1
150
+ @local_index_for_parent_index[ 2 ].should == nil
151
+ @parent_and_interpolated_object_count.should == 1
152
+ end
153
+
154
+ end
155
+
156
+ ##################################################################################################
157
+ # public ######################################################################################
158
+ ##################################################################################################
159
+
160
+ #########
161
+ # []= #
162
+ #########
163
+
164
+ it 'can add elements' do
165
+
166
+ cascading_composite_array = ::CompositingArray::Unique.new
167
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
168
+
169
+ cascading_composite_array[ 0 ] = :A
170
+ cascading_composite_array.should == [ :A ]
171
+ sub_cascading_composite_array.should == [ :A ]
172
+
173
+ cascading_composite_array[ 1 ] = :B
174
+ cascading_composite_array.should == [ :A, :B ]
175
+ sub_cascading_composite_array.should == [ :A, :B ]
176
+
177
+ sub_cascading_composite_array[ 0 ] = :C
178
+ cascading_composite_array.should == [ :A, :B ]
179
+ sub_cascading_composite_array.should == [ :C, :B ]
180
+
181
+ sub_cascading_composite_array[ 0 ] = :B
182
+ cascading_composite_array.should == [ :A, :B ]
183
+ sub_cascading_composite_array.should == [ :C, :B ]
184
+
185
+ sub_cascading_composite_array[ 2 ] = :C
186
+ cascading_composite_array.should == [ :A, :B ]
187
+ sub_cascading_composite_array.should == [ :C, :B ]
188
+
189
+ cascading_composite_array[ 2 ] = :D
190
+ cascading_composite_array.should == [ :A, :B, :D ]
191
+ sub_cascading_composite_array.should == [ :C, :B, :D ]
192
+
193
+ end
194
+
195
+ ############
196
+ # insert #
197
+ ############
198
+
199
+ it 'can insert elements' do
200
+
201
+ cascading_composite_array = ::CompositingArray::Unique.new
202
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
203
+
204
+ cascading_composite_array.insert( 3, :D )
205
+ cascading_composite_array.should == [ nil, :D ]
206
+ sub_cascading_composite_array.should == [ nil, :D ]
207
+
208
+ cascading_composite_array.insert( 1, :B )
209
+ cascading_composite_array.should == [ nil, :B, :D ]
210
+ sub_cascading_composite_array.should == [ nil, :B, :D ]
211
+
212
+ cascading_composite_array.insert( 2, :C )
213
+ cascading_composite_array.should == [ nil, :B, :C, :D ]
214
+ sub_cascading_composite_array.should == [ nil, :B, :C, :D ]
215
+
216
+ sub_cascading_composite_array.insert( 0, :E )
217
+ cascading_composite_array.should == [ nil, :B, :C, :D ]
218
+ sub_cascading_composite_array.should == [ :E, nil, :B, :C, :D ]
219
+
220
+ sub_cascading_composite_array.insert( 4, :F )
221
+ cascading_composite_array.should == [ nil, :B, :C, :D ]
222
+ sub_cascading_composite_array.should == [ :E, nil, :B, :C, :F, :D ]
223
+
224
+ end
225
+
226
+ ##########
227
+ # push #
228
+ # << #
229
+ ##########
230
+
231
+ it 'can add elements' do
232
+
233
+ cascading_composite_array = ::CompositingArray::Unique.new
234
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
235
+
236
+ cascading_composite_array << :A
237
+ cascading_composite_array.should == [ :A ]
238
+ sub_cascading_composite_array.should == [ :A ]
239
+
240
+ cascading_composite_array << :B
241
+ cascading_composite_array.should == [ :A, :B ]
242
+ sub_cascading_composite_array.should == [ :A, :B ]
243
+
244
+ sub_cascading_composite_array << :C
245
+ cascading_composite_array.should == [ :A, :B ]
246
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
247
+
248
+ sub_cascading_composite_array << :B
249
+ cascading_composite_array.should == [ :A, :B ]
250
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
251
+
252
+ end
253
+
254
+ ############
255
+ # concat #
256
+ # + #
257
+ ############
258
+
259
+ it 'can add elements' do
260
+
261
+ # NOTE: this breaks + by causing it to modify the array like +=
262
+ # The alternative was worse.
263
+
264
+ cascading_composite_array = ::CompositingArray::Unique.new
265
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
266
+
267
+ cascading_composite_array.concat( [ :A ] )
268
+ cascading_composite_array.should == [ :A ]
269
+ sub_cascading_composite_array.should == [ :A ]
270
+
271
+ cascading_composite_array += [ :B ]
272
+ cascading_composite_array.should == [ :A, :B ]
273
+ sub_cascading_composite_array.should == [ :A, :B ]
274
+
275
+ sub_cascading_composite_array.concat( [ :C ] )
276
+ cascading_composite_array.should == [ :A, :B ]
277
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
278
+
279
+ sub_cascading_composite_array += [ :B ]
280
+ cascading_composite_array.should == [ :A, :B ]
281
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
282
+
283
+ end
284
+
285
+ ####################
286
+ # delete_objects #
287
+ ####################
288
+
289
+ it 'can delete multiple elements' do
290
+
291
+ cascading_composite_array = ::CompositingArray::Unique.new
292
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
293
+
294
+ cascading_composite_array += [ :A, :B ]
295
+ cascading_composite_array.should == [ :A, :B ]
296
+ sub_cascading_composite_array.should == [ :A, :B ]
297
+
298
+ cascading_composite_array.delete_objects( :A, :B )
299
+ cascading_composite_array.should == [ ]
300
+ sub_cascading_composite_array.should == [ ]
301
+
302
+ sub_cascading_composite_array += [ :B, :C, :D ]
303
+ cascading_composite_array.should == [ ]
304
+ sub_cascading_composite_array.should == [ :B, :C, :D ]
305
+
306
+ sub_cascading_composite_array.delete_objects( :C, :B )
307
+ cascading_composite_array.should == [ ]
308
+ sub_cascading_composite_array.should == [ :D ]
309
+
310
+ end
311
+
312
+ #######
313
+ # - #
314
+ #######
315
+
316
+ it 'can exclude elements' do
317
+
318
+ cascading_composite_array = ::CompositingArray::Unique.new
319
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
320
+
321
+ cascading_composite_array.push( :A )
322
+ cascading_composite_array.should == [ :A ]
323
+ sub_cascading_composite_array.should == [ :A ]
324
+
325
+ cascading_composite_array -= [ :A ]
326
+ cascading_composite_array.should == [ ]
327
+ sub_cascading_composite_array.should == [ ]
328
+
329
+ cascading_composite_array.push( :B )
330
+ cascading_composite_array.should == [ :B ]
331
+ sub_cascading_composite_array.should == [ :B ]
332
+
333
+ sub_cascading_composite_array.push( :C )
334
+ cascading_composite_array.should == [ :B ]
335
+ sub_cascading_composite_array.should == [ :B, :C ]
336
+
337
+ sub_cascading_composite_array -= [ :B ]
338
+ cascading_composite_array.should == [ :B ]
339
+ sub_cascading_composite_array.should == [ :C ]
340
+
341
+ end
342
+
343
+ ############
344
+ # delete #
345
+ ############
346
+
347
+ it 'can delete elements' do
348
+
349
+ cascading_composite_array = ::CompositingArray::Unique.new
350
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
351
+
352
+ cascading_composite_array.push( :A )
353
+ cascading_composite_array.should == [ :A ]
354
+ sub_cascading_composite_array.should == [ :A ]
355
+
356
+ cascading_composite_array.delete( :A )
357
+ cascading_composite_array.should == [ ]
358
+ sub_cascading_composite_array.should == [ ]
359
+
360
+ cascading_composite_array.push( :B )
361
+ cascading_composite_array.should == [ :B ]
362
+ sub_cascading_composite_array.should == [ :B ]
363
+
364
+ sub_cascading_composite_array.push( :C )
365
+ cascading_composite_array.should == [ :B ]
366
+ sub_cascading_composite_array.should == [ :B, :C ]
367
+
368
+ sub_cascading_composite_array.delete( :B )
369
+ cascading_composite_array.should == [ :B ]
370
+ sub_cascading_composite_array.should == [ :C ]
371
+
372
+ end
373
+
374
+ ###############
375
+ # delete_at #
376
+ ###############
377
+
378
+ it 'can delete by indexes' do
379
+
380
+ cascading_composite_array = ::CompositingArray::Unique.new
381
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
382
+
383
+ cascading_composite_array.push( :A )
384
+ cascading_composite_array.should == [ :A ]
385
+ sub_cascading_composite_array.should == [ :A ]
386
+
387
+ cascading_composite_array.delete_at( 0 )
388
+ cascading_composite_array.should == [ ]
389
+ sub_cascading_composite_array.should == [ ]
390
+
391
+ cascading_composite_array.push( :B )
392
+ cascading_composite_array.should == [ :B ]
393
+ sub_cascading_composite_array.should == [ :B ]
394
+
395
+ sub_cascading_composite_array.push( :C )
396
+ cascading_composite_array.should == [ :B ]
397
+ sub_cascading_composite_array.should == [ :B, :C ]
398
+
399
+ sub_cascading_composite_array.delete_at( 0 )
400
+ cascading_composite_array.should == [ :B ]
401
+ sub_cascading_composite_array.should == [ :C ]
402
+
403
+ end
404
+
405
+ #######################
406
+ # delete_at_indexes #
407
+ #######################
408
+
409
+ it 'can delete by indexes' do
410
+
411
+ cascading_composite_array = ::CompositingArray::Unique.new
412
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
413
+
414
+ cascading_composite_array.push( :A, :B, :C )
415
+ cascading_composite_array.should == [ :A, :B, :C ]
416
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
417
+
418
+ cascading_composite_array.delete_at_indexes( 0, 1 )
419
+ cascading_composite_array.should == [ :C ]
420
+ sub_cascading_composite_array.should == [ :C ]
421
+
422
+ sub_cascading_composite_array.push( :C, :B )
423
+ cascading_composite_array.should == [ :C ]
424
+ sub_cascading_composite_array.should == [ :C, :B ]
425
+
426
+ sub_cascading_composite_array.delete_at_indexes( 0, 1, 2 )
427
+ cascading_composite_array.should == [ :C ]
428
+ sub_cascading_composite_array.should == [ ]
429
+
430
+ end
431
+
432
+ ###############
433
+ # delete_if #
434
+ ###############
435
+
436
+ it 'can delete by block' do
437
+
438
+ cascading_composite_array = ::CompositingArray::Unique.new
439
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
440
+
441
+ cascading_composite_array.push( :A, :B, :C )
442
+ cascading_composite_array.should == [ :A, :B, :C ]
443
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
444
+ cascading_composite_array.delete_if do |object|
445
+ object != :C
446
+ end
447
+ cascading_composite_array.should == [ :C ]
448
+ sub_cascading_composite_array.should == [ :C ]
449
+
450
+ sub_cascading_composite_array.push( :C, :B )
451
+ cascading_composite_array.should == [ :C ]
452
+ sub_cascading_composite_array.should == [ :C, :B ]
453
+ sub_cascading_composite_array.delete_if do |object|
454
+ object != nil
455
+ end
456
+ sub_cascading_composite_array.should == [ ]
457
+ cascading_composite_array.should == [ :C ]
458
+
459
+ cascading_composite_array.delete_if.is_a?( Enumerator ).should == true
460
+
461
+ end
462
+
463
+ #############
464
+ # keep_if #
465
+ #############
466
+
467
+ it 'can keep by block' do
468
+
469
+ cascading_composite_array = ::CompositingArray::Unique.new
470
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
471
+
472
+ cascading_composite_array.push( :A, :B, :C )
473
+ cascading_composite_array.should == [ :A, :B, :C ]
474
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
475
+ cascading_composite_array.keep_if do |object|
476
+ object == :C
477
+ end
478
+ cascading_composite_array.should == [ :C ]
479
+ sub_cascading_composite_array.should == [ :C ]
480
+
481
+ sub_cascading_composite_array.push( :C, :B )
482
+ cascading_composite_array.should == [ :C ]
483
+ sub_cascading_composite_array.should == [ :C, :B ]
484
+ sub_cascading_composite_array.keep_if do |object|
485
+ object == nil
486
+ end
487
+ cascading_composite_array.should == [ :C ]
488
+ sub_cascading_composite_array.should == [ ]
489
+
490
+ end
491
+
492
+ ##############
493
+ # compact! #
494
+ ##############
495
+
496
+ it 'can compact' do
497
+
498
+ cascading_composite_array = ::CompositingArray::Unique.new
499
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
500
+
501
+ cascading_composite_array.push( :A, nil, :B, nil, :C, nil )
502
+ cascading_composite_array.should == [ :A, nil, :B, :C ]
503
+ sub_cascading_composite_array.should == [ :A, nil, :B, :C ]
504
+ cascading_composite_array.compact!
505
+ cascading_composite_array.should == [ :A, :B, :C ]
506
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
507
+
508
+ sub_cascading_composite_array.push( nil, nil, :D )
509
+ cascading_composite_array.should == [ :A, :B, :C ]
510
+ sub_cascading_composite_array.should == [ :A, :B, :C, nil, :D ]
511
+ sub_cascading_composite_array.compact!
512
+ cascading_composite_array.should == [ :A, :B, :C ]
513
+ sub_cascading_composite_array.should == [ :A, :B, :C, :D ]
514
+
515
+ end
516
+
517
+ ##############
518
+ # flatten! #
519
+ ##############
520
+
521
+ it 'can flatten' do
522
+
523
+ cascading_composite_array = ::CompositingArray::Unique.new
524
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
525
+
526
+ cascading_composite_array.push( :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] )
527
+ cascading_composite_array.should == [ :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] ]
528
+ sub_cascading_composite_array.should == [ :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] ]
529
+ cascading_composite_array.flatten!
530
+ cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E ]
531
+ sub_cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E ]
532
+
533
+ sub_cascading_composite_array.push( [ :F_F, :F_G ], :D, [ :F_H ] )
534
+ cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E ]
535
+ sub_cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E, [ :F_F, :F_G ], :D, [ :F_H ] ]
536
+ sub_cascading_composite_array.flatten!
537
+ cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E ]
538
+ sub_cascading_composite_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E, :F_F, :F_G, :D, :F_H ]
539
+
540
+ end
541
+
542
+ #############
543
+ # reject! #
544
+ #############
545
+
546
+ it 'can reject' do
547
+
548
+ cascading_composite_array = ::CompositingArray::Unique.new
549
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
550
+
551
+ cascading_composite_array.push( :A, :B, :C )
552
+ cascading_composite_array.should == [ :A, :B, :C ]
553
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
554
+ cascading_composite_array.reject! do |object|
555
+ object != :C
556
+ end
557
+ cascading_composite_array.should == [ :C ]
558
+ sub_cascading_composite_array.should == [ :C ]
559
+
560
+ sub_cascading_composite_array.push( :C, :B )
561
+ cascading_composite_array.should == [ :C ]
562
+ sub_cascading_composite_array.should == [ :C, :B ]
563
+ sub_cascading_composite_array.reject! do |object|
564
+ object != nil
565
+ end
566
+ sub_cascading_composite_array.should == [ ]
567
+ cascading_composite_array.should == [ :C ]
568
+
569
+ cascading_composite_array.reject!.is_a?( Enumerator ).should == true
570
+
571
+ end
572
+
573
+ #############
574
+ # replace #
575
+ #############
576
+
577
+ it 'can replace self' do
578
+
579
+ cascading_composite_array = ::CompositingArray::Unique.new
580
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
581
+
582
+ cascading_composite_array.push( :A, :B, :C )
583
+ cascading_composite_array.should == [ :A, :B, :C ]
584
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
585
+ cascading_composite_array.replace( [ :D, :E, :F ] )
586
+ cascading_composite_array.should == [ :D, :E, :F ]
587
+ sub_cascading_composite_array.should == [ :D, :E, :F ]
588
+
589
+ cascading_composite_array.should == [ :D, :E, :F ]
590
+ sub_cascading_composite_array.should == [ :D, :E, :F ]
591
+ sub_cascading_composite_array.replace( [ :G, :H, :I ] )
592
+ cascading_composite_array.should == [ :D, :E, :F ]
593
+ sub_cascading_composite_array.should == [ :G, :H, :I ]
594
+
595
+ end
596
+
597
+ ##############
598
+ # reverse! #
599
+ ##############
600
+
601
+ it 'can reverse self' do
602
+
603
+ cascading_composite_array = ::CompositingArray::Unique.new
604
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
605
+
606
+ cascading_composite_array.push( :A, :B, :C )
607
+ cascading_composite_array.should == [ :A, :B, :C ]
608
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
609
+ cascading_composite_array.reverse!
610
+ cascading_composite_array.should == [ :C, :B, :A ]
611
+ sub_cascading_composite_array.should == [ :C, :B, :A ]
612
+
613
+ cascading_composite_array.should == [ :C, :B, :A ]
614
+ sub_cascading_composite_array.should == [ :C, :B, :A ]
615
+ sub_cascading_composite_array.reverse!
616
+ cascading_composite_array.should == [ :C, :B, :A ]
617
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
618
+
619
+ end
620
+
621
+ #############
622
+ # rotate! #
623
+ #############
624
+
625
+ it 'can rotate self' do
626
+
627
+ cascading_composite_array = ::CompositingArray::Unique.new
628
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
629
+
630
+ cascading_composite_array.push( :A, :B, :C )
631
+ cascading_composite_array.should == [ :A, :B, :C ]
632
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
633
+
634
+ cascading_composite_array.rotate!
635
+ cascading_composite_array.should == [ :B, :C, :A ]
636
+ sub_cascading_composite_array.should == [ :B, :C, :A ]
637
+
638
+ cascading_composite_array.rotate!( -1 )
639
+ cascading_composite_array.should == [ :A, :B, :C ]
640
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
641
+
642
+ sub_cascading_composite_array.rotate!( 2 )
643
+ cascading_composite_array.should == [ :A, :B, :C ]
644
+ sub_cascading_composite_array.should == [ :C, :A, :B ]
645
+
646
+ end
647
+
648
+ #############
649
+ # select! #
650
+ #############
651
+
652
+ it 'can keep by select' do
653
+
654
+ cascading_composite_array = ::CompositingArray::Unique.new
655
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
656
+
657
+ cascading_composite_array.push( :A, :B, :C )
658
+ cascading_composite_array.should == [ :A, :B, :C ]
659
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
660
+ cascading_composite_array.select! do |object|
661
+ object == :C
662
+ end
663
+ cascading_composite_array.should == [ :C ]
664
+ sub_cascading_composite_array.should == [ :C ]
665
+
666
+ sub_cascading_composite_array.push( :C, :B )
667
+ cascading_composite_array.should == [ :C ]
668
+ sub_cascading_composite_array.should == [ :C, :B ]
669
+ sub_cascading_composite_array.select! do |object|
670
+ object == nil
671
+ end
672
+ cascading_composite_array.should == [ :C ]
673
+ sub_cascading_composite_array.should == [ ]
674
+
675
+ cascading_composite_array.select!.is_a?( Enumerator ).should == true
676
+
677
+ end
678
+
679
+ ##############
680
+ # shuffle! #
681
+ ##############
682
+
683
+ it 'can shuffle self' do
684
+
685
+ cascading_composite_array = ::CompositingArray::Unique.new
686
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
687
+
688
+ cascading_composite_array.push( :A, :B, :C )
689
+ cascading_composite_array.should == [ :A, :B, :C ]
690
+ sub_cascading_composite_array.should == cascading_composite_array
691
+
692
+ prior_version = cascading_composite_array.dup
693
+ attempts = [ ]
694
+ 50.times do
695
+ cascading_composite_array.shuffle!
696
+ attempts.push( cascading_composite_array == prior_version )
697
+ prior_version = cascading_composite_array.dup
698
+ end
699
+ attempts_correct = attempts.select { |member| member == false }.count
700
+ ( attempts_correct >= 10 ).should == true
701
+ sub_cascading_composite_array.should == cascading_composite_array
702
+ first_shuffle_version = cascading_composite_array
703
+
704
+ cascading_composite_array.should == first_shuffle_version
705
+ attempts = [ ]
706
+ 50.times do
707
+ sub_cascading_composite_array.shuffle!
708
+ attempts.push( sub_cascading_composite_array == cascading_composite_array )
709
+ prior_version = cascading_composite_array.dup
710
+ end
711
+ attempts_correct = attempts.select { |member| member == false }.count
712
+ ( attempts_correct >= 10 ).should == true
713
+
714
+ end
715
+
716
+ ##############
717
+ # collect! #
718
+ # map! #
719
+ ##############
720
+
721
+ it 'can replace by collect/map' do
722
+
723
+ cascading_composite_array = ::CompositingArray::Unique.new
724
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
725
+
726
+ cascading_composite_array.push( :A, :B, :C )
727
+ cascading_composite_array.should == [ :A, :B, :C ]
728
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
729
+ cascading_composite_array.collect! do |object|
730
+ :C
731
+ end
732
+ cascading_composite_array.should == [ :C ]
733
+ sub_cascading_composite_array.should == [ :C ]
734
+
735
+ sub_cascading_composite_array.collect! do |object|
736
+ :A
737
+ end
738
+ cascading_composite_array.should == [ :C ]
739
+ sub_cascading_composite_array.should == [ :A ]
740
+
741
+ cascading_composite_array.collect!.is_a?( Enumerator ).should == true
742
+
743
+ end
744
+
745
+ ###########
746
+ # sort! #
747
+ ###########
748
+
749
+ it 'can replace by collect/map' do
750
+
751
+ cascading_composite_array = ::CompositingArray::Unique.new
752
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
753
+
754
+ cascading_composite_array.push( :A, :B, :C )
755
+ cascading_composite_array.should == [ :A, :B, :C ]
756
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
757
+ cascading_composite_array.sort! do |a, b|
758
+ if a < b
759
+ 1
760
+ elsif a > b
761
+ -1
762
+ elsif a == b
763
+ 0
764
+ end
765
+ end
766
+ cascading_composite_array.should == [ :C, :B, :A ]
767
+ sub_cascading_composite_array.should == [ :C, :B, :A ]
768
+
769
+ sub_cascading_composite_array.sort! do |a, b|
770
+ if a < b
771
+ -1
772
+ elsif a > b
773
+ 1
774
+ elsif a == b
775
+ 0
776
+ end
777
+ end
778
+ cascading_composite_array.should == [ :C, :B, :A ]
779
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
780
+
781
+ cascading_composite_array.sort!
782
+ cascading_composite_array.should == [ :A, :B, :C ]
783
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
784
+
785
+ end
786
+
787
+ ##############
788
+ # sort_by! #
789
+ ##############
790
+
791
+ it 'can replace by collect/map' do
792
+
793
+ cascading_composite_array = ::CompositingArray::Unique.new
794
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
795
+
796
+ cascading_composite_array.push( :A, :B, :C )
797
+ cascading_composite_array.should == [ :A, :B, :C ]
798
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
799
+ cascading_composite_array.sort_by! do |object|
800
+ case object
801
+ when :A
802
+ :B
803
+ when :B
804
+ :A
805
+ when :C
806
+ :C
807
+ end
808
+ end
809
+ cascading_composite_array.should == [ :B, :A, :C ]
810
+ sub_cascading_composite_array.should == [ :B, :A, :C ]
811
+
812
+ sub_cascading_composite_array.sort_by! do |object|
813
+ case object
814
+ when :A
815
+ :C
816
+ when :B
817
+ :B
818
+ when :C
819
+ :A
820
+ end
821
+ end
822
+ cascading_composite_array.should == [ :B, :A, :C ]
823
+ sub_cascading_composite_array.should == [ :C, :B, :A ]
824
+
825
+ cascading_composite_array.sort_by!.is_a?( Enumerator ).should == true
826
+
827
+ end
828
+
829
+ ###########
830
+ # uniq! #
831
+ ###########
832
+
833
+ it 'can remove non-unique elements' do
834
+
835
+ cascading_composite_array = ::CompositingArray::Unique.new
836
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
837
+
838
+ cascading_composite_array.push( :A, :B, :C, :C, :C, :B, :A )
839
+ cascading_composite_array.should == [ :A, :B, :C ]
840
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
841
+ cascading_composite_array.uniq!
842
+ cascading_composite_array.should == [ :A, :B, :C ]
843
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
844
+
845
+ sub_cascading_composite_array.push( :C, :B )
846
+ cascading_composite_array.should == [ :A, :B, :C ]
847
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
848
+ sub_cascading_composite_array.uniq!
849
+ cascading_composite_array.should == [ :A, :B, :C ]
850
+ sub_cascading_composite_array.should == [ :A, :B, :C ]
851
+
852
+ end
853
+
854
+ #############
855
+ # unshift #
856
+ #############
857
+
858
+ it 'can unshift onto the first element' do
859
+
860
+ cascading_composite_array = ::CompositingArray::Unique.new
861
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
862
+
863
+ cascading_composite_array += :A
864
+ cascading_composite_array.should == [ :A ]
865
+ sub_cascading_composite_array.should == [ :A ]
866
+
867
+ cascading_composite_array.unshift( :B )
868
+ cascading_composite_array.should == [ :B, :A ]
869
+ sub_cascading_composite_array.should == [ :B, :A ]
870
+
871
+ sub_cascading_composite_array.unshift( :C )
872
+ cascading_composite_array.should == [ :B, :A ]
873
+ sub_cascading_composite_array.should == [ :C, :B, :A ]
874
+
875
+ end
876
+
877
+ #########
878
+ # pop #
879
+ #########
880
+
881
+ it 'can pop the final element' do
882
+
883
+ cascading_composite_array = ::CompositingArray::Unique.new
884
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
885
+
886
+ cascading_composite_array += :A
887
+ cascading_composite_array.should == [ :A ]
888
+ sub_cascading_composite_array.should == [ :A ]
889
+
890
+ cascading_composite_array.pop.should == :A
891
+ cascading_composite_array.should == [ ]
892
+ sub_cascading_composite_array.should == [ ]
893
+
894
+ cascading_composite_array += :B
895
+ cascading_composite_array.should == [ :B ]
896
+ sub_cascading_composite_array.should == [ :B ]
897
+
898
+ sub_cascading_composite_array += :C
899
+ cascading_composite_array.should == [ :B ]
900
+ sub_cascading_composite_array.should == [ :B, :C ]
901
+ sub_cascading_composite_array.pop.should == :C
902
+ cascading_composite_array.should == [ :B ]
903
+ sub_cascading_composite_array.should == [ :B ]
904
+
905
+ end
906
+
907
+ ###########
908
+ # shift #
909
+ ###########
910
+
911
+ it 'can shift the first element' do
912
+
913
+ cascading_composite_array = ::CompositingArray::Unique.new
914
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
915
+
916
+ cascading_composite_array += :A
917
+ cascading_composite_array.should == [ :A ]
918
+ sub_cascading_composite_array.should == [ :A ]
919
+
920
+ cascading_composite_array.shift.should == :A
921
+ cascading_composite_array.should == [ ]
922
+ sub_cascading_composite_array.should == [ ]
923
+
924
+ cascading_composite_array += :B
925
+ cascading_composite_array.should == [ :B ]
926
+ sub_cascading_composite_array.should == [ :B ]
927
+
928
+ sub_cascading_composite_array += :C
929
+ cascading_composite_array.should == [ :B ]
930
+ sub_cascading_composite_array.should == [ :B, :C ]
931
+ sub_cascading_composite_array.shift.should == :B
932
+ cascading_composite_array.should == [ :B ]
933
+ sub_cascading_composite_array.should == [ :C ]
934
+
935
+ end
936
+
937
+ ############
938
+ # slice! #
939
+ ############
940
+
941
+ it 'can slice elements' do
942
+
943
+ cascading_composite_array = ::CompositingArray::Unique.new
944
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
945
+
946
+ cascading_composite_array += :A
947
+ cascading_composite_array.should == [ :A ]
948
+ sub_cascading_composite_array.should == [ :A ]
949
+
950
+ cascading_composite_array.slice!( 0, 1 ).should == [ :A ]
951
+ cascading_composite_array.should == [ ]
952
+ sub_cascading_composite_array.should == [ ]
953
+
954
+ cascading_composite_array += :B
955
+ cascading_composite_array.should == [ :B ]
956
+ sub_cascading_composite_array.should == [ :B ]
957
+
958
+ sub_cascading_composite_array += :C
959
+ cascading_composite_array.should == [ :B ]
960
+ sub_cascading_composite_array.should == [ :B, :C ]
961
+
962
+ sub_cascading_composite_array.slice!( 0, 1 ).should == [ :B ]
963
+ cascading_composite_array.should == [ :B ]
964
+ sub_cascading_composite_array.should == [ :C ]
965
+
966
+ end
967
+
968
+ ###########
969
+ # clear #
970
+ ###########
971
+
972
+ it 'can clear, causing present elements to be excluded' do
973
+
974
+ cascading_composite_array = ::CompositingArray::Unique.new
975
+ sub_cascading_composite_array = ::CompositingArray::Unique.new( cascading_composite_array )
976
+
977
+ cascading_composite_array += :A
978
+ cascading_composite_array.should == [ :A ]
979
+ sub_cascading_composite_array.should == [ :A ]
980
+
981
+ cascading_composite_array.clear
982
+ cascading_composite_array.should == [ ]
983
+ sub_cascading_composite_array.should == [ ]
984
+
985
+ cascading_composite_array += :B
986
+ cascading_composite_array.should == [ :B ]
987
+ sub_cascading_composite_array.should == [ :B ]
988
+
989
+ sub_cascading_composite_array += :C
990
+ cascading_composite_array.should == [ :B ]
991
+ sub_cascading_composite_array.should == [ :B, :C ]
992
+
993
+ sub_cascading_composite_array.clear
994
+ cascading_composite_array.should == [ :B ]
995
+ sub_cascading_composite_array.should == [ ]
996
+
997
+ end
998
+
999
+ ##################
1000
+ # pre_set_hook #
1001
+ ##################
1002
+
1003
+ it 'has a hook that is called before setting a value; return value is used in place of object' do
1004
+
1005
+ class ::CompositingArray::Unique::SubMockPreSet < ::CompositingArray
1006
+
1007
+ def pre_set_hook( index, object, is_insert = false )
1008
+ return :some_other_value
1009
+ end
1010
+
1011
+ end
1012
+
1013
+ cascading_composite_array = ::CompositingArray::Unique::SubMockPreSet.new
1014
+
1015
+ cascading_composite_array.push( :some_value )
1016
+
1017
+ cascading_composite_array.should == [ :some_other_value ]
1018
+
1019
+ end
1020
+
1021
+ ###################
1022
+ # post_set_hook #
1023
+ ###################
1024
+
1025
+ it 'has a hook that is called after setting a value' do
1026
+
1027
+ class ::CompositingArray::Unique::SubMockPostSet < ::CompositingArray
1028
+
1029
+ def post_set_hook( index, object, is_insert = false )
1030
+ return :some_other_value
1031
+ end
1032
+
1033
+ end
1034
+
1035
+ cascading_composite_array = ::CompositingArray::Unique::SubMockPostSet.new
1036
+
1037
+ cascading_composite_array.push( :some_value ).should == [ :some_other_value ]
1038
+
1039
+ cascading_composite_array.should == [ :some_value ]
1040
+
1041
+ end
1042
+
1043
+ ##################
1044
+ # pre_get_hook #
1045
+ ##################
1046
+
1047
+ it 'has a hook that is called before getting a value; if return value is false, get does not occur' do
1048
+
1049
+ class ::CompositingArray::Unique::SubMockPreGet < ::CompositingArray
1050
+
1051
+ def pre_get_hook( index )
1052
+ return false
1053
+ end
1054
+
1055
+ end
1056
+
1057
+ cascading_composite_array = ::CompositingArray::Unique::SubMockPreGet.new
1058
+
1059
+ cascading_composite_array.push( :some_value )
1060
+ cascading_composite_array[ 0 ].should == nil
1061
+
1062
+ cascading_composite_array.should == [ :some_value ]
1063
+
1064
+ end
1065
+
1066
+ ###################
1067
+ # post_get_hook #
1068
+ ###################
1069
+
1070
+ it 'has a hook that is called after getting a value' do
1071
+
1072
+ class ::CompositingArray::Unique::SubMockPostGet < ::CompositingArray
1073
+
1074
+ def post_get_hook( index, object )
1075
+ return :some_other_value
1076
+ end
1077
+
1078
+ end
1079
+
1080
+ cascading_composite_array = ::CompositingArray::Unique::SubMockPostGet.new
1081
+
1082
+ cascading_composite_array.push( :some_value )
1083
+ cascading_composite_array[ 0 ].should == :some_other_value
1084
+
1085
+ cascading_composite_array.should == [ :some_value ]
1086
+
1087
+ end
1088
+
1089
+ #####################
1090
+ # pre_delete_hook #
1091
+ #####################
1092
+
1093
+ it 'has a hook that is called before deleting an index; if return value is false, delete does not occur' do
1094
+
1095
+ class ::CompositingArray::Unique::SubMockPreDelete < ::CompositingArray
1096
+
1097
+ def pre_delete_hook( index )
1098
+ return false
1099
+ end
1100
+
1101
+ end
1102
+
1103
+ cascading_composite_array = ::CompositingArray::Unique::SubMockPreDelete.new
1104
+
1105
+ cascading_composite_array.push( :some_value )
1106
+ cascading_composite_array.delete_at( 0 )
1107
+
1108
+ cascading_composite_array.should == [ :some_value ]
1109
+
1110
+ end
1111
+
1112
+ ######################
1113
+ # post_delete_hook #
1114
+ ######################
1115
+
1116
+ it 'has a hook that is called after deleting an index' do
1117
+
1118
+ class ::CompositingArray::Unique::SubMockPostDelete < ::CompositingArray
1119
+
1120
+ def post_delete_hook( index, object )
1121
+ return :some_other_value
1122
+ end
1123
+
1124
+ end
1125
+
1126
+ cascading_composite_array = ::CompositingArray::Unique::SubMockPostDelete.new
1127
+
1128
+ cascading_composite_array.push( :some_value )
1129
+ cascading_composite_array.delete_at( 0 ).should == :some_other_value
1130
+
1131
+ cascading_composite_array.should == [ ]
1132
+
1133
+ end
1134
+
1135
+ ########################
1136
+ # child_pre_set_hook #
1137
+ ########################
1138
+
1139
+ it 'has a hook that is called before setting a value that has been passed by a parent; return value is used in place of object' do
1140
+
1141
+ class ::CompositingArray::Unique::SubMockChildPreSet < ::CompositingArray
1142
+
1143
+ def child_pre_set_hook( index, object, is_insert = false )
1144
+ return :some_other_value
1145
+ end
1146
+
1147
+ end
1148
+
1149
+ cascading_composite_array = ::CompositingArray::Unique::SubMockChildPreSet.new
1150
+ sub_cascading_composite_array = ::CompositingArray::Unique::SubMockChildPreSet.new( cascading_composite_array )
1151
+ cascading_composite_array.push( :some_value )
1152
+
1153
+ sub_cascading_composite_array.should == [ :some_other_value ]
1154
+
1155
+ end
1156
+
1157
+ #########################
1158
+ # child_post_set_hook #
1159
+ #########################
1160
+
1161
+ it 'has a hook that is called after setting a value passed by a parent' do
1162
+
1163
+ class ::CompositingArray::Unique::SubMockChildPostSet < ::CompositingArray
1164
+
1165
+ def child_post_set_hook( index, object, is_insert = false )
1166
+ push( :some_other_value )
1167
+ end
1168
+
1169
+ end
1170
+
1171
+ cascading_composite_array = ::CompositingArray::Unique::SubMockChildPostSet.new
1172
+ sub_cascading_composite_array = ::CompositingArray::Unique::SubMockChildPostSet.new( cascading_composite_array )
1173
+ cascading_composite_array.push( :some_value )
1174
+
1175
+ cascading_composite_array.should == [ :some_value ]
1176
+ sub_cascading_composite_array.should == [ :some_value, :some_other_value ]
1177
+
1178
+ end
1179
+
1180
+ ###########################
1181
+ # child_pre_delete_hook #
1182
+ ###########################
1183
+
1184
+ it 'has a hook that is called before deleting an index that has been passed by a parent; if return value is false, delete does not occur' do
1185
+
1186
+ class ::CompositingArray::Unique::SubMockChildPreDelete < ::CompositingArray
1187
+
1188
+ def child_pre_delete_hook( index )
1189
+ false
1190
+ end
1191
+
1192
+ end
1193
+
1194
+ cascading_composite_array = ::CompositingArray::Unique::SubMockChildPreDelete.new
1195
+ sub_cascading_composite_array = ::CompositingArray::Unique::SubMockChildPreDelete.new( cascading_composite_array )
1196
+ cascading_composite_array.push( :some_value )
1197
+ cascading_composite_array.delete( :some_value )
1198
+
1199
+ cascading_composite_array.should == [ ]
1200
+ sub_cascading_composite_array.should == [ :some_value ]
1201
+
1202
+ end
1203
+
1204
+ ############################
1205
+ # child_post_delete_hook #
1206
+ ############################
1207
+
1208
+ it 'has a hook that is called after deleting an index passed by a parent' do
1209
+
1210
+ class ::CompositingArray::Unique::SubMockChildPostDelete < ::CompositingArray
1211
+
1212
+ def child_post_delete_hook( index, object )
1213
+ delete( :some_other_value )
1214
+ end
1215
+
1216
+ end
1217
+
1218
+ cascading_composite_array = ::CompositingArray::Unique::SubMockChildPostDelete.new
1219
+ sub_cascading_composite_array = ::CompositingArray::Unique::SubMockChildPostDelete.new( cascading_composite_array )
1220
+ cascading_composite_array.push( :some_value )
1221
+ sub_cascading_composite_array.push( :some_other_value )
1222
+ cascading_composite_array.delete( :some_value )
1223
+
1224
+ cascading_composite_array.should == [ ]
1225
+ sub_cascading_composite_array.should == [ ]
1226
+
1227
+ end
1228
+
1229
+ end