array-sorted-unique 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.rdoc +8 -0
- data/README.md +44 -0
- data/lib/array-sorted-unique.rb +2 -0
- data/lib/array/hooked/sorted/unique.rb +2 -0
- data/lib/array/hooked/unique/sorted.rb +2 -0
- data/lib/array/sorted/namespaces.rb +10 -0
- data/lib/array/sorted/requires.rb +27 -0
- data/lib/array/sorted/unique.rb +15 -0
- data/lib/array/sorted/unique/array_interface.rb +10 -0
- data/lib/array/unique/sorted.rb +2 -0
- data/lib/sorted_array/unique.rb +6 -0
- data/lib/sorted_array/unique/array_interface.rb +10 -0
- data/spec/array/sorted/unique_spec.rb +669 -0
- metadata +91 -0
data/CHANGELOG.rdoc
ADDED
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Sorted Unique Array #
|
2
|
+
|
3
|
+
http://rubygems.org/gems/array-sorted-unique
|
4
|
+
|
5
|
+
# Description #
|
6
|
+
|
7
|
+
A subclass of Array::Sorted and Array::Unique (and therefore also Array::Hooked).
|
8
|
+
|
9
|
+
# Summary #
|
10
|
+
|
11
|
+
Provides Array::Sorted::Unique and its alias Array::Unique::Sorted as well as UniqueSortedArray and its alias SortedUniqueArray.
|
12
|
+
|
13
|
+
# Install #
|
14
|
+
|
15
|
+
* sudo gem install array-sorted-unique
|
16
|
+
|
17
|
+
# Usage #
|
18
|
+
|
19
|
+
Just like Array::Sorted and Array::Unique (provided by sorted-array and unique-array), which are subclasses of Array (and Array::Hooked, provided by hooked_array)!
|
20
|
+
|
21
|
+
# License #
|
22
|
+
|
23
|
+
(The MIT License)
|
24
|
+
|
25
|
+
Copyright (c) Asher
|
26
|
+
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
28
|
+
a copy of this software and associated documentation files (the
|
29
|
+
'Software'), to deal in the Software without restriction, including
|
30
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
31
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
32
|
+
permit persons to whom the Software is furnished to do so, subject to
|
33
|
+
the following conditions:
|
34
|
+
|
35
|
+
The above copyright notice and this permission notice shall be
|
36
|
+
included in all copies or substantial portions of the Software.
|
37
|
+
|
38
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
39
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
40
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
41
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
42
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
43
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
44
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
basepath = 'unique'
|
3
|
+
|
4
|
+
files = [
|
5
|
+
|
6
|
+
'array_interface'
|
7
|
+
|
8
|
+
]
|
9
|
+
|
10
|
+
second_basepath = '../../sorted_array/unique'
|
11
|
+
|
12
|
+
second_files = [
|
13
|
+
|
14
|
+
'array_interface'
|
15
|
+
|
16
|
+
]
|
17
|
+
|
18
|
+
files.each do |this_file|
|
19
|
+
require_relative( File.join( basepath, this_file ) + '.rb' )
|
20
|
+
end
|
21
|
+
second_files.each do |this_file|
|
22
|
+
require_relative( File.join( second_basepath, this_file ) + '.rb' )
|
23
|
+
end
|
24
|
+
|
25
|
+
require_relative( basepath + '.rb' )
|
26
|
+
require_relative( second_basepath + '.rb' )
|
27
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
require 'array/sorted'
|
3
|
+
require 'array/unique'
|
4
|
+
|
5
|
+
# namespaces that have to be declared ahead of time for proper load order
|
6
|
+
require_relative './namespaces'
|
7
|
+
|
8
|
+
# source file requires
|
9
|
+
require_relative './requires.rb'
|
10
|
+
|
11
|
+
class ::Array::Sorted::Unique < ::Array::Unique
|
12
|
+
|
13
|
+
include ::Array::Sorted::Unique::ArrayInterface
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,669 @@
|
|
1
|
+
|
2
|
+
require_relative '../../../lib/array-sorted-unique.rb'
|
3
|
+
|
4
|
+
describe ::Array::Sorted::Unique do
|
5
|
+
|
6
|
+
################
|
7
|
+
# initialize #
|
8
|
+
################
|
9
|
+
|
10
|
+
it 'can add initialize with an ancestor, inheriting its values and linking to it as a child' do
|
11
|
+
|
12
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
13
|
+
|
14
|
+
sorted_unique_array.instance_variable_get( :@parent_composite_object ).should == nil
|
15
|
+
sorted_unique_array.should == []
|
16
|
+
sorted_unique_array.push( :A, :B, :C, :D )
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
#########
|
21
|
+
# []= #
|
22
|
+
#########
|
23
|
+
|
24
|
+
it 'can add elements' do
|
25
|
+
|
26
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
27
|
+
|
28
|
+
sorted_unique_array[ 0 ] = :A
|
29
|
+
sorted_unique_array.should == [ :A ]
|
30
|
+
|
31
|
+
sorted_unique_array[ 1 ] = :B
|
32
|
+
sorted_unique_array.should == [ :A, :B ]
|
33
|
+
|
34
|
+
sorted_unique_array[ 0 ] = :D
|
35
|
+
sorted_unique_array.should == [ :B, :D ]
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
############
|
40
|
+
# insert #
|
41
|
+
############
|
42
|
+
|
43
|
+
it 'can insert elements' do
|
44
|
+
|
45
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
46
|
+
|
47
|
+
sorted_unique_array.insert( 3, :D )
|
48
|
+
sorted_unique_array.should == [ nil, :D ]
|
49
|
+
|
50
|
+
sorted_unique_array.insert( 1, :B )
|
51
|
+
sorted_unique_array.should == [ nil, :B, :D ]
|
52
|
+
|
53
|
+
sorted_unique_array.insert( 2, :C )
|
54
|
+
sorted_unique_array.should == [ nil, :B, :C, :D ]
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
##########
|
59
|
+
# push #
|
60
|
+
# << #
|
61
|
+
##########
|
62
|
+
|
63
|
+
it 'can add elements' do
|
64
|
+
|
65
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
66
|
+
|
67
|
+
sorted_unique_array << :A
|
68
|
+
sorted_unique_array.should == [ :A ]
|
69
|
+
|
70
|
+
sorted_unique_array << :B
|
71
|
+
sorted_unique_array.should == [ :A, :B ]
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
############
|
76
|
+
# concat #
|
77
|
+
# + #
|
78
|
+
############
|
79
|
+
|
80
|
+
it 'can add elements' do
|
81
|
+
|
82
|
+
# NOTE: this breaks + by causing it to modify the array like +=
|
83
|
+
# The alternative was worse.
|
84
|
+
|
85
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
86
|
+
|
87
|
+
sorted_unique_array.concat( [ :A ] )
|
88
|
+
sorted_unique_array.should == [ :A ]
|
89
|
+
|
90
|
+
sorted_unique_array += [ :B ]
|
91
|
+
sorted_unique_array.should == [ :A, :B ]
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
####################
|
96
|
+
# delete_objects #
|
97
|
+
####################
|
98
|
+
|
99
|
+
it 'can delete multiple elements' do
|
100
|
+
|
101
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
102
|
+
|
103
|
+
sorted_unique_array += [ :A, :B ]
|
104
|
+
sorted_unique_array.should == [ :A, :B ]
|
105
|
+
|
106
|
+
sorted_unique_array.delete_objects( :A, :B )
|
107
|
+
sorted_unique_array.should == [ ]
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
#######
|
112
|
+
# - #
|
113
|
+
#######
|
114
|
+
|
115
|
+
it 'can exclude elements' do
|
116
|
+
|
117
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
118
|
+
|
119
|
+
sorted_unique_array.push( :A )
|
120
|
+
sorted_unique_array.should == [ :A ]
|
121
|
+
|
122
|
+
sorted_unique_array -= [ :A ]
|
123
|
+
sorted_unique_array.should == [ ]
|
124
|
+
|
125
|
+
sorted_unique_array.push( :B )
|
126
|
+
sorted_unique_array.should == [ :B ]
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
############
|
131
|
+
# delete #
|
132
|
+
############
|
133
|
+
|
134
|
+
it 'can delete elements' do
|
135
|
+
|
136
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
137
|
+
|
138
|
+
sorted_unique_array.push( :A )
|
139
|
+
sorted_unique_array.should == [ :A ]
|
140
|
+
|
141
|
+
sorted_unique_array.delete( :A )
|
142
|
+
sorted_unique_array.should == [ ]
|
143
|
+
|
144
|
+
sorted_unique_array.push( :B )
|
145
|
+
sorted_unique_array.should == [ :B ]
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
###############
|
150
|
+
# delete_at #
|
151
|
+
###############
|
152
|
+
|
153
|
+
it 'can delete by indexes' do
|
154
|
+
|
155
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
156
|
+
|
157
|
+
sorted_unique_array.push( :A )
|
158
|
+
sorted_unique_array.should == [ :A ]
|
159
|
+
|
160
|
+
sorted_unique_array.delete_at( 0 )
|
161
|
+
sorted_unique_array.should == [ ]
|
162
|
+
|
163
|
+
sorted_unique_array.push( :B )
|
164
|
+
sorted_unique_array.should == [ :B ]
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
#######################
|
169
|
+
# delete_at_indexes #
|
170
|
+
#######################
|
171
|
+
|
172
|
+
it 'can delete by indexes' do
|
173
|
+
|
174
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
175
|
+
|
176
|
+
sorted_unique_array.push( :A, :B, :C )
|
177
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
178
|
+
|
179
|
+
sorted_unique_array.delete_at_indexes( 0, 1 )
|
180
|
+
sorted_unique_array.should == [ :C ]
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
###############
|
185
|
+
# delete_if #
|
186
|
+
###############
|
187
|
+
|
188
|
+
it 'can delete by block' do
|
189
|
+
|
190
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
191
|
+
|
192
|
+
sorted_unique_array.push( :A, :B, :C )
|
193
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
194
|
+
sorted_unique_array.delete_if do |object|
|
195
|
+
object != :C
|
196
|
+
end
|
197
|
+
sorted_unique_array.should == [ :C ]
|
198
|
+
|
199
|
+
sorted_unique_array.delete_if.is_a?( Enumerator ).should == true
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
#############
|
204
|
+
# keep_if #
|
205
|
+
#############
|
206
|
+
|
207
|
+
it 'can keep by block' do
|
208
|
+
|
209
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
210
|
+
|
211
|
+
sorted_unique_array.push( :A, :B, :C )
|
212
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
213
|
+
sorted_unique_array.keep_if do |object|
|
214
|
+
object == :C
|
215
|
+
end
|
216
|
+
sorted_unique_array.should == [ :C ]
|
217
|
+
|
218
|
+
end
|
219
|
+
|
220
|
+
##############
|
221
|
+
# compact! #
|
222
|
+
##############
|
223
|
+
|
224
|
+
it 'can compact' do
|
225
|
+
|
226
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
227
|
+
|
228
|
+
sorted_unique_array.push( :A, nil, :B, nil, :C, nil )
|
229
|
+
sorted_unique_array.should == [ nil, :A, :B, :C ]
|
230
|
+
sorted_unique_array.compact!
|
231
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
##############
|
236
|
+
# flatten! #
|
237
|
+
##############
|
238
|
+
|
239
|
+
it 'can flatten' do
|
240
|
+
|
241
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
242
|
+
|
243
|
+
sorted_unique_array.push( :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] )
|
244
|
+
sorted_unique_array.should == [ :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] ]
|
245
|
+
sorted_unique_array.flatten!
|
246
|
+
sorted_unique_array.should == [ :A, :B, :C, :F_A, :F_B, :F_C, :F_D, :F_E ]
|
247
|
+
|
248
|
+
end
|
249
|
+
|
250
|
+
#############
|
251
|
+
# reject! #
|
252
|
+
#############
|
253
|
+
|
254
|
+
it 'can reject' do
|
255
|
+
|
256
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
257
|
+
|
258
|
+
sorted_unique_array.push( :A, :B, :C )
|
259
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
260
|
+
sorted_unique_array.reject! do |object|
|
261
|
+
object != :C
|
262
|
+
end
|
263
|
+
sorted_unique_array.should == [ :C ]
|
264
|
+
|
265
|
+
sorted_unique_array.reject!.is_a?( Enumerator ).should == true
|
266
|
+
|
267
|
+
end
|
268
|
+
|
269
|
+
#############
|
270
|
+
# replace #
|
271
|
+
#############
|
272
|
+
|
273
|
+
it 'can replace self' do
|
274
|
+
|
275
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
276
|
+
|
277
|
+
sorted_unique_array.push( :A, :B, :C )
|
278
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
279
|
+
sorted_unique_array.replace( [ :D, :E, :F ] )
|
280
|
+
sorted_unique_array.should == [ :D, :E, :F ]
|
281
|
+
|
282
|
+
end
|
283
|
+
|
284
|
+
##############
|
285
|
+
# reverse! #
|
286
|
+
##############
|
287
|
+
|
288
|
+
it 'can reverse self' do
|
289
|
+
|
290
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
291
|
+
|
292
|
+
sorted_unique_array.push( :A, :B, :C )
|
293
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
294
|
+
sorted_unique_array.reverse!
|
295
|
+
sorted_unique_array.should == [ :C, :B, :A ]
|
296
|
+
|
297
|
+
end
|
298
|
+
|
299
|
+
#############
|
300
|
+
# rotate! #
|
301
|
+
#############
|
302
|
+
|
303
|
+
it 'can rotate self' do
|
304
|
+
|
305
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
306
|
+
|
307
|
+
sorted_unique_array.push( :A, :B, :C )
|
308
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
309
|
+
|
310
|
+
sorted_unique_array.rotate!
|
311
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
312
|
+
|
313
|
+
sorted_unique_array.rotate!( -1 )
|
314
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
315
|
+
|
316
|
+
end
|
317
|
+
|
318
|
+
#############
|
319
|
+
# select! #
|
320
|
+
#############
|
321
|
+
|
322
|
+
it 'can keep by select' do
|
323
|
+
|
324
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
325
|
+
|
326
|
+
sorted_unique_array.push( :A, :B, :C )
|
327
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
328
|
+
sorted_unique_array.select! do |object|
|
329
|
+
object == :C
|
330
|
+
end
|
331
|
+
sorted_unique_array.should == [ :C ]
|
332
|
+
|
333
|
+
sorted_unique_array.select!.is_a?( Enumerator ).should == true
|
334
|
+
|
335
|
+
end
|
336
|
+
|
337
|
+
##############
|
338
|
+
# shuffle! #
|
339
|
+
##############
|
340
|
+
|
341
|
+
it 'can shuffle self' do
|
342
|
+
|
343
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
344
|
+
|
345
|
+
sorted_unique_array.push( :A, :B, :C )
|
346
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
347
|
+
|
348
|
+
first_shuffle_version = sorted_unique_array.dup
|
349
|
+
sorted_unique_array.shuffle!
|
350
|
+
sorted_unique_array.should == first_shuffle_version
|
351
|
+
|
352
|
+
end
|
353
|
+
|
354
|
+
##############
|
355
|
+
# collect! #
|
356
|
+
# map! #
|
357
|
+
##############
|
358
|
+
|
359
|
+
it 'can replace by collect/map' do
|
360
|
+
|
361
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
362
|
+
|
363
|
+
sorted_unique_array.push( :A, :B, :C )
|
364
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
365
|
+
sorted_unique_array.collect! do |object|
|
366
|
+
:C
|
367
|
+
end
|
368
|
+
sorted_unique_array.should == [ :C, ]
|
369
|
+
|
370
|
+
sorted_unique_array.collect!.is_a?( Enumerator ).should == true
|
371
|
+
|
372
|
+
end
|
373
|
+
|
374
|
+
###########
|
375
|
+
# sort! #
|
376
|
+
###########
|
377
|
+
|
378
|
+
it 'can replace by collect/map' do
|
379
|
+
|
380
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
381
|
+
|
382
|
+
sorted_unique_array.push( :A, :B, :C )
|
383
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
384
|
+
sorted_unique_array.sort! do |a, b|
|
385
|
+
if a < b
|
386
|
+
1
|
387
|
+
elsif a > b
|
388
|
+
-1
|
389
|
+
elsif a == b
|
390
|
+
0
|
391
|
+
end
|
392
|
+
end
|
393
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
394
|
+
|
395
|
+
sorted_unique_array.sort!
|
396
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
397
|
+
|
398
|
+
end
|
399
|
+
|
400
|
+
##############
|
401
|
+
# sort_by! #
|
402
|
+
##############
|
403
|
+
|
404
|
+
it 'can replace by collect/map' do
|
405
|
+
|
406
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
407
|
+
|
408
|
+
sorted_unique_array.push( :A, :B, :C )
|
409
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
410
|
+
sorted_unique_array.sort_by! do |object|
|
411
|
+
case object
|
412
|
+
when :A
|
413
|
+
:B
|
414
|
+
when :B
|
415
|
+
:A
|
416
|
+
when :C
|
417
|
+
:C
|
418
|
+
end
|
419
|
+
end
|
420
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
421
|
+
|
422
|
+
sorted_unique_array.sort_by!.is_a?( Enumerator ).should == true
|
423
|
+
|
424
|
+
end
|
425
|
+
|
426
|
+
###########
|
427
|
+
# uniq! #
|
428
|
+
###########
|
429
|
+
|
430
|
+
it 'can remove non-unique elements' do
|
431
|
+
|
432
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
433
|
+
|
434
|
+
sorted_unique_array.push( :A, :B, :C, :C, :C, :B, :A )
|
435
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
436
|
+
sorted_unique_array.uniq!
|
437
|
+
sorted_unique_array.should == [ :A, :B, :C ]
|
438
|
+
|
439
|
+
end
|
440
|
+
|
441
|
+
#############
|
442
|
+
# unshift #
|
443
|
+
#############
|
444
|
+
|
445
|
+
it 'can unshift onto the first element' do
|
446
|
+
|
447
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
448
|
+
|
449
|
+
sorted_unique_array += :A
|
450
|
+
sorted_unique_array.should == [ :A ]
|
451
|
+
|
452
|
+
sorted_unique_array.unshift( :B )
|
453
|
+
sorted_unique_array.should == [ :A, :B ]
|
454
|
+
|
455
|
+
end
|
456
|
+
|
457
|
+
#########
|
458
|
+
# pop #
|
459
|
+
#########
|
460
|
+
|
461
|
+
it 'can pop the final element' do
|
462
|
+
|
463
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
464
|
+
|
465
|
+
sorted_unique_array += :A
|
466
|
+
sorted_unique_array.should == [ :A ]
|
467
|
+
|
468
|
+
sorted_unique_array.pop.should == :A
|
469
|
+
sorted_unique_array.should == [ ]
|
470
|
+
|
471
|
+
sorted_unique_array += :B
|
472
|
+
sorted_unique_array.should == [ :B ]
|
473
|
+
|
474
|
+
end
|
475
|
+
|
476
|
+
###########
|
477
|
+
# shift #
|
478
|
+
###########
|
479
|
+
|
480
|
+
it 'can shift the first element' do
|
481
|
+
|
482
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
483
|
+
|
484
|
+
sorted_unique_array += :A
|
485
|
+
sorted_unique_array.should == [ :A ]
|
486
|
+
|
487
|
+
sorted_unique_array.shift.should == :A
|
488
|
+
sorted_unique_array.should == [ ]
|
489
|
+
|
490
|
+
sorted_unique_array += :B
|
491
|
+
sorted_unique_array.should == [ :B ]
|
492
|
+
|
493
|
+
end
|
494
|
+
|
495
|
+
############
|
496
|
+
# slice! #
|
497
|
+
############
|
498
|
+
|
499
|
+
it 'can slice elements' do
|
500
|
+
|
501
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
502
|
+
|
503
|
+
sorted_unique_array += :A
|
504
|
+
sorted_unique_array.should == [ :A ]
|
505
|
+
|
506
|
+
sorted_unique_array.slice!( 0, 1 ).should == [ :A ]
|
507
|
+
sorted_unique_array.should == [ ]
|
508
|
+
|
509
|
+
sorted_unique_array += :B
|
510
|
+
sorted_unique_array.should == [ :B ]
|
511
|
+
|
512
|
+
end
|
513
|
+
|
514
|
+
###########
|
515
|
+
# clear #
|
516
|
+
###########
|
517
|
+
|
518
|
+
it 'can clear, causing present elements to be excluded' do
|
519
|
+
|
520
|
+
sorted_unique_array = ::Array::Sorted::Unique.new
|
521
|
+
|
522
|
+
sorted_unique_array += :A
|
523
|
+
sorted_unique_array.should == [ :A ]
|
524
|
+
|
525
|
+
sorted_unique_array.clear
|
526
|
+
sorted_unique_array.should == [ ]
|
527
|
+
|
528
|
+
sorted_unique_array += :B
|
529
|
+
sorted_unique_array.should == [ :B ]
|
530
|
+
|
531
|
+
end
|
532
|
+
|
533
|
+
##################
|
534
|
+
# pre_set_hook #
|
535
|
+
##################
|
536
|
+
|
537
|
+
it 'has a hook that is called before setting a value; return value is used in place of object' do
|
538
|
+
|
539
|
+
class ::Array::Sorted::Unique::SubMockPreSet < ::Array::Sorted::Unique
|
540
|
+
|
541
|
+
def pre_set_hook( index, object, is_insert = false )
|
542
|
+
return :some_other_value
|
543
|
+
end
|
544
|
+
|
545
|
+
end
|
546
|
+
|
547
|
+
sorted_unique_array = ::Array::Sorted::Unique::SubMockPreSet.new
|
548
|
+
|
549
|
+
sorted_unique_array.push( :some_value )
|
550
|
+
|
551
|
+
sorted_unique_array.should == [ :some_other_value ]
|
552
|
+
|
553
|
+
end
|
554
|
+
|
555
|
+
###################
|
556
|
+
# post_set_hook #
|
557
|
+
###################
|
558
|
+
|
559
|
+
it 'has a hook that is called after setting a value' do
|
560
|
+
|
561
|
+
class ::Array::Sorted::Unique::SubMockPostSet < ::Array::Sorted::Unique
|
562
|
+
|
563
|
+
def post_set_hook( index, object, is_insert = false )
|
564
|
+
return :some_other_value
|
565
|
+
end
|
566
|
+
|
567
|
+
end
|
568
|
+
|
569
|
+
sorted_unique_array = ::Array::Sorted::Unique::SubMockPostSet.new
|
570
|
+
|
571
|
+
sorted_unique_array.push( :some_value ).should == [ :some_other_value ]
|
572
|
+
|
573
|
+
sorted_unique_array.should == [ :some_value ]
|
574
|
+
|
575
|
+
end
|
576
|
+
|
577
|
+
##################
|
578
|
+
# pre_get_hook #
|
579
|
+
##################
|
580
|
+
|
581
|
+
it 'has a hook that is called before getting a value; if return value is false, get does not occur' do
|
582
|
+
|
583
|
+
class ::Array::Sorted::Unique::SubMockPreGet < ::Array::Sorted::Unique
|
584
|
+
|
585
|
+
def pre_get_hook( index )
|
586
|
+
return false
|
587
|
+
end
|
588
|
+
|
589
|
+
end
|
590
|
+
|
591
|
+
sorted_unique_array = ::Array::Sorted::Unique::SubMockPreGet.new
|
592
|
+
|
593
|
+
sorted_unique_array.push( :some_value )
|
594
|
+
sorted_unique_array[ 0 ].should == nil
|
595
|
+
|
596
|
+
sorted_unique_array.should == [ :some_value ]
|
597
|
+
|
598
|
+
end
|
599
|
+
|
600
|
+
###################
|
601
|
+
# post_get_hook #
|
602
|
+
###################
|
603
|
+
|
604
|
+
it 'has a hook that is called after getting a value' do
|
605
|
+
|
606
|
+
class ::Array::Sorted::Unique::SubMockPostGet < ::Array::Sorted::Unique
|
607
|
+
|
608
|
+
def post_get_hook( index, object )
|
609
|
+
return :some_other_value
|
610
|
+
end
|
611
|
+
|
612
|
+
end
|
613
|
+
|
614
|
+
sorted_unique_array = ::Array::Sorted::Unique::SubMockPostGet.new
|
615
|
+
|
616
|
+
sorted_unique_array.push( :some_value )
|
617
|
+
sorted_unique_array[ 0 ].should == :some_other_value
|
618
|
+
|
619
|
+
sorted_unique_array.should == [ :some_value ]
|
620
|
+
|
621
|
+
end
|
622
|
+
|
623
|
+
#####################
|
624
|
+
# pre_delete_hook #
|
625
|
+
#####################
|
626
|
+
|
627
|
+
it 'has a hook that is called before deleting an index; if return value is false, delete does not occur' do
|
628
|
+
|
629
|
+
class ::Array::Sorted::Unique::SubMockPreDelete < ::Array::Sorted::Unique
|
630
|
+
|
631
|
+
def pre_delete_hook( index )
|
632
|
+
return false
|
633
|
+
end
|
634
|
+
|
635
|
+
end
|
636
|
+
|
637
|
+
sorted_unique_array = ::Array::Sorted::Unique::SubMockPreDelete.new
|
638
|
+
|
639
|
+
sorted_unique_array.push( :some_value )
|
640
|
+
sorted_unique_array.delete_at( 0 )
|
641
|
+
|
642
|
+
sorted_unique_array.should == [ :some_value ]
|
643
|
+
|
644
|
+
end
|
645
|
+
|
646
|
+
######################
|
647
|
+
# post_delete_hook #
|
648
|
+
######################
|
649
|
+
|
650
|
+
it 'has a hook that is called after deleting an index' do
|
651
|
+
|
652
|
+
class ::Array::Sorted::Unique::SubMockPostDelete < ::Array::Sorted::Unique
|
653
|
+
|
654
|
+
def post_delete_hook( index, object )
|
655
|
+
return :some_other_value
|
656
|
+
end
|
657
|
+
|
658
|
+
end
|
659
|
+
|
660
|
+
sorted_unique_array = ::Array::Sorted::Unique::SubMockPostDelete.new
|
661
|
+
|
662
|
+
sorted_unique_array.push( :some_value )
|
663
|
+
sorted_unique_array.delete_at( 0 ).should == :some_other_value
|
664
|
+
|
665
|
+
sorted_unique_array.should == [ ]
|
666
|
+
|
667
|
+
end
|
668
|
+
|
669
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: array-sorted-unique
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Asher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: array-sorted
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: array-unique
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: A subclass of Array::Sorted and Array::Unique (and therefore also Array::Hooked).
|
47
|
+
email: asher@ridiculouspower.com
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- lib/array/hooked/sorted/unique.rb
|
53
|
+
- lib/array/hooked/unique/sorted.rb
|
54
|
+
- lib/array/sorted/namespaces.rb
|
55
|
+
- lib/array/sorted/requires.rb
|
56
|
+
- lib/array/sorted/unique/array_interface.rb
|
57
|
+
- lib/array/sorted/unique.rb
|
58
|
+
- lib/array/unique/sorted.rb
|
59
|
+
- lib/array-sorted-unique.rb
|
60
|
+
- lib/sorted_array/unique/array_interface.rb
|
61
|
+
- lib/sorted_array/unique.rb
|
62
|
+
- spec/array/sorted/unique_spec.rb
|
63
|
+
- README.md
|
64
|
+
- CHANGELOG.rdoc
|
65
|
+
homepage: http://rubygems.org/gems/array-sorted-unique
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project: array-sorted-unique
|
85
|
+
rubygems_version: 1.8.23
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Provides Array::Sorted::Unique and its alias Array::Unique::Sorted as well
|
89
|
+
as UniqueSortedArray and its alias SortedUniqueArray.
|
90
|
+
test_files: []
|
91
|
+
has_rdoc:
|