array-unique 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +13 -0
- data/README.md +44 -0
- data/lib/array-unique.rb +2 -0
- data/lib/array/hooked/unique.rb +2 -0
- data/lib/array/namespaces.rb +7 -0
- data/lib/array/requires.rb +26 -0
- data/lib/array/unique.rb +14 -0
- data/lib/array/unique/array_interface.rb +155 -0
- data/lib/unique_array.rb +6 -0
- data/lib/unique_array/array_interface.rb +9 -0
- data/spec/array/unique_spec.rb +676 -0
- metadata +72 -0
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
== 6/14/2012
|
3
|
+
|
4
|
+
Initial release - split from compositing-unique-array.
|
5
|
+
Fixed require path for hooked_array to gem.
|
6
|
+
|
7
|
+
== 6/15/2012
|
8
|
+
|
9
|
+
Fixed UniqueArray::Interface to include Array::Unique::Interface.
|
10
|
+
|
11
|
+
== 6/30/2012
|
12
|
+
|
13
|
+
Renamed from unique-array to array-unique. File schema updated to reflect gem name.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Unique Array #
|
2
|
+
|
3
|
+
http://rubygems.org/gems/array-unique
|
4
|
+
|
5
|
+
# Description #
|
6
|
+
|
7
|
+
Provides ::Array::Unique and ::UniqueArray.
|
8
|
+
|
9
|
+
# Summary #
|
10
|
+
|
11
|
+
A subclass of Array::Hooked that also keeps array unique.
|
12
|
+
|
13
|
+
# Install #
|
14
|
+
|
15
|
+
* sudo gem install array-unique
|
16
|
+
|
17
|
+
# Usage #
|
18
|
+
|
19
|
+
Just like Array or Array::Hooked (provided by hooked_array)! Automatically keeps array unique.
|
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.
|
data/lib/array-unique.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
basepath = 'unique'
|
3
|
+
|
4
|
+
files = [
|
5
|
+
|
6
|
+
'array_interface'
|
7
|
+
|
8
|
+
]
|
9
|
+
|
10
|
+
second_basepath = '../unique_array'
|
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' )
|
data/lib/array/unique.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
|
2
|
+
require 'array/hooked'
|
3
|
+
|
4
|
+
# namespaces that have to be declared ahead of time for proper load order
|
5
|
+
require_relative './namespaces'
|
6
|
+
|
7
|
+
# source file requires
|
8
|
+
require_relative './requires.rb'
|
9
|
+
|
10
|
+
class ::Array::Unique < ::Array::Hooked
|
11
|
+
|
12
|
+
include ::Array::Unique::Interface
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
|
2
|
+
module ::Array::Unique::Interface
|
3
|
+
|
4
|
+
include ::Array::Hooked::ArrayInterface
|
5
|
+
|
6
|
+
instances_identify_as!( ::Array::Unique )
|
7
|
+
|
8
|
+
################
|
9
|
+
# initialize #
|
10
|
+
################
|
11
|
+
|
12
|
+
def initialize( *args )
|
13
|
+
|
14
|
+
@unique_keys = { }
|
15
|
+
|
16
|
+
super
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
##################################### Self Management ##########################################
|
21
|
+
|
22
|
+
#########
|
23
|
+
# []= #
|
24
|
+
#########
|
25
|
+
|
26
|
+
def []=( index, object )
|
27
|
+
|
28
|
+
# we only set if we don't already have the object being inserted
|
29
|
+
# for this reason we return nil if no insert occurred
|
30
|
+
|
31
|
+
return_value = nil
|
32
|
+
|
33
|
+
# make sure that set is unique
|
34
|
+
unless @unique_keys.has_key?( object )
|
35
|
+
|
36
|
+
@unique_keys[ object ] = true
|
37
|
+
|
38
|
+
return_value = super
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
return return_value
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
############
|
47
|
+
# insert #
|
48
|
+
############
|
49
|
+
|
50
|
+
def insert( index, *objects )
|
51
|
+
|
52
|
+
# we only insert if we don't already have the object being inserted
|
53
|
+
# for this reason we return nil if no insert occurred
|
54
|
+
|
55
|
+
# if we have less elements in self than the index we are inserting at
|
56
|
+
# we need to make sure the nils inserted cascade
|
57
|
+
if index > count
|
58
|
+
if @unique_keys.has_key?( nil )
|
59
|
+
index -= ( index - count + 1 )
|
60
|
+
else
|
61
|
+
objects.unshift( nil )
|
62
|
+
index -= ( index - count )
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# get rid of objects already inserted
|
67
|
+
indexes_to_delete = [ ]
|
68
|
+
objects.each_with_index do |this_object, index|
|
69
|
+
if @unique_keys.has_key?( this_object )
|
70
|
+
indexes_to_delete.push( index )
|
71
|
+
else
|
72
|
+
@unique_keys[ this_object ] = true
|
73
|
+
end
|
74
|
+
end
|
75
|
+
indexes_to_delete.sort.reverse.each do |this_index|
|
76
|
+
objects.delete_at( this_index )
|
77
|
+
end
|
78
|
+
|
79
|
+
return nil if objects.empty?
|
80
|
+
|
81
|
+
return super
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
##################################
|
86
|
+
# perform_delete_between_hooks #
|
87
|
+
##################################
|
88
|
+
|
89
|
+
def perform_delete_between_hooks( index )
|
90
|
+
|
91
|
+
@unique_keys.delete( self[ index ] )
|
92
|
+
|
93
|
+
return super
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
##############
|
98
|
+
# include? #
|
99
|
+
##############
|
100
|
+
|
101
|
+
def include?( object )
|
102
|
+
|
103
|
+
return @unique_keys.has_key?( object )
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
##############
|
108
|
+
# collect! #
|
109
|
+
# map! #
|
110
|
+
##############
|
111
|
+
|
112
|
+
def collect!
|
113
|
+
|
114
|
+
return to_enum unless block_given?
|
115
|
+
|
116
|
+
delete_indexes = [ ]
|
117
|
+
self.each_with_index do |this_object, index|
|
118
|
+
replacement_object = yield( this_object )
|
119
|
+
if @unique_keys.has_key?( replacement_object ) and replacement_object != this_object
|
120
|
+
delete_indexes.push( index )
|
121
|
+
else
|
122
|
+
self[ index ] = replacement_object
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
delete_indexes.sort.reverse.each do |this_index|
|
127
|
+
delete_at( this_index )
|
128
|
+
end
|
129
|
+
|
130
|
+
return self
|
131
|
+
|
132
|
+
end
|
133
|
+
alias_method :map!, :collect!
|
134
|
+
|
135
|
+
##########
|
136
|
+
# uniq #
|
137
|
+
##########
|
138
|
+
|
139
|
+
def uniq
|
140
|
+
|
141
|
+
return self
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
###########
|
146
|
+
# uniq! #
|
147
|
+
###########
|
148
|
+
|
149
|
+
def uniq!
|
150
|
+
|
151
|
+
return self
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
data/lib/unique_array.rb
ADDED
@@ -0,0 +1,676 @@
|
|
1
|
+
|
2
|
+
require_relative '../../lib/array-unique.rb'
|
3
|
+
|
4
|
+
describe ::Array::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
|
+
unique_array = ::Array::Unique.new
|
13
|
+
|
14
|
+
unique_array.instance_variable_get( :@parent_composite_object ).should == nil
|
15
|
+
unique_array.should == []
|
16
|
+
unique_array.push( :A, :B, :C, :D )
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
#########
|
21
|
+
# []= #
|
22
|
+
#########
|
23
|
+
|
24
|
+
it 'can add elements' do
|
25
|
+
|
26
|
+
unique_array = ::Array::Unique.new
|
27
|
+
|
28
|
+
unique_array[ 0 ] = :A
|
29
|
+
unique_array.should == [ :A ]
|
30
|
+
|
31
|
+
unique_array[ 1 ] = :B
|
32
|
+
unique_array.should == [ :A, :B ]
|
33
|
+
|
34
|
+
unique_array[ 2 ] = :D
|
35
|
+
unique_array.should == [ :A, :B, :D ]
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
############
|
40
|
+
# insert #
|
41
|
+
############
|
42
|
+
|
43
|
+
it 'can insert elements' do
|
44
|
+
|
45
|
+
unique_array = ::Array::Unique.new
|
46
|
+
|
47
|
+
unique_array.insert( 3, :D )
|
48
|
+
unique_array.should == [ nil, :D ]
|
49
|
+
|
50
|
+
unique_array.insert( 1, :B )
|
51
|
+
unique_array.should == [ nil, :B, :D ]
|
52
|
+
|
53
|
+
unique_array.insert( 2, :C )
|
54
|
+
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
|
+
unique_array = ::Array::Unique.new
|
66
|
+
|
67
|
+
unique_array << :A
|
68
|
+
unique_array.should == [ :A ]
|
69
|
+
|
70
|
+
unique_array << :B
|
71
|
+
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
|
+
unique_array = ::Array::Unique.new
|
86
|
+
|
87
|
+
unique_array.concat( [ :A ] )
|
88
|
+
unique_array.should == [ :A ]
|
89
|
+
|
90
|
+
unique_array += [ :B ]
|
91
|
+
unique_array.should == [ :A, :B ]
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
####################
|
96
|
+
# delete_objects #
|
97
|
+
####################
|
98
|
+
|
99
|
+
it 'can delete multiple elements' do
|
100
|
+
|
101
|
+
unique_array = ::Array::Unique.new
|
102
|
+
|
103
|
+
unique_array += [ :A, :B ]
|
104
|
+
unique_array.should == [ :A, :B ]
|
105
|
+
|
106
|
+
unique_array.delete_objects( :A, :B )
|
107
|
+
unique_array.should == [ ]
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
#######
|
112
|
+
# - #
|
113
|
+
#######
|
114
|
+
|
115
|
+
it 'can exclude elements' do
|
116
|
+
|
117
|
+
unique_array = ::Array::Unique.new
|
118
|
+
|
119
|
+
unique_array.push( :A )
|
120
|
+
unique_array.should == [ :A ]
|
121
|
+
|
122
|
+
unique_array -= [ :A ]
|
123
|
+
unique_array.should == [ ]
|
124
|
+
|
125
|
+
unique_array.push( :B )
|
126
|
+
unique_array.should == [ :B ]
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
############
|
131
|
+
# delete #
|
132
|
+
############
|
133
|
+
|
134
|
+
it 'can delete elements' do
|
135
|
+
|
136
|
+
unique_array = ::Array::Unique.new
|
137
|
+
|
138
|
+
unique_array.push( :A )
|
139
|
+
unique_array.should == [ :A ]
|
140
|
+
|
141
|
+
unique_array.delete( :A )
|
142
|
+
unique_array.should == [ ]
|
143
|
+
|
144
|
+
unique_array.push( :B )
|
145
|
+
unique_array.should == [ :B ]
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
###############
|
150
|
+
# delete_at #
|
151
|
+
###############
|
152
|
+
|
153
|
+
it 'can delete by indexes' do
|
154
|
+
|
155
|
+
unique_array = ::Array::Unique.new
|
156
|
+
|
157
|
+
unique_array.push( :A )
|
158
|
+
unique_array.should == [ :A ]
|
159
|
+
|
160
|
+
unique_array.delete_at( 0 )
|
161
|
+
unique_array.should == [ ]
|
162
|
+
|
163
|
+
unique_array.push( :B )
|
164
|
+
unique_array.should == [ :B ]
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
#######################
|
169
|
+
# delete_at_indexes #
|
170
|
+
#######################
|
171
|
+
|
172
|
+
it 'can delete by indexes' do
|
173
|
+
|
174
|
+
unique_array = ::Array::Unique.new
|
175
|
+
|
176
|
+
unique_array.push( :A, :B, :C )
|
177
|
+
unique_array.should == [ :A, :B, :C ]
|
178
|
+
|
179
|
+
unique_array.delete_at_indexes( 0, 1 )
|
180
|
+
unique_array.should == [ :C ]
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
###############
|
185
|
+
# delete_if #
|
186
|
+
###############
|
187
|
+
|
188
|
+
it 'can delete by block' do
|
189
|
+
|
190
|
+
unique_array = ::Array::Unique.new
|
191
|
+
|
192
|
+
unique_array.push( :A, :B, :C )
|
193
|
+
unique_array.should == [ :A, :B, :C ]
|
194
|
+
unique_array.delete_if do |object|
|
195
|
+
object != :C
|
196
|
+
end
|
197
|
+
unique_array.should == [ :C ]
|
198
|
+
|
199
|
+
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
|
+
unique_array = ::Array::Unique.new
|
210
|
+
|
211
|
+
unique_array.push( :A, :B, :C )
|
212
|
+
unique_array.should == [ :A, :B, :C ]
|
213
|
+
unique_array.keep_if do |object|
|
214
|
+
object == :C
|
215
|
+
end
|
216
|
+
unique_array.should == [ :C ]
|
217
|
+
|
218
|
+
end
|
219
|
+
|
220
|
+
##############
|
221
|
+
# compact! #
|
222
|
+
##############
|
223
|
+
|
224
|
+
it 'can compact' do
|
225
|
+
|
226
|
+
unique_array = ::Array::Unique.new
|
227
|
+
|
228
|
+
unique_array.push( :A, nil, :B, nil, :C, nil )
|
229
|
+
unique_array.should == [ :A, nil, :B, :C ]
|
230
|
+
unique_array.compact!
|
231
|
+
unique_array.should == [ :A, :B, :C ]
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
##############
|
236
|
+
# flatten! #
|
237
|
+
##############
|
238
|
+
|
239
|
+
it 'can flatten' do
|
240
|
+
|
241
|
+
unique_array = ::Array::Unique.new
|
242
|
+
|
243
|
+
unique_array.push( :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] )
|
244
|
+
unique_array.should == [ :A, [ :F_A, :F_B ], :B, [ :F_C ], :C, [ :F_D ], [ :F_E ] ]
|
245
|
+
unique_array.flatten!
|
246
|
+
unique_array.should == [ :A, :F_A, :F_B, :B, :F_C, :C, :F_D, :F_E ]
|
247
|
+
|
248
|
+
end
|
249
|
+
|
250
|
+
#############
|
251
|
+
# reject! #
|
252
|
+
#############
|
253
|
+
|
254
|
+
it 'can reject' do
|
255
|
+
|
256
|
+
unique_array = ::Array::Unique.new
|
257
|
+
|
258
|
+
unique_array.push( :A, :B, :C )
|
259
|
+
unique_array.should == [ :A, :B, :C ]
|
260
|
+
unique_array.reject! do |object|
|
261
|
+
object != :C
|
262
|
+
end
|
263
|
+
unique_array.should == [ :C ]
|
264
|
+
|
265
|
+
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
|
+
unique_array = ::Array::Unique.new
|
276
|
+
|
277
|
+
unique_array.push( :A, :B, :C )
|
278
|
+
unique_array.should == [ :A, :B, :C ]
|
279
|
+
unique_array.replace( [ :D, :E, :F ] )
|
280
|
+
unique_array.should == [ :D, :E, :F ]
|
281
|
+
|
282
|
+
end
|
283
|
+
|
284
|
+
##############
|
285
|
+
# reverse! #
|
286
|
+
##############
|
287
|
+
|
288
|
+
it 'can reverse self' do
|
289
|
+
|
290
|
+
unique_array = ::Array::Unique.new
|
291
|
+
|
292
|
+
unique_array.push( :A, :B, :C )
|
293
|
+
unique_array.should == [ :A, :B, :C ]
|
294
|
+
unique_array.reverse!
|
295
|
+
unique_array.should == [ :C, :B, :A ]
|
296
|
+
|
297
|
+
end
|
298
|
+
|
299
|
+
#############
|
300
|
+
# rotate! #
|
301
|
+
#############
|
302
|
+
|
303
|
+
it 'can rotate self' do
|
304
|
+
|
305
|
+
unique_array = ::Array::Unique.new
|
306
|
+
|
307
|
+
unique_array.push( :A, :B, :C )
|
308
|
+
unique_array.should == [ :A, :B, :C ]
|
309
|
+
|
310
|
+
unique_array.rotate!
|
311
|
+
unique_array.should == [ :B, :C, :A ]
|
312
|
+
|
313
|
+
unique_array.rotate!( -1 )
|
314
|
+
unique_array.should == [ :A, :B, :C ]
|
315
|
+
|
316
|
+
end
|
317
|
+
|
318
|
+
#############
|
319
|
+
# select! #
|
320
|
+
#############
|
321
|
+
|
322
|
+
it 'can keep by select' do
|
323
|
+
|
324
|
+
unique_array = ::Array::Unique.new
|
325
|
+
|
326
|
+
unique_array.push( :A, :B, :C )
|
327
|
+
unique_array.should == [ :A, :B, :C ]
|
328
|
+
unique_array.select! do |object|
|
329
|
+
object == :C
|
330
|
+
end
|
331
|
+
unique_array.should == [ :C ]
|
332
|
+
|
333
|
+
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
|
+
unique_array = ::Array::Unique.new
|
344
|
+
|
345
|
+
unique_array.push( :A, :B, :C )
|
346
|
+
unique_array.should == [ :A, :B, :C ]
|
347
|
+
|
348
|
+
prior_version = unique_array.dup
|
349
|
+
attempts = [ ]
|
350
|
+
50.times do
|
351
|
+
unique_array.shuffle!
|
352
|
+
attempts.push( unique_array == prior_version )
|
353
|
+
prior_version = unique_array.dup
|
354
|
+
end
|
355
|
+
attempts_correct = attempts.select { |member| member == false }.count
|
356
|
+
( attempts_correct >= 10 ).should == true
|
357
|
+
first_shuffle_version = unique_array
|
358
|
+
|
359
|
+
end
|
360
|
+
|
361
|
+
##############
|
362
|
+
# collect! #
|
363
|
+
# map! #
|
364
|
+
##############
|
365
|
+
|
366
|
+
it 'can replace by collect/map' do
|
367
|
+
|
368
|
+
unique_array = ::Array::Unique.new
|
369
|
+
|
370
|
+
unique_array.push( :A, :B, :C )
|
371
|
+
unique_array.should == [ :A, :B, :C ]
|
372
|
+
unique_array.collect! do |object|
|
373
|
+
:C
|
374
|
+
end
|
375
|
+
unique_array.should == [ :C ]
|
376
|
+
|
377
|
+
unique_array.collect!.is_a?( Enumerator ).should == true
|
378
|
+
|
379
|
+
end
|
380
|
+
|
381
|
+
###########
|
382
|
+
# sort! #
|
383
|
+
###########
|
384
|
+
|
385
|
+
it 'can replace by collect/map' do
|
386
|
+
|
387
|
+
unique_array = ::Array::Unique.new
|
388
|
+
|
389
|
+
unique_array.push( :A, :B, :C )
|
390
|
+
unique_array.should == [ :A, :B, :C ]
|
391
|
+
unique_array.sort! do |a, b|
|
392
|
+
if a < b
|
393
|
+
1
|
394
|
+
elsif a > b
|
395
|
+
-1
|
396
|
+
elsif a == b
|
397
|
+
0
|
398
|
+
end
|
399
|
+
end
|
400
|
+
unique_array.should == [ :C, :B, :A ]
|
401
|
+
|
402
|
+
unique_array.sort!
|
403
|
+
unique_array.should == [ :A, :B, :C ]
|
404
|
+
|
405
|
+
end
|
406
|
+
|
407
|
+
##############
|
408
|
+
# sort_by! #
|
409
|
+
##############
|
410
|
+
|
411
|
+
it 'can replace by collect/map' do
|
412
|
+
|
413
|
+
unique_array = ::Array::Unique.new
|
414
|
+
|
415
|
+
unique_array.push( :A, :B, :C )
|
416
|
+
unique_array.should == [ :A, :B, :C ]
|
417
|
+
unique_array.sort_by! do |object|
|
418
|
+
case object
|
419
|
+
when :A
|
420
|
+
:B
|
421
|
+
when :B
|
422
|
+
:A
|
423
|
+
when :C
|
424
|
+
:C
|
425
|
+
end
|
426
|
+
end
|
427
|
+
unique_array.should == [ :B, :A, :C ]
|
428
|
+
|
429
|
+
unique_array.sort_by!.is_a?( Enumerator ).should == true
|
430
|
+
|
431
|
+
end
|
432
|
+
|
433
|
+
###########
|
434
|
+
# uniq! #
|
435
|
+
###########
|
436
|
+
|
437
|
+
it 'can remove non-unique elements' do
|
438
|
+
|
439
|
+
unique_array = ::Array::Unique.new
|
440
|
+
|
441
|
+
unique_array.push( :A, :B, :C, :C, :C, :B, :A )
|
442
|
+
unique_array.should == [ :A, :B, :C ]
|
443
|
+
unique_array.uniq!
|
444
|
+
unique_array.should == [ :A, :B, :C ]
|
445
|
+
|
446
|
+
end
|
447
|
+
|
448
|
+
#############
|
449
|
+
# unshift #
|
450
|
+
#############
|
451
|
+
|
452
|
+
it 'can unshift onto the first element' do
|
453
|
+
|
454
|
+
unique_array = ::Array::Unique.new
|
455
|
+
|
456
|
+
unique_array += :A
|
457
|
+
unique_array.should == [ :A ]
|
458
|
+
|
459
|
+
unique_array.unshift( :B )
|
460
|
+
unique_array.should == [ :B, :A ]
|
461
|
+
|
462
|
+
end
|
463
|
+
|
464
|
+
#########
|
465
|
+
# pop #
|
466
|
+
#########
|
467
|
+
|
468
|
+
it 'can pop the final element' do
|
469
|
+
|
470
|
+
unique_array = ::Array::Unique.new
|
471
|
+
|
472
|
+
unique_array += :A
|
473
|
+
unique_array.should == [ :A ]
|
474
|
+
|
475
|
+
unique_array.pop.should == :A
|
476
|
+
unique_array.should == [ ]
|
477
|
+
|
478
|
+
unique_array += :B
|
479
|
+
unique_array.should == [ :B ]
|
480
|
+
|
481
|
+
end
|
482
|
+
|
483
|
+
###########
|
484
|
+
# shift #
|
485
|
+
###########
|
486
|
+
|
487
|
+
it 'can shift the first element' do
|
488
|
+
|
489
|
+
unique_array = ::Array::Unique.new
|
490
|
+
|
491
|
+
unique_array += :A
|
492
|
+
unique_array.should == [ :A ]
|
493
|
+
|
494
|
+
unique_array.shift.should == :A
|
495
|
+
unique_array.should == [ ]
|
496
|
+
|
497
|
+
unique_array += :B
|
498
|
+
unique_array.should == [ :B ]
|
499
|
+
|
500
|
+
end
|
501
|
+
|
502
|
+
############
|
503
|
+
# slice! #
|
504
|
+
############
|
505
|
+
|
506
|
+
it 'can slice elements' do
|
507
|
+
|
508
|
+
unique_array = ::Array::Unique.new
|
509
|
+
|
510
|
+
unique_array += :A
|
511
|
+
unique_array.should == [ :A ]
|
512
|
+
|
513
|
+
unique_array.slice!( 0, 1 ).should == [ :A ]
|
514
|
+
unique_array.should == [ ]
|
515
|
+
|
516
|
+
unique_array += :B
|
517
|
+
unique_array.should == [ :B ]
|
518
|
+
|
519
|
+
end
|
520
|
+
|
521
|
+
###########
|
522
|
+
# clear #
|
523
|
+
###########
|
524
|
+
|
525
|
+
it 'can clear, causing present elements to be excluded' do
|
526
|
+
|
527
|
+
unique_array = ::Array::Unique.new
|
528
|
+
|
529
|
+
unique_array += :A
|
530
|
+
unique_array.should == [ :A ]
|
531
|
+
|
532
|
+
unique_array.clear
|
533
|
+
unique_array.should == [ ]
|
534
|
+
|
535
|
+
unique_array += :B
|
536
|
+
unique_array.should == [ :B ]
|
537
|
+
|
538
|
+
end
|
539
|
+
|
540
|
+
##################
|
541
|
+
# pre_set_hook #
|
542
|
+
##################
|
543
|
+
|
544
|
+
it 'has a hook that is called before setting a value; return value is used in place of object' do
|
545
|
+
|
546
|
+
class ::Array::Unique::SubMockPreSet < ::Array::Hooked
|
547
|
+
|
548
|
+
def pre_set_hook( index, object, is_insert = false )
|
549
|
+
return :some_other_value
|
550
|
+
end
|
551
|
+
|
552
|
+
end
|
553
|
+
|
554
|
+
unique_array = ::Array::Unique::SubMockPreSet.new
|
555
|
+
|
556
|
+
unique_array.push( :some_value )
|
557
|
+
|
558
|
+
unique_array.should == [ :some_other_value ]
|
559
|
+
|
560
|
+
end
|
561
|
+
|
562
|
+
###################
|
563
|
+
# post_set_hook #
|
564
|
+
###################
|
565
|
+
|
566
|
+
it 'has a hook that is called after setting a value' do
|
567
|
+
|
568
|
+
class ::Array::Unique::SubMockPostSet < ::Array::Hooked
|
569
|
+
|
570
|
+
def post_set_hook( index, object, is_insert = false )
|
571
|
+
return :some_other_value
|
572
|
+
end
|
573
|
+
|
574
|
+
end
|
575
|
+
|
576
|
+
unique_array = ::Array::Unique::SubMockPostSet.new
|
577
|
+
|
578
|
+
unique_array.push( :some_value ).should == [ :some_other_value ]
|
579
|
+
|
580
|
+
unique_array.should == [ :some_value ]
|
581
|
+
|
582
|
+
end
|
583
|
+
|
584
|
+
##################
|
585
|
+
# pre_get_hook #
|
586
|
+
##################
|
587
|
+
|
588
|
+
it 'has a hook that is called before getting a value; if return value is false, get does not occur' do
|
589
|
+
|
590
|
+
class ::Array::Unique::SubMockPreGet < ::Array::Hooked
|
591
|
+
|
592
|
+
def pre_get_hook( index )
|
593
|
+
return false
|
594
|
+
end
|
595
|
+
|
596
|
+
end
|
597
|
+
|
598
|
+
unique_array = ::Array::Unique::SubMockPreGet.new
|
599
|
+
|
600
|
+
unique_array.push( :some_value )
|
601
|
+
unique_array[ 0 ].should == nil
|
602
|
+
|
603
|
+
unique_array.should == [ :some_value ]
|
604
|
+
|
605
|
+
end
|
606
|
+
|
607
|
+
###################
|
608
|
+
# post_get_hook #
|
609
|
+
###################
|
610
|
+
|
611
|
+
it 'has a hook that is called after getting a value' do
|
612
|
+
|
613
|
+
class ::Array::Unique::SubMockPostGet < ::Array::Hooked
|
614
|
+
|
615
|
+
def post_get_hook( index, object )
|
616
|
+
return :some_other_value
|
617
|
+
end
|
618
|
+
|
619
|
+
end
|
620
|
+
|
621
|
+
unique_array = ::Array::Unique::SubMockPostGet.new
|
622
|
+
|
623
|
+
unique_array.push( :some_value )
|
624
|
+
unique_array[ 0 ].should == :some_other_value
|
625
|
+
|
626
|
+
unique_array.should == [ :some_value ]
|
627
|
+
|
628
|
+
end
|
629
|
+
|
630
|
+
#####################
|
631
|
+
# pre_delete_hook #
|
632
|
+
#####################
|
633
|
+
|
634
|
+
it 'has a hook that is called before deleting an index; if return value is false, delete does not occur' do
|
635
|
+
|
636
|
+
class ::Array::Unique::SubMockPreDelete < ::Array::Hooked
|
637
|
+
|
638
|
+
def pre_delete_hook( index )
|
639
|
+
return false
|
640
|
+
end
|
641
|
+
|
642
|
+
end
|
643
|
+
|
644
|
+
unique_array = ::Array::Unique::SubMockPreDelete.new
|
645
|
+
|
646
|
+
unique_array.push( :some_value )
|
647
|
+
unique_array.delete_at( 0 )
|
648
|
+
|
649
|
+
unique_array.should == [ :some_value ]
|
650
|
+
|
651
|
+
end
|
652
|
+
|
653
|
+
######################
|
654
|
+
# post_delete_hook #
|
655
|
+
######################
|
656
|
+
|
657
|
+
it 'has a hook that is called after deleting an index' do
|
658
|
+
|
659
|
+
class ::Array::Unique::SubMockPostDelete < ::Array::Hooked
|
660
|
+
|
661
|
+
def post_delete_hook( index, object )
|
662
|
+
return :some_other_value
|
663
|
+
end
|
664
|
+
|
665
|
+
end
|
666
|
+
|
667
|
+
unique_array = ::Array::Unique::SubMockPostDelete.new
|
668
|
+
|
669
|
+
unique_array.push( :some_value )
|
670
|
+
unique_array.delete_at( 0 ).should == :some_other_value
|
671
|
+
|
672
|
+
unique_array.should == [ ]
|
673
|
+
|
674
|
+
end
|
675
|
+
|
676
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: array-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-hooked
|
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
|
+
description: A subclass of Array::Hooked that also keeps array unique.
|
31
|
+
email: asher@ridiculouspower.com
|
32
|
+
executables: []
|
33
|
+
extensions: []
|
34
|
+
extra_rdoc_files: []
|
35
|
+
files:
|
36
|
+
- lib/array/hooked/unique.rb
|
37
|
+
- lib/array/namespaces.rb
|
38
|
+
- lib/array/requires.rb
|
39
|
+
- lib/array/unique/array_interface.rb
|
40
|
+
- lib/array/unique.rb
|
41
|
+
- lib/array-unique.rb
|
42
|
+
- lib/unique_array/array_interface.rb
|
43
|
+
- lib/unique_array.rb
|
44
|
+
- spec/array/unique_spec.rb
|
45
|
+
- README.md
|
46
|
+
- CHANGELOG.rdoc
|
47
|
+
homepage: http://rubygems.org/gems/array-unique
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project: array-unique
|
67
|
+
rubygems_version: 1.8.23
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Provides Array::Unique and UniqueArray.
|
71
|
+
test_files: []
|
72
|
+
has_rdoc:
|