naturally 1.5.0 → 2.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.
- checksums.yaml +4 -4
- data/lib/naturally.rb +18 -1
- data/lib/naturally/version.rb +1 -1
- data/spec/naturally_spec.rb +22 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e92f4d1a615260c308166aeb8e444042a8fcac4
|
4
|
+
data.tar.gz: fce65d08b22e07b490ad58f11bfa08e0ccaad5dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9480f5bd22d47fd903b7714fd0cccf5b1f3e97a2dae758aefa770beaa557deba41b5af49491357f3f5a19de09e11a88a344988afad9276c73a6e51649ddfc84b
|
7
|
+
data.tar.gz: b71f7d4376eb1baced1dd2c7e027b7f25dec3aa62379878ddb8ddba15a4d8a761b43f0f687948a2b30ae3b86b1e1568bf1c49d17fd1ec51710fc75f5b9dad922
|
data/lib/naturally.rb
CHANGED
@@ -12,11 +12,16 @@ module Naturally
|
|
12
12
|
end
|
13
13
|
|
14
14
|
# Sort an array of objects "naturally" by a given attribute.
|
15
|
+
# If block is given, attribute is ignored and each object
|
16
|
+
# is yielded to the block to obtain the sort key.
|
15
17
|
#
|
16
18
|
# @param [Array<Object>] an_array the list of objects to sort.
|
17
19
|
# @param [Symbol] an_attribute the attribute by which to sort.
|
20
|
+
# @param [Block] &block a block that should evaluate to the
|
21
|
+
# sort key for the yielded object
|
18
22
|
# @return [Array<Object>] the objects in natural sort order.
|
19
|
-
def self.sort_by(an_array, an_attribute)
|
23
|
+
def self.sort_by(an_array, an_attribute=nil, &block)
|
24
|
+
return sort_by_block(an_array, &block) if block_given?
|
20
25
|
an_array.sort_by { |obj| normalize(obj.send(an_attribute)) }
|
21
26
|
end
|
22
27
|
|
@@ -35,4 +40,16 @@ module Naturally
|
|
35
40
|
tokens = complex_number.to_s.scan(/\p{Word}+/)
|
36
41
|
tokens.map { |t| Segment.new(t) }
|
37
42
|
end
|
43
|
+
|
44
|
+
private
|
45
|
+
# Sort an array of objects "naturally", yielding each object
|
46
|
+
# to the block to obtain the sort key.
|
47
|
+
#
|
48
|
+
# @param [Array<Object>] an_array the list of objects to sort.
|
49
|
+
# @param [Block] &block a block that should evaluate to the
|
50
|
+
# sort key for the yielded object
|
51
|
+
# @return [Array<Object>] the objects in natural sort order.
|
52
|
+
def self.sort_by_block(an_array, &block)
|
53
|
+
an_array.sort_by { |obj| normalize(yield(obj)) }
|
54
|
+
end
|
38
55
|
end
|
data/lib/naturally/version.rb
CHANGED
data/spec/naturally_spec.rb
CHANGED
@@ -153,4 +153,26 @@ describe Naturally do
|
|
153
153
|
]
|
154
154
|
end
|
155
155
|
end
|
156
|
+
|
157
|
+
describe '#sort_naturally_by_block' do
|
158
|
+
it 'sorts using a block' do
|
159
|
+
releases = [
|
160
|
+
{:name => 'Saucy Salamander', :version => '13.10'},
|
161
|
+
{:name => 'Raring Ringtail', :version => '13.04'},
|
162
|
+
{:name => 'Precise Pangolin', :version => '12.04.4'},
|
163
|
+
{:name => 'Maverick Meerkat', :version => '10.10'},
|
164
|
+
{:name => 'Quantal Quetzal', :version => '12.10'},
|
165
|
+
{:name => 'Lucid Lynx', :version => '10.04.4'}
|
166
|
+
]
|
167
|
+
actual = Naturally.sort_by(releases){|r| r[:version]}
|
168
|
+
expect(actual.map{|r| r[:name]}).to eq [
|
169
|
+
'Lucid Lynx',
|
170
|
+
'Maverick Meerkat',
|
171
|
+
'Precise Pangolin',
|
172
|
+
'Quantal Quetzal',
|
173
|
+
'Raring Ringtail',
|
174
|
+
'Saucy Salamander'
|
175
|
+
]
|
176
|
+
end
|
177
|
+
end
|
156
178
|
end
|