ranker 1.1.2 → 1.1.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.
- checksums.yaml +4 -4
- data/lib/ranker/rankings.rb +14 -13
- data/lib/ranker/strategies/strategy.rb +49 -2
- data/lib/ranker/version.rb +1 -1
- data/spec/lib/ranker/rankings_spec.rb +2 -2
- data/spec/lib/ranker/strategies/strategy_spec.rb +39 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5c3b5746a5cdd097d78fc2823646621bc6cfc6ed
|
|
4
|
+
data.tar.gz: 1db30566ffe2483db910c37b85d0080a5be77ef1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c57616642e3d8e62f1f876dbf929b6a94f85c40b94bd5ad89415b9d528268481d2e681a4e6a61b657e2713975c6918098be65369fcf0ab39fc5f8a4cce913a53
|
|
7
|
+
data.tar.gz: 3aa2ee9a5e3c13dc10ce62c8b1ad2fb6c918fa9ea524ddeb73b01034877e712a2b9f3bbf284d04097b111b7c95aec17d16b05c21e31375d1a6ede6ce15b1c0c5
|
data/lib/ranker/rankings.rb
CHANGED
|
@@ -38,9 +38,8 @@ module Ranker
|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
def total
|
|
41
|
-
unless valid?
|
|
42
|
-
|
|
43
|
-
end
|
|
41
|
+
raise Error.new(errors) unless valid?
|
|
42
|
+
|
|
44
43
|
@total ||= scores.reduce(:+)
|
|
45
44
|
end
|
|
46
45
|
|
|
@@ -85,19 +84,21 @@ module Ranker
|
|
|
85
84
|
}
|
|
86
85
|
end
|
|
87
86
|
|
|
88
|
-
end # Rankings class
|
|
89
87
|
|
|
88
|
+
# Inner classes:
|
|
90
89
|
|
|
91
|
-
|
|
90
|
+
class Error < StandardError
|
|
92
91
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
92
|
+
def initialize(errors)
|
|
93
|
+
message = 'Rankings has errors: '
|
|
94
|
+
message << errors.map { |name, error|
|
|
95
|
+
"#{name} #{error}"
|
|
96
|
+
}.join(', ')
|
|
97
|
+
super(message)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
end # Error class
|
|
100
101
|
|
|
101
|
-
end #
|
|
102
|
+
end # Rankings class
|
|
102
103
|
|
|
103
104
|
end # Ranker module
|
|
@@ -17,15 +17,30 @@ module Ranker::Strategies
|
|
|
17
17
|
|
|
18
18
|
# Properties:
|
|
19
19
|
|
|
20
|
+
def errors
|
|
21
|
+
@errors ||= {}
|
|
22
|
+
end
|
|
23
|
+
|
|
20
24
|
def rankings
|
|
21
25
|
@rankings ||= Ranker::Rankings.new(self)
|
|
22
26
|
end
|
|
23
27
|
|
|
28
|
+
def scores
|
|
29
|
+
rankables_grouped_by_score.keys
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def valid?
|
|
33
|
+
validate
|
|
34
|
+
errors.empty?
|
|
35
|
+
end
|
|
36
|
+
|
|
24
37
|
|
|
25
38
|
# Methods:
|
|
26
39
|
|
|
27
40
|
|
|
28
41
|
def rank
|
|
42
|
+
raise Error.new(errors) unless valid?
|
|
43
|
+
|
|
29
44
|
execute
|
|
30
45
|
rankings
|
|
31
46
|
end
|
|
@@ -77,6 +92,38 @@ module Ranker::Strategies
|
|
|
77
92
|
rankables_grouped_by_score[score]
|
|
78
93
|
end
|
|
79
94
|
|
|
80
|
-
|
|
95
|
+
def validate
|
|
96
|
+
errors.clear
|
|
97
|
+
validate_scores
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def validate_scores
|
|
101
|
+
if scores_have_nil_values?
|
|
102
|
+
errors['scores'] = 'contains nil values'
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def scores_have_nil_values?
|
|
107
|
+
scores.any? { |score|
|
|
108
|
+
score == nil
|
|
109
|
+
}
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
# Inner classes:
|
|
114
|
+
|
|
115
|
+
class Error < StandardError
|
|
116
|
+
|
|
117
|
+
def initialize(errors)
|
|
118
|
+
message = 'Strategy has errors: '
|
|
119
|
+
message << errors.map { |name, error|
|
|
120
|
+
"#{name} #{error}"
|
|
121
|
+
}.join(', ')
|
|
122
|
+
super(message)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
end # Error class
|
|
126
|
+
|
|
127
|
+
end # Strategy class
|
|
81
128
|
|
|
82
|
-
end # module
|
|
129
|
+
end # Ranker::Strategies module
|
data/lib/ranker/version.rb
CHANGED
|
@@ -29,7 +29,7 @@ describe Ranker::Rankings do
|
|
|
29
29
|
context 'when rankables contain nil values' do
|
|
30
30
|
let(:rankables) { [1, 2, 3, nil, 5, 6, 7] }
|
|
31
31
|
it 'should raise an error' do
|
|
32
|
-
lambda { rankings.mean }.should raise_error(Ranker::
|
|
32
|
+
lambda { rankings.mean }.should raise_error(Ranker::Rankings::Error)
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
|
|
@@ -62,7 +62,7 @@ describe Ranker::Rankings do
|
|
|
62
62
|
context 'when rankables contain nil values' do
|
|
63
63
|
let(:rankables) { [1, 2, 3, nil, 5, 6, 7] }
|
|
64
64
|
it 'should raise an error' do
|
|
65
|
-
lambda { rankings.standard_deviation }.should raise_error(Ranker::
|
|
65
|
+
lambda { rankings.standard_deviation }.should raise_error(Ranker::Rankings::Error)
|
|
66
66
|
end
|
|
67
67
|
end
|
|
68
68
|
|
|
@@ -13,6 +13,36 @@ describe Ranker::Strategies::StandardCompetition do
|
|
|
13
13
|
its(:score) { should be }
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
+
describe :properties do
|
|
17
|
+
|
|
18
|
+
describe :valid? do
|
|
19
|
+
let(:rankables) { raise NotImplementedError }
|
|
20
|
+
let(:strategy) { klass.new(rankables) }
|
|
21
|
+
let(:valid) { strategy.valid? }
|
|
22
|
+
subject { valid }
|
|
23
|
+
before {
|
|
24
|
+
valid
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
context 'when rankables are not nil' do
|
|
28
|
+
let(:rankables) { [1, 2, 3, 4, 5, 6, 7] }
|
|
29
|
+
it { should == true }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context 'when rankables contain nil values' do
|
|
33
|
+
let(:rankables) { [1, 2, 3, nil, 5, 6, 7] }
|
|
34
|
+
it { should == false }
|
|
35
|
+
|
|
36
|
+
context 'errors' do
|
|
37
|
+
subject { strategy.errors['scores'] }
|
|
38
|
+
it { should == 'contains nil values' }
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end # valid?
|
|
43
|
+
|
|
44
|
+
end # properties
|
|
45
|
+
|
|
16
46
|
describe :methods do
|
|
17
47
|
|
|
18
48
|
describe :rank do
|
|
@@ -21,6 +51,15 @@ describe Ranker::Strategies::StandardCompetition do
|
|
|
21
51
|
it 'should be an abstract method' do
|
|
22
52
|
lambda { strategy.rank }.should raise_error(NotImplementedError)
|
|
23
53
|
end
|
|
54
|
+
|
|
55
|
+
context 'when rankables has nil values' do
|
|
56
|
+
let(:rankables) { [1, 2, 3, nil, 5, 6, 7] }
|
|
57
|
+
let(:strategy) { klass.new(rankables) }
|
|
58
|
+
it 'should raise an error' do
|
|
59
|
+
lambda { strategy.rank }.should raise_error(Ranker::Strategies::Strategy::Error)
|
|
60
|
+
end
|
|
61
|
+
end # when rankables had nil values
|
|
62
|
+
|
|
24
63
|
end # rank
|
|
25
64
|
|
|
26
65
|
end # methods
|