sorted_array 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sorted_array/sorted_array.rb +65 -0
- data/lib/sorted_array/version.rb +1 -1
- metadata +4 -3
@@ -0,0 +1,65 @@
|
|
1
|
+
module SortedArray
|
2
|
+
|
3
|
+
# A SortedArray takes a Sorter-object as a parameter.
|
4
|
+
# The array gets sorted after each push,<<,unsift.
|
5
|
+
# It defines marshal-methods and can be used with PStore
|
6
|
+
# @see DefaultSorter
|
7
|
+
class SortedArray < Array
|
8
|
+
|
9
|
+
# @params [Array] args the first argument is a Sorter-object
|
10
|
+
# @example
|
11
|
+
# my_array = SortedArray.new( DefaultSorter.new(:foo) )
|
12
|
+
# my_array << AnyClassWhichSupportsFoo.new(1)
|
13
|
+
# my_array << AnyClassWhichSupportsFoo.new(3)
|
14
|
+
# my_array << AnyClassWhichSupportsFoo.new(2)
|
15
|
+
# # my_array.map(&:foo) => 1,2,3
|
16
|
+
# @see DefaultSorter
|
17
|
+
# @see Array
|
18
|
+
def initialize *args
|
19
|
+
@sorter = args.shift
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
23
|
+
# @see Array
|
24
|
+
def push *other
|
25
|
+
super
|
26
|
+
@sorter.sort(self)
|
27
|
+
end
|
28
|
+
|
29
|
+
# @see Array
|
30
|
+
def unshift other
|
31
|
+
super
|
32
|
+
@sorter.sort(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
# @see Array
|
36
|
+
def << other
|
37
|
+
push *other
|
38
|
+
@sorter.sort(self)
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
|
43
|
+
# @return [Array] - Sorter-object or nil, [...values...]
|
44
|
+
def marshal_dump
|
45
|
+
[@sorter ? @sorter.marshal_dump : nil, to_a]
|
46
|
+
end
|
47
|
+
|
48
|
+
# Initialize sorter and read in data
|
49
|
+
# @param [Array] array - _sorter, [..entries..]
|
50
|
+
def marshal_load array
|
51
|
+
_sorter, _data = *array
|
52
|
+
if _sorter
|
53
|
+
initialize_sorter _sorter
|
54
|
+
push *_data if _data
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def initialize_sorter args
|
61
|
+
@sorter = args.shift.new *args
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
data/lib/sorted_array/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sorted_array
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -189,6 +189,7 @@ files:
|
|
189
189
|
- TODO.md
|
190
190
|
- lib/sorted_array.rb
|
191
191
|
- lib/sorted_array/default_sorter.rb
|
192
|
+
- lib/sorted_array/sorted_array.rb
|
192
193
|
- lib/sorted_array/version.rb
|
193
194
|
- sorted_array.gemspec
|
194
195
|
- spec/default_sorter_spec.rb
|
@@ -209,7 +210,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
209
210
|
version: '0'
|
210
211
|
segments:
|
211
212
|
- 0
|
212
|
-
hash:
|
213
|
+
hash: 1235662549708378242
|
213
214
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
214
215
|
none: false
|
215
216
|
requirements:
|
@@ -218,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
218
219
|
version: '0'
|
219
220
|
segments:
|
220
221
|
- 0
|
221
|
-
hash:
|
222
|
+
hash: 1235662549708378242
|
222
223
|
requirements: []
|
223
224
|
rubyforge_project:
|
224
225
|
rubygems_version: 1.8.25
|