sortable 0.0.1
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/History.txt +4 -0
- data/Manifest.txt +10 -0
- data/README.rdoc +63 -0
- data/Rakefile +17 -0
- data/lib/sortable.rb +27 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/test_helper.rb +3 -0
- data/test/test_sortable.rb +54 -0
- metadata +82 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
= sortable
|
2
|
+
|
3
|
+
* http://github.com/bscofield/sortable
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
Sortable provides a DSL for defining sort order on any Ruby object
|
8
|
+
|
9
|
+
To use it, you just call the sortable method and pass it a list of methods and/or blocks; when
|
10
|
+
you call sort on a collection of these objects, each method/block is evaluated in turn, and the
|
11
|
+
first that provides a non-zero sort value is used.
|
12
|
+
|
13
|
+
== EXAMPLE:
|
14
|
+
|
15
|
+
require 'sortable'
|
16
|
+
|
17
|
+
class Book
|
18
|
+
attr_accessor :author, :title
|
19
|
+
|
20
|
+
sortable :author, lambda {|book| book.title.sub(/^(The|A|An) /, '')}
|
21
|
+
|
22
|
+
def initialize(author, title)
|
23
|
+
self.author = author
|
24
|
+
self.title = title
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
fforde = Book.new('Fforde', 'Something Rotten')
|
29
|
+
stroud1 = Book.new('Stroud', 'The Golem\'s Eye')
|
30
|
+
stroud2 = Book.new('Stroud', 'The Amulet of Samarkand')
|
31
|
+
colfer = Book.new('Colfer', 'Artemis Fowl')
|
32
|
+
|
33
|
+
[fforde, stroud1, stroud2, colfer].sort #=> colfer, fforde, stroud2, stroud1
|
34
|
+
|
35
|
+
== INSTALL:
|
36
|
+
|
37
|
+
Installation is available with a simple:
|
38
|
+
sudo gem install sortable
|
39
|
+
|
40
|
+
== LICENSE:
|
41
|
+
|
42
|
+
(The MIT License)
|
43
|
+
|
44
|
+
Copyright (c) 2009 Ben Scofield
|
45
|
+
|
46
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
47
|
+
a copy of this software and associated documentation files (the
|
48
|
+
'Software'), to deal in the Software without restriction, including
|
49
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
50
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
51
|
+
permit persons to whom the Software is furnished to do so, subject to
|
52
|
+
the following conditions:
|
53
|
+
|
54
|
+
The above copyright notice and this permission notice shall be
|
55
|
+
included in all copies or substantial portions of the Software.
|
56
|
+
|
57
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
58
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
59
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
60
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
61
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
62
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
63
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/sortable'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
|
9
|
+
# Generate all the Rake tasks
|
10
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
11
|
+
$hoe = Hoe.spec 'sortable' do
|
12
|
+
self.developer 'Ben Scofield', 'ruby@turrean.com'
|
13
|
+
self.rubyforge_name = 'sortable-object'
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'newgem/tasks'
|
17
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
data/lib/sortable.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Sortable
|
2
|
+
VERSION = '0.0.1'
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend Sortable::ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def sortable(*sorters)
|
10
|
+
define_method :"<=>", lambda { |other|
|
11
|
+
comp = 0
|
12
|
+
sorters.each do |sorter|
|
13
|
+
comp = if sorter.respond_to?(:call)
|
14
|
+
sorter.call(self) <=> sorter.call(other)
|
15
|
+
else
|
16
|
+
self.send(sorter) <=> other.send(sorter)
|
17
|
+
end
|
18
|
+
|
19
|
+
break if comp != 0
|
20
|
+
end
|
21
|
+
comp
|
22
|
+
}
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
Object.send(:include, Sortable)
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/sortable.rb'}"
|
9
|
+
puts "Loading sortable gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'pp'
|
2
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
3
|
+
|
4
|
+
class TestBook
|
5
|
+
attr_accessor :author, :title
|
6
|
+
|
7
|
+
def initialize(author, title)
|
8
|
+
self.author = author
|
9
|
+
self.title = title
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestSortable < Test::Unit::TestCase
|
14
|
+
def setup
|
15
|
+
@fforde = TestBook.new('Fforde', 'Something Rotten')
|
16
|
+
@stroud1 = TestBook.new('Stroud', 'The Golem\'s Eye')
|
17
|
+
@stroud2 = TestBook.new('Stroud', 'The Amulet of Samarkand')
|
18
|
+
@colfer = TestBook.new('Colfer', 'Artemis Fowl')
|
19
|
+
|
20
|
+
@books = [@fforde, @stroud1, @stroud2, @colfer]
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_sortable_should_be_defined
|
24
|
+
assert_nothing_raised do
|
25
|
+
Sortable
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_object_should_include_sortable
|
30
|
+
assert Object.ancestors.include?(Sortable)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_object_should_have_sortable_method
|
34
|
+
assert Object.respond_to?(:sortable)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_books_should_sort_by_author_and_title
|
38
|
+
TestBook.send(:sortable, :author, :title)
|
39
|
+
|
40
|
+
assert_equal [@colfer, @fforde, @stroud2, @stroud1], @books.sort
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_books_should_sort_by_unaltered_title
|
44
|
+
TestBook.send(:sortable, :title, :author)
|
45
|
+
|
46
|
+
assert_equal [@colfer, @fforde, @stroud2, @stroud1], @books.sort
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_books_should_sort_by_altered_title
|
50
|
+
TestBook.send(:sortable, lambda {|book| book.title.sub(/^(The|A|An) /, '')}, :author)
|
51
|
+
|
52
|
+
assert_equal [@stroud2, @colfer, @stroud1, @fforde], @books.sort
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sortable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Scofield
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-23 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.3
|
24
|
+
version:
|
25
|
+
description: |-
|
26
|
+
Sortable provides a DSL for defining sort order on any Ruby object
|
27
|
+
|
28
|
+
To use it, you just call the sortable method and pass it a list of methods and/or blocks; when
|
29
|
+
you call sort on a collection of these objects, each method/block is evaluated in turn, and the
|
30
|
+
first that provides a non-zero sort value is used.
|
31
|
+
email:
|
32
|
+
- ruby@turrean.com
|
33
|
+
executables: []
|
34
|
+
|
35
|
+
extensions: []
|
36
|
+
|
37
|
+
extra_rdoc_files:
|
38
|
+
- History.txt
|
39
|
+
- Manifest.txt
|
40
|
+
files:
|
41
|
+
- History.txt
|
42
|
+
- Manifest.txt
|
43
|
+
- README.rdoc
|
44
|
+
- Rakefile
|
45
|
+
- lib/sortable.rb
|
46
|
+
- script/console
|
47
|
+
- script/destroy
|
48
|
+
- script/generate
|
49
|
+
- test/test_helper.rb
|
50
|
+
- test/test_sortable.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/bscofield/sortable
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --main
|
58
|
+
- README.rdoc
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project: sortable-object
|
76
|
+
rubygems_version: 1.3.4
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: Sortable provides a DSL for defining sort order on any Ruby object To use it, you just call the sortable method and pass it a list of methods and/or blocks; when you call sort on a collection of these objects, each method/block is evaluated in turn, and the first that provides a non-zero sort value is used.
|
80
|
+
test_files:
|
81
|
+
- test/test_helper.rb
|
82
|
+
- test/test_sortable.rb
|