sorted_array 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -3
- data/lib/sorted_array.rb +1 -0
- data/lib/sorted_array/reverse_sorter.rb +14 -0
- data/lib/sorted_array/version.rb +1 -1
- data/spec/reversed_sorter_spec.rb +32 -0
- metadata +7 -4
data/README.md
CHANGED
@@ -4,9 +4,11 @@ The gem is providing a class `SortedArray` which keeps sorted
|
|
4
4
|
after adding new items. When initializing a `SortedArray` you chose
|
5
5
|
a Sorter which must have a method `:sort`
|
6
6
|
|
7
|
-
|
7
|
+
Sorter-classes `DefaultSorter` and `ReverseSorter` are included.
|
8
8
|
|
9
9
|
The included `DefaultSorter` sorts a list of Objects by a given method-name.
|
10
|
+
The included `ReverseSorter` sorts a list of Objects by a given method-name and then reverses it.
|
11
|
+
|
10
12
|
|
11
13
|
## Installation
|
12
14
|
|
@@ -55,11 +57,14 @@ And a copy of the last coverage-report is at
|
|
55
57
|
OpenStruct.new( foo: 3 ),
|
56
58
|
OpenStruct.new( foo: 2 )
|
57
59
|
]
|
58
|
-
keep_sorted
|
59
|
-
|
60
|
+
keep_sorted = SortedArray.new( DefaultSorter.new(:foo), data )
|
61
|
+
keep_reversed = SortedArray.new( ReverseSorter.new(:foo), data )
|
62
|
+
keep_sorted << OpenStruct.new( foo: 0 )
|
63
|
+
keep_reversed << OpenStruct.new( foo: 0 )
|
60
64
|
|
61
65
|
# data.map(&:foo) => [1,3,2]
|
62
66
|
# keep_sorted.map(&:foo) => [0,1,2,3]
|
67
|
+
# keep_reversed.map(&:foo) => [3,2,1,0]
|
63
68
|
|
64
69
|
## Persistent
|
65
70
|
|
data/lib/sorted_array.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
module SortedArray
|
2
|
+
|
3
|
+
# Sorts and then reverse the array
|
4
|
+
class ReverseSorter < DefaultSorter
|
5
|
+
|
6
|
+
# @param [Object] array - must implement sort! and reverse!
|
7
|
+
# @return [Object] - the given object, sorted and reversed
|
8
|
+
def sort(array)
|
9
|
+
super
|
10
|
+
array.reverse!
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
data/lib/sorted_array/version.rb
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pstore'
|
3
|
+
|
4
|
+
describe SortedArray::ReverseSorter do
|
5
|
+
|
6
|
+
let(:sorter) { SortedArray::ReverseSorter.new(:foo) }
|
7
|
+
let(:items) { [
|
8
|
+
OpenStruct.new(foo: 1),
|
9
|
+
OpenStruct.new(foo: 3),
|
10
|
+
OpenStruct.new(foo: 2)
|
11
|
+
] }
|
12
|
+
let(:reversed) { items.sort{|a,b|a.foo <=> b.foo}.reverse }
|
13
|
+
let(:store) { PStore.new(SORTER_TEST_STORE) }
|
14
|
+
|
15
|
+
it 'sorts and reverse an array by a given sort-method' do
|
16
|
+
expect(sorter.sort(items)).to eq(reversed)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'stores the method-name to a PStore' do
|
20
|
+
sorter.should_receive(:marshal_dump)
|
21
|
+
store.transaction do |s|
|
22
|
+
s[:sorter] = sorter
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'loads the method-name and propper class from a PStore' do
|
27
|
+
_s = nil
|
28
|
+
store.transaction(true) { |r| _s = r[:sorter] }
|
29
|
+
expect(_s).to be_a SortedArray::ReverseSorter
|
30
|
+
expect(_s.method).to eq(:to_s)
|
31
|
+
end
|
32
|
+
end
|
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.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-06-
|
12
|
+
date: 2013-06-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -189,10 +189,12 @@ files:
|
|
189
189
|
- TODO.md
|
190
190
|
- lib/sorted_array.rb
|
191
191
|
- lib/sorted_array/default_sorter.rb
|
192
|
+
- lib/sorted_array/reverse_sorter.rb
|
192
193
|
- lib/sorted_array/sorted_array.rb
|
193
194
|
- lib/sorted_array/version.rb
|
194
195
|
- sorted_array.gemspec
|
195
196
|
- spec/default_sorter_spec.rb
|
197
|
+
- spec/reversed_sorter_spec.rb
|
196
198
|
- spec/sorted_array_spec.rb
|
197
199
|
- spec/spec_helper.rb
|
198
200
|
homepage: https://github.com/iboard/sorted_array
|
@@ -210,7 +212,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
210
212
|
version: '0'
|
211
213
|
segments:
|
212
214
|
- 0
|
213
|
-
hash:
|
215
|
+
hash: -3091631067158972171
|
214
216
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
215
217
|
none: false
|
216
218
|
requirements:
|
@@ -219,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
219
221
|
version: '0'
|
220
222
|
segments:
|
221
223
|
- 0
|
222
|
-
hash:
|
224
|
+
hash: -3091631067158972171
|
223
225
|
requirements: []
|
224
226
|
rubyforge_project:
|
225
227
|
rubygems_version: 1.8.25
|
@@ -228,6 +230,7 @@ specification_version: 3
|
|
228
230
|
summary: SortedArray.new { |a,b| a.foo <=> b.foo }
|
229
231
|
test_files:
|
230
232
|
- spec/default_sorter_spec.rb
|
233
|
+
- spec/reversed_sorter_spec.rb
|
231
234
|
- spec/sorted_array_spec.rb
|
232
235
|
- spec/spec_helper.rb
|
233
236
|
has_rdoc:
|