compositing-array-sorted-unique 1.0.12

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