naturally 1.0.2 → 1.0.3

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.
data/README.md CHANGED
@@ -4,6 +4,11 @@ Natural sorting with support for legal document numbering.
4
4
  See [Counting to 10 in Californian](http://www.weblaws.org/blog/2012/08/counting-from-1-to-10-in-californian/)
5
5
  for the motivations to make this library.
6
6
 
7
+ The core of the search is [from here](https://github.com/ahoward/version_sorter). I then made
8
+ several changes to handle the particular types of numbers that come up in statutes, such
9
+ as *335.1, 336, 336a*, etc.
10
+
11
+
7
12
  ## Installation
8
13
 
9
14
  Add this line to your application's Gemfile:
@@ -18,9 +23,36 @@ Or install it yourself as:
18
23
 
19
24
  $ gem install naturally
20
25
 
26
+
21
27
  ## Usage
22
28
 
23
- See the spec for examples.
29
+ ```Ruby
30
+ # Sort a simple array of strings
31
+ Naturally.sort(["1.1", "1.10", "1.2"]) # => ["1.1", "1.2", "1.10"]
32
+
33
+ # Sort an array of objects by one attribute
34
+ Thing = Struct.new(:number, :name)
35
+ objects = [
36
+ Thing.new('1.1', 'color'),
37
+ Thing.new('1.2', 'size'),
38
+ Thing.new('1.1.1', 'opacity'),
39
+ Thing.new('1.1.2', 'lightness'),
40
+ Thing.new('1.10', 'hardness'),
41
+ Thing.new('2.1', 'weight'),
42
+ Thing.new('1.3', 'shape')
43
+ ]
44
+ objects.sort_by{ |o| Naturally.normalize(o.number) }
45
+ # => [#<struct Thing number="1.1", name="color">,
46
+ #<struct Thing number="1.1.1", name="opacity">,
47
+ #<struct Thing number="1.1.2", name="lightness">,
48
+ #<struct Thing number="1.2", name="size">,
49
+ #<struct Thing number="1.3", name="shape">,
50
+ #<struct Thing number="1.10", name="hardness">,
51
+ #<struct Thing number="2.1", name="weight">]
52
+ ```
53
+
54
+ See [the spec for more examples](https://github.com/dogweather/naturally/blob/master/spec/naturally_spec.rb).
55
+
24
56
 
25
57
  ## Contributing
26
58
 
@@ -1,3 +1,3 @@
1
1
  module Naturally
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
  end
data/lib/naturally.rb CHANGED
@@ -3,14 +3,19 @@ require "naturally/version"
3
3
  module Naturally
4
4
 
5
5
  # Perform a natural sort.
6
- # @param [Array<String>] a list of numbers to sort.
6
+ # @param [Array<String>] an_array the list of numbers to sort.
7
7
  # @return [Array<String>] the numbers sorted naturally.
8
8
  def self.sort(an_array)
9
9
  return an_array.sort_by{ |x| normalize(x) }
10
10
  end
11
11
 
12
- def self.normalize(version)
13
- version.to_s.scan(%r/[0-9a-zA-Z]+/o).map{|i| NumberElement.new(i)}
12
+ # Convert the given number into and object that can be sorted
13
+ # naturally.
14
+ # @param [String] number the number in complex form such as 1.2a.3.
15
+ # @return [Array<NumberElement>] an array of NumberElements which are
16
+ # able to be sorted naturally via a normal 'sort'.
17
+ def self.normalize(number)
18
+ number.to_s.scan(%r/[0-9a-zA-Z]+/o).map{|i| NumberElement.new(i)}
14
19
  end
15
20
 
16
21
 
@@ -32,4 +32,29 @@ describe Naturally do
32
32
  Naturally.sort(a).should == b
33
33
  end
34
34
  end
35
+
36
+ describe '#normalize' do
37
+ it 'enables sorting objects by one particular attribute' do
38
+ Thing = Struct.new(:number, :name)
39
+ objects = [
40
+ Thing.new('1.1', 'color'),
41
+ Thing.new('1.2', 'size'),
42
+ Thing.new('1.1.1', 'opacity'),
43
+ Thing.new('1.1.2', 'lightness'),
44
+ Thing.new('1.10', 'hardness'),
45
+ Thing.new('2.1', 'weight'),
46
+ Thing.new('1.3', 'shape')
47
+ ]
48
+ sorted = objects.sort_by{ |o| Naturally.normalize(o.number) }
49
+ sorted.map{|o| o.name}.should == %w[
50
+ color
51
+ opacity
52
+ lightness
53
+ size
54
+ shape
55
+ hardness
56
+ weight
57
+ ]
58
+ end
59
+ end
35
60
  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.0.2
4
+ version: 1.0.3
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: 2012-12-08 00:00:00.000000000 Z
12
+ date: 2012-12-15 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Natural Sorting with support for legal numbering
15
15
  email: