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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c21d347e9e1658f3ebc10a896f5b96aa5192b00
4
- data.tar.gz: 0d830b3bbb12d2a6debb452a421ce641f4062f6a
3
+ metadata.gz: 4e92f4d1a615260c308166aeb8e444042a8fcac4
4
+ data.tar.gz: fce65d08b22e07b490ad58f11bfa08e0ccaad5dc
5
5
  SHA512:
6
- metadata.gz: 1ed7567011f553a3bfae3ce97f1d9642bc3f828691a6d6b0a2ad999fd3d72117a4f9736d958e30964ec50304b733ecbe4ed501df142e73c85765e7e0d487203e
7
- data.tar.gz: 6467f4990f5ba292fb15464de537383d24388634a9509517f803d072c85815fb8de15f1a90cc345f4338a5484970a361bd1cf3d51172e8503c4ecff6f398fab1
6
+ metadata.gz: 9480f5bd22d47fd903b7714fd0cccf5b1f3e97a2dae758aefa770beaa557deba41b5af49491357f3f5a19de09e11a88a344988afad9276c73a6e51649ddfc84b
7
+ data.tar.gz: b71f7d4376eb1baced1dd2c7e027b7f25dec3aa62379878ddb8ddba15a4d8a761b43f0f687948a2b30ae3b86b1e1568bf1c49d17fd1ec51710fc75f5b9dad922
@@ -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
@@ -1,4 +1,4 @@
1
1
  module Naturally
2
2
  # Gem version
3
- VERSION = '1.5.0'
3
+ VERSION = '2.0.0'
4
4
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naturally
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter