ranker 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ranker::Strategies::StandardCompetition do
4
+ let(:klass) { Ranker::Strategies::Strategy }
5
+
6
+ describe :initialize do
7
+ let(:rankables) { [1, 2, 3, 4, 5, 5, 6, 7, 7, 7, 2] }
8
+ let(:strategy) { klass.new(rankables) }
9
+ subject { strategy }
10
+ its(:rankables) { should == rankables }
11
+ its(:rankings) { should be }
12
+ its(:options) { should be }
13
+ its(:score) { should be }
14
+ end
15
+
16
+ describe :methods do
17
+
18
+ describe :rank do
19
+ let(:strategy) { klass.new([]) }
20
+ subject { strategy }
21
+ it 'should be an abstract method' do
22
+ lambda { strategy.rank }.should raise_error(NotImplementedError)
23
+ end
24
+ end # rank
25
+
26
+ end # methods
27
+
28
+ end
@@ -0,0 +1,144 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ranker do
4
+ let(:klass) { Ranker }
5
+
6
+ describe :class_methods do
7
+
8
+ describe :rank do
9
+ let(:rankables) { raise NotImplementedError }
10
+ let(:rankings) { klass.rank(rankables) }
11
+ subject { rankings }
12
+
13
+ context 'when simple rankables are used' do
14
+ let(:rankables) { [1, 1, 2, 3, 3, 4, 5, 6, 7, 7, 7, 1, 1, 3] }
15
+ subject { rankings }
16
+ it { should have(7).items }
17
+
18
+ context '1st ranking' do
19
+ let(:ranking) { rankings[0] }
20
+ subject { ranking }
21
+ its(:rank) { should == 1 }
22
+ its(:rankables) { should == [7, 7, 7] }
23
+ end
24
+
25
+ context 'last ranking' do
26
+ let(:ranking) { rankings[-1] }
27
+ subject { ranking }
28
+ its(:rank) { should == 11 }
29
+ its(:rankables) { should == [1, 1, 1, 1] }
30
+ end
31
+
32
+ end # when simple rankables are used
33
+
34
+ context 'when ranking by symbol' do
35
+ let(:player_1) { Player.new(150) }
36
+ let(:player_2) { Player.new(100) }
37
+ let(:player_3) { Player.new(100) }
38
+ let(:player_4) { Player.new(25) }
39
+ let(:players) { [player_1, player_2, player_3, player_4] }
40
+ let(:rankings) { klass.rank(players, :by => :score) }
41
+ it { should have(3).items }
42
+
43
+ context '1st ranking' do
44
+ let(:ranking) { rankings[0] }
45
+ subject { ranking }
46
+ its(:rank) { should == 1 }
47
+ its(:rankables) { should =~ [player_1] }
48
+ end
49
+
50
+ context '2nd ranking' do
51
+ let(:ranking) { rankings[1] }
52
+ subject { ranking }
53
+ its(:rank) { should == 2 }
54
+ its(:rankables) { should =~ [player_2, player_3] }
55
+ end
56
+
57
+ context '3rd ranking' do
58
+ let(:ranking) { rankings[2] }
59
+ subject { ranking }
60
+ its(:rank) { should == 4 }
61
+ its(:rankables) { should =~ [player_4] }
62
+ end
63
+
64
+ end # when ranking by symbol
65
+
66
+ context 'when ranking by symbol and desc false' do
67
+ let(:player_1) { Player.new(150) }
68
+ let(:player_2) { Player.new(100) }
69
+ let(:player_3) { Player.new(100) }
70
+ let(:player_4) { Player.new(25) }
71
+ let(:players) { [player_1, player_2, player_3, player_4] }
72
+ let(:rankings) { klass.rank(players, :by => :score, :desc => false) }
73
+ it { should have(3).items }
74
+
75
+ context '1st ranking' do
76
+ let(:ranking) { rankings[0] }
77
+ subject { ranking }
78
+ its(:rank) { should == 1 }
79
+ its(:rankables) { should =~ [player_4] }
80
+ end
81
+
82
+ context '2nd ranking' do
83
+ let(:ranking) { rankings[1] }
84
+ subject { ranking }
85
+ its(:rank) { should == 2 }
86
+ its(:rankables) { should =~ [player_2, player_3] }
87
+ end
88
+
89
+ context '3rd ranking' do
90
+ let(:ranking) { rankings[2] }
91
+ subject { ranking }
92
+ its(:rank) { should == 4 }
93
+ its(:rankables) { should =~ [player_1] }
94
+ end
95
+
96
+ end # when ranking by symbol
97
+
98
+ context 'when using lambda to rank by' do
99
+ let(:player_1) { Player.new(150) }
100
+ let(:player_2) { Player.new(100) }
101
+ let(:player_3) { Player.new(100) }
102
+ let(:player_4) { Player.new(25) }
103
+ let(:players) { [player_1, player_2, player_3, player_4] }
104
+ let(:rankings) { klass.rank(players, :by => lambda { |player| player.score}) }
105
+ it { should have(3).items }
106
+
107
+ context '1st ranking' do
108
+ let(:ranking) { rankings[0] }
109
+ subject { ranking }
110
+ its(:rank) { should == 1 }
111
+ its(:rankables) { should =~ [player_1] }
112
+ end
113
+
114
+ context '2nd ranking' do
115
+ let(:ranking) { rankings[1] }
116
+ subject { ranking }
117
+ its(:rank) { should == 2 }
118
+ its(:rankables) { should =~ [player_2, player_3] }
119
+ end
120
+
121
+ context '3rd ranking' do
122
+ let(:ranking) { rankings[2] }
123
+ subject { ranking }
124
+ its(:rank) { should == 4 }
125
+ its(:rankables) { should =~ [player_4] }
126
+ end
127
+
128
+ end # when ranking by symbol
129
+
130
+ end # rank
131
+
132
+ end # class_methods
133
+
134
+ end
135
+
136
+ class Player
137
+
138
+ attr_reader :score
139
+
140
+ def initialize(score)
141
+ @score = score
142
+ end
143
+
144
+ end
@@ -0,0 +1,3 @@
1
+ require 'pry'
2
+ require File.expand_path('../../lib/ranker.rb', __FILE__)
3
+
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ranker
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ilya Scharrenbroich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2014-01-06 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pry
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - &id002
20
+ - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :development
24
+ version_requirements: *id001
25
+ - !ruby/object:Gem::Dependency
26
+ name: pry-nav
27
+ prerelease: false
28
+ requirement: &id003 !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - *id002
31
+ type: :development
32
+ version_requirements: *id003
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id004 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - *id002
39
+ type: :development
40
+ version_requirements: *id004
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ prerelease: false
44
+ requirement: &id005 !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - *id002
47
+ type: :development
48
+ version_requirements: *id005
49
+ description:
50
+ email:
51
+ - ilya.j.s@gmail.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - lib/ranker/ranking.rb
60
+ - lib/ranker/rankings.rb
61
+ - lib/ranker/strategies/dense.rb
62
+ - lib/ranker/strategies/modified_competition.rb
63
+ - lib/ranker/strategies/ordinal.rb
64
+ - lib/ranker/strategies/standard_competition.rb
65
+ - lib/ranker/strategies/strategy.rb
66
+ - lib/ranker/strategies.rb
67
+ - lib/ranker/version.rb
68
+ - lib/ranker.rb
69
+ - README.md
70
+ - spec/lib/ranker/ranking_spec.rb
71
+ - spec/lib/ranker/rankings_spec.rb
72
+ - spec/lib/ranker/strategies/dense_spec.rb
73
+ - spec/lib/ranker/strategies/modified_competition_spec.rb
74
+ - spec/lib/ranker/strategies/ordinal_spec.rb
75
+ - spec/lib/ranker/strategies/standard_competition_spec.rb
76
+ - spec/lib/ranker/strategies/strategy_spec.rb
77
+ - spec/ranker_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: https://github.com/quidproquo/ranker
80
+ licenses: []
81
+
82
+ metadata: {}
83
+
84
+ post_install_message:
85
+ rdoc_options: []
86
+
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - *id002
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - *id002
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 2.0.14
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Library that enables the ranking of lists of items using various ranking strategies.
102
+ test_files:
103
+ - spec/lib/ranker/ranking_spec.rb
104
+ - spec/lib/ranker/rankings_spec.rb
105
+ - spec/lib/ranker/strategies/dense_spec.rb
106
+ - spec/lib/ranker/strategies/modified_competition_spec.rb
107
+ - spec/lib/ranker/strategies/ordinal_spec.rb
108
+ - spec/lib/ranker/strategies/standard_competition_spec.rb
109
+ - spec/lib/ranker/strategies/strategy_spec.rb
110
+ - spec/ranker_spec.rb
111
+ - spec/spec_helper.rb