rank 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/.gitignore +4 -0
- data/Gemfile +4 -0
- data/License.txt +19 -0
- data/README.markdown +47 -0
- data/Rakefile +2 -0
- data/lib/rank.rb +60 -0
- data/lib/rank/version.rb +3 -0
- data/rank.gemspec +23 -0
- data/test/rank_test.rb +55 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/License.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Panayiotis Thomakos
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Rank
|
2
|
+
|
3
|
+
Easily add rankings to arrays of objects.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
gem install rank
|
8
|
+
|
9
|
+
## Getting Started
|
10
|
+
|
11
|
+
Add rankings to your objects:
|
12
|
+
|
13
|
+
things = [{:attr => 'a'}, {:attr => 'c'}, {:attr => 'b'}]
|
14
|
+
Rank.add things, :attr
|
15
|
+
=> [{:attr=>"a", :rank=>1}, {:attr=>"b", :rank=>2}, {:attr=>"c", :rank=>3}]
|
16
|
+
|
17
|
+
You can add rank to ActiveRecord objects as well:
|
18
|
+
|
19
|
+
Rank.add User.all, :firstname, :lastname
|
20
|
+
|
21
|
+
In general anything that responds to '[]' can have a rank added to it.
|
22
|
+
|
23
|
+
## Additional Options
|
24
|
+
|
25
|
+
Adding a sorting order:
|
26
|
+
|
27
|
+
Rank.add User.all, [:firstname, :asc], [:lastname, :desc]
|
28
|
+
|
29
|
+
You can specify a conversion to floating point or integer:
|
30
|
+
|
31
|
+
Rank.add User.all, [:id, :asc, :integer]
|
32
|
+
|
33
|
+
You can choose to honor ties or to ignore them:
|
34
|
+
|
35
|
+
things = [{:attr => 'a'}, {:attr => 'c'}, {:attr => 'b'}, {:attr => 'b'}]
|
36
|
+
|
37
|
+
Rank.add things, :attr, :ties => false
|
38
|
+
=> [{:attr=>"a", :rank=>1}, {:attr=>"b", :rank=>2}, {:attr=>"b", :rank=>3}, {:attr=>"c", :rank=>4}]
|
39
|
+
|
40
|
+
Rank.add things, :attr, :ties => true
|
41
|
+
=> [{:attr=>"a", :rank=>1}, {:attr=>"b", :rank=>2}, {:attr=>"b", :rank=>2}, {:attr=>"c", :rank=>4}]
|
42
|
+
|
43
|
+
You can leave the objects in their original positions:
|
44
|
+
|
45
|
+
things = [{:attr => 'a'}, {:attr => 'c'}, {:attr => 'b'}]
|
46
|
+
Rank.add things, :attr, :sort => false
|
47
|
+
[{:attr=>"a", :rank=>1}, {:attr=>"c", :rank=>2}, {:attr=>"b", :rank=>2}]
|
data/Rakefile
ADDED
data/lib/rank.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module Rank
|
2
|
+
#assumes ascending :asc, but can also use descending :desc
|
3
|
+
def self.compare a, b, *attributes
|
4
|
+
attributes.each do |attribute, order, conversion|
|
5
|
+
a_attribute = self.perform_conversion a[attribute], conversion
|
6
|
+
b_attribute = self.perform_conversion b[attribute], conversion
|
7
|
+
use_order = order || :asc
|
8
|
+
if a_attribute.nil? and !b_attribute.nil?
|
9
|
+
return 1
|
10
|
+
elsif !a_attribute.nil? and b_attribute.nil?
|
11
|
+
return -1
|
12
|
+
elsif !a_attribute.nil? and !b_attribute.nil?
|
13
|
+
if a_attribute > b_attribute
|
14
|
+
return (use_order == :asc ? 1 : -1)
|
15
|
+
elsif a_attribute < b_attribute
|
16
|
+
return (use_order == :asc ? -1 : 1)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
return 0
|
21
|
+
end
|
22
|
+
|
23
|
+
#add_rank athletes, :attribute => :created_at, :ties => true
|
24
|
+
def self.add objects, *attributes
|
25
|
+
options = { :ties => true, :sort => true }.merge(extract_options! attributes)
|
26
|
+
|
27
|
+
current_rank = 1
|
28
|
+
last_object = nil
|
29
|
+
objects.sort!{ |a, b| Rank.compare(a, b, *attributes) } if options[:sort]
|
30
|
+
objects.each_with_index do |object, i|
|
31
|
+
if options[:ties]
|
32
|
+
current_rank = i+1 if !!last_object && Rank.compare(object, last_object, *attributes) == 1
|
33
|
+
else
|
34
|
+
current_rank = i+1 if !!last_object
|
35
|
+
end
|
36
|
+
object[:rank] = current_rank
|
37
|
+
last_object = object
|
38
|
+
end
|
39
|
+
|
40
|
+
return objects
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.extract_options! args
|
44
|
+
args.last.is_a?(::Hash) ? args.pop : {}
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.perform_conversion value, conversion
|
48
|
+
begin
|
49
|
+
if conversion.nil? || conversion == :none
|
50
|
+
value
|
51
|
+
elsif conversion == :float
|
52
|
+
value.to_f
|
53
|
+
elsif conversion == :integer
|
54
|
+
value.to_i
|
55
|
+
end
|
56
|
+
rescue
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/rank/version.rb
ADDED
data/rank.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rank/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rank"
|
7
|
+
s.version = Rank::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Pan Thomakos"]
|
10
|
+
s.email = ["pan.thomakos@gmail.com"]
|
11
|
+
s.homepage = "http://github.com/panthomakos/rank"
|
12
|
+
s.summary = "rank-#{Rank::VERSION}"
|
13
|
+
s.description = %q{Easily add rankings to arrays of objects.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "rank"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.extra_rdoc_files = ['README.markdown', 'License.txt']
|
21
|
+
s.rdoc_options = ['--charset=UTF-8']
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
data/test/rank_test.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../lib/rank')
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class RankTest < Test::Unit::TestCase
|
5
|
+
def test_compare
|
6
|
+
first = {:id => 10}
|
7
|
+
second = {:id => 20}
|
8
|
+
assert_equal -1, Rank.compare(first, second, :id)
|
9
|
+
assert_equal 0, Rank.compare(first, first, :id)
|
10
|
+
assert_equal 1, Rank.compare(second, first, :id)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_compare_with_multiple_attributes
|
14
|
+
first = {:one => 1, :two => 'b'}
|
15
|
+
second = {:one => 2, :two => 'a'}
|
16
|
+
assert_equal 1, Rank.compare(first, second, :two, :one)
|
17
|
+
assert_equal -1, Rank.compare(first, second, :one, :two)
|
18
|
+
assert_equal 0, Rank.compare(first, first, :one, :two)
|
19
|
+
assert_equal 0, Rank.compare(first, first, :two, :one)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_setting_rank_without_ties
|
23
|
+
first = {:id => 1}
|
24
|
+
second = {:id => 2}
|
25
|
+
Rank.add [first, second], :id, :ties => false
|
26
|
+
assert_equal 1, first[:rank]
|
27
|
+
assert_equal 2, second[:rank]
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_setting_rank_with_ties
|
31
|
+
first = {:one => 1}
|
32
|
+
second = {:one => 1}
|
33
|
+
third = {:one => 2}
|
34
|
+
Rank.add [first, second, third], :one, :ties => true
|
35
|
+
assert_equal 1, first[:rank]
|
36
|
+
assert_equal 1, second[:rank]
|
37
|
+
assert_equal 3, third[:rank]
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_always_ranking_nil_values_last
|
41
|
+
first = {:one => 1}
|
42
|
+
second = {:one => nil}
|
43
|
+
third = {:one => 2}
|
44
|
+
|
45
|
+
Rank.add [first, second, third], [:one, :asc], :ties => true, :sort => true
|
46
|
+
assert_equal 1, first[:rank]
|
47
|
+
assert_equal 2, third[:rank]
|
48
|
+
assert_equal 3, second[:rank]
|
49
|
+
|
50
|
+
Rank.add [first, second, third], [:one, :desc], :ties => true, :sort => true
|
51
|
+
assert_equal 1, third[:rank]
|
52
|
+
assert_equal 2, first[:rank]
|
53
|
+
assert_equal 3, second[:rank]
|
54
|
+
end
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rank
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Pan Thomakos
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-19 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Easily add rankings to arrays of objects.
|
22
|
+
email:
|
23
|
+
- pan.thomakos@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.markdown
|
30
|
+
- License.txt
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- License.txt
|
35
|
+
- README.markdown
|
36
|
+
- Rakefile
|
37
|
+
- lib/rank.rb
|
38
|
+
- lib/rank/version.rb
|
39
|
+
- rank.gemspec
|
40
|
+
- test/rank_test.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/panthomakos/rank
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: rank
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: rank-0.0.1
|
73
|
+
test_files:
|
74
|
+
- test/rank_test.rb
|