Permalink
Browse files

specs and docs

  • Loading branch information...
1 parent 8cdd9eb commit 5992d7b6e50b7de61d637fd36afd9ca965e98446 @dogweather committed Dec 15, 2012
Showing with 40 additions and 0 deletions.
  1. +15 −0 README.md
  2. +25 −0 spec/naturally_spec.rb
View
@@ -27,7 +27,22 @@ Or install it yourself as:
## Usage
```Ruby
+# Sort a simple array of strings
Naturally.sort(["1.1", "1.10", "1.2"]) # => ["1.1", "1.2", "1.10"]
+
+# Sort an array of objects by one attribute
+objects.sort_by{ |o| Naturally.normalize(o.number) }
+Thing = Struct.new(:number, :name)
+objects = [
+ Thing.new('1.1', 'color'),
+ Thing.new('1.2', 'size'),
+ Thing.new('1.1.1', 'opacity'),
+ Thing.new('1.1.2', 'lightness'),
+ Thing.new('1.10', 'hardness'),
+ Thing.new('2.1', 'weight'),
+ Thing.new('1.3', 'shape')
+ ]
+sorted = objects.sort_by{ |o| Naturally.normalize(o.number) }
```
See [the spec for more examples](https://github.com/dogweather/naturally/blob/master/spec/naturally_spec.rb).
View
@@ -32,4 +32,29 @@
Naturally.sort(a).should == b
end
end
+
+ describe '#normalize' do
+ it 'enables sorting objects by one particular attribute' do
+ Thing = Struct.new(:number, :name)
+ objects = [
+ Thing.new('1.1', 'color'),
+ Thing.new('1.2', 'size'),
+ Thing.new('1.1.1', 'opacity'),
+ Thing.new('1.1.2', 'lightness'),
+ Thing.new('1.10', 'hardness'),
+ Thing.new('2.1', 'weight'),
+ Thing.new('1.3', 'shape')
+ ]
+ sorted = objects.sort_by{ |o| Naturally.normalize(o.number) }
+ sorted.map{|o| o.name}.should == %w[
+ color
+ opacity
+ lightness
+ size
+ shape
+ hardness
+ weight
+ ]
+ end
+ end
end

0 comments on commit 5992d7b

Please sign in to comment.