ranker 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/ranker/rankings.rb +51 -4
- data/lib/ranker/strategies/strategy.rb +1 -1
- data/lib/ranker/version.rb +1 -1
- data/spec/lib/ranker/ranking_spec.rb +2 -2
- data/spec/lib/ranker/rankings_spec.rb +44 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95ba813411999220469ff0cfa6abdeb92dd80cd0
|
4
|
+
data.tar.gz: 7c089dfa7754a24d2f35a87dd605a8e01b0dfe11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aaf519717eb324031eb13c5fac87b581d6b60f4135876d8f43a5d2c5dd7172e536c5a623c48f2b31f5dccd3608d29af31e4122b479ffab2ae38f71577e638ad1
|
7
|
+
data.tar.gz: c8fa18492eaaba497d03e57cfee32ec6d6c3f0f0ae7d1f16e140ff63fc255c72672628769cc5d93e7bd56f8fbdb114385573888f594007763b0bacd5460d7ca8
|
data/README.md
CHANGED
data/lib/ranker/rankings.rb
CHANGED
@@ -12,6 +12,10 @@ module Ranker
|
|
12
12
|
|
13
13
|
# Properties:
|
14
14
|
|
15
|
+
def errors
|
16
|
+
@errors ||= {}
|
17
|
+
end
|
18
|
+
|
15
19
|
def mean
|
16
20
|
@mean ||= total.to_f / num_scores
|
17
21
|
end
|
@@ -34,6 +38,9 @@ module Ranker
|
|
34
38
|
end
|
35
39
|
|
36
40
|
def total
|
41
|
+
unless valid?
|
42
|
+
raise RankingsError.new(errors)
|
43
|
+
end
|
37
44
|
@total ||= scores.reduce(:+)
|
38
45
|
end
|
39
46
|
|
@@ -43,14 +50,54 @@ module Ranker
|
|
43
50
|
}
|
44
51
|
end
|
45
52
|
|
53
|
+
def valid?
|
54
|
+
validate
|
55
|
+
errors.empty?
|
56
|
+
end
|
57
|
+
|
46
58
|
|
47
59
|
# Methods:
|
48
60
|
|
49
|
-
def
|
61
|
+
def create_ranking(rank, score, rankables)
|
50
62
|
scores.concat(Array.new(rankables.count, score))
|
51
|
-
|
63
|
+
ranking = Ranking.new(self, self.count, rank, score, rankables)
|
64
|
+
self << ranking
|
65
|
+
ranking
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
protected
|
70
|
+
|
71
|
+
def validate
|
72
|
+
errors.clear
|
73
|
+
validate_scores
|
74
|
+
end
|
75
|
+
|
76
|
+
def validate_scores
|
77
|
+
if scores_have_nil_values?
|
78
|
+
errors['scores'] = 'contains nil values'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def scores_have_nil_values?
|
83
|
+
scores.any? { |score|
|
84
|
+
score == nil
|
85
|
+
}
|
86
|
+
end
|
87
|
+
|
88
|
+
end # Rankings class
|
89
|
+
|
90
|
+
|
91
|
+
class RankingsError < StandardError
|
92
|
+
|
93
|
+
def initialize(errors)
|
94
|
+
message = 'Rankings has errors: '
|
95
|
+
message << errors.map { |name, error|
|
96
|
+
"#{name} #{error}"
|
97
|
+
}.join(', ')
|
98
|
+
super(message)
|
52
99
|
end
|
53
100
|
|
54
|
-
end # class
|
101
|
+
end # RankingError class
|
55
102
|
|
56
|
-
end # module
|
103
|
+
end # Ranker module
|
data/lib/ranker/version.rb
CHANGED
@@ -11,7 +11,7 @@ describe Ranker::Ranking do
|
|
11
11
|
let(:rankables) { [1, 2, 3, 4, 5, 5, 1, 2] }
|
12
12
|
before {
|
13
13
|
rankables.each_with_index { |value, index|
|
14
|
-
rankings.
|
14
|
+
rankings.create_ranking(index + 1, value, [value])
|
15
15
|
}
|
16
16
|
}
|
17
17
|
|
@@ -45,7 +45,7 @@ describe Ranker::Ranking do
|
|
45
45
|
let(:rankables) { [1, 2, 3, 4, 5, 5, 1, 2] }
|
46
46
|
before {
|
47
47
|
rankables.each_with_index { |value, index|
|
48
|
-
rankings.
|
48
|
+
rankings.create_ranking(index + 1, value, [value])
|
49
49
|
}
|
50
50
|
}
|
51
51
|
|
@@ -12,7 +12,7 @@ describe Ranker::Rankings do
|
|
12
12
|
subject { rankings.mean }
|
13
13
|
before {
|
14
14
|
rankables.each_with_index { |value, index|
|
15
|
-
rankings.
|
15
|
+
rankings.create_ranking(index + 1, value, [value])
|
16
16
|
}
|
17
17
|
}
|
18
18
|
|
@@ -26,6 +26,13 @@ describe Ranker::Rankings do
|
|
26
26
|
it { should be_nan }
|
27
27
|
end
|
28
28
|
|
29
|
+
context 'when rankables contain nil values' do
|
30
|
+
let(:rankables) { [1, 2, 3, nil, 5, 6, 7] }
|
31
|
+
it 'should raise an error' do
|
32
|
+
lambda { rankings.mean }.should raise_error(Ranker::RankingsError)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
29
36
|
end # mean
|
30
37
|
|
31
38
|
describe :standard_deviation do
|
@@ -33,7 +40,7 @@ describe Ranker::Rankings do
|
|
33
40
|
subject { rankings.standard_deviation }
|
34
41
|
before {
|
35
42
|
rankables.each_with_index { |value, index|
|
36
|
-
rankings.
|
43
|
+
rankings.create_ranking(index + 1, value, [value])
|
37
44
|
}
|
38
45
|
}
|
39
46
|
|
@@ -52,8 +59,43 @@ describe Ranker::Rankings do
|
|
52
59
|
it { should be_nan }
|
53
60
|
end
|
54
61
|
|
62
|
+
context 'when rankables contain nil values' do
|
63
|
+
let(:rankables) { [1, 2, 3, nil, 5, 6, 7] }
|
64
|
+
it 'should raise an error' do
|
65
|
+
lambda { rankings.standard_deviation }.should raise_error(Ranker::RankingsError)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
55
69
|
end # standard_deviation
|
56
70
|
|
71
|
+
describe :valid? do
|
72
|
+
let(:rankables) { raise NotImplementedError }
|
73
|
+
let(:valid) { rankings.valid? }
|
74
|
+
subject { valid }
|
75
|
+
before {
|
76
|
+
rankables.each_with_index { |value, index|
|
77
|
+
rankings.create_ranking(index + 1, value, [value])
|
78
|
+
}
|
79
|
+
valid
|
80
|
+
}
|
81
|
+
|
82
|
+
context 'when rankables are not nil' do
|
83
|
+
let(:rankables) { [1, 2, 3, 4, 5, 6, 7] }
|
84
|
+
it { should == true }
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'when rankables contain nil values' do
|
88
|
+
let(:rankables) { [1, 2, 3, nil, 5, 6, 7] }
|
89
|
+
it { should == false }
|
90
|
+
|
91
|
+
context 'errors' do
|
92
|
+
subject { rankings.errors['scores'] }
|
93
|
+
it { should == 'contains nil values' }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
end # valid?
|
98
|
+
|
57
99
|
end # properties
|
58
100
|
|
59
101
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ranker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Scharrenbroich
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: coveralls
|