ranker 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 95ba813411999220469ff0cfa6abdeb92dd80cd0
4
- data.tar.gz: 7c089dfa7754a24d2f35a87dd605a8e01b0dfe11
3
+ metadata.gz: 5c3b5746a5cdd097d78fc2823646621bc6cfc6ed
4
+ data.tar.gz: 1db30566ffe2483db910c37b85d0080a5be77ef1
5
5
  SHA512:
6
- metadata.gz: aaf519717eb324031eb13c5fac87b581d6b60f4135876d8f43a5d2c5dd7172e536c5a623c48f2b31f5dccd3608d29af31e4122b479ffab2ae38f71577e638ad1
7
- data.tar.gz: c8fa18492eaaba497d03e57cfee32ec6d6c3f0f0ae7d1f16e140ff63fc255c72672628769cc5d93e7bd56f8fbdb114385573888f594007763b0bacd5460d7ca8
6
+ metadata.gz: c57616642e3d8e62f1f876dbf929b6a94f85c40b94bd5ad89415b9d528268481d2e681a4e6a61b657e2713975c6918098be65369fcf0ab39fc5f8a4cce913a53
7
+ data.tar.gz: 3aa2ee9a5e3c13dc10ce62c8b1ad2fb6c918fa9ea524ddeb73b01034877e712a2b9f3bbf284d04097b111b7c95aec17d16b05c21e31375d1a6ede6ce15b1c0c5
@@ -38,9 +38,8 @@ module Ranker
38
38
  end
39
39
 
40
40
  def total
41
- unless valid?
42
- raise RankingsError.new(errors)
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
- class RankingsError < StandardError
90
+ class Error < StandardError
92
91
 
93
- def initialize(errors)
94
- message = 'Rankings has errors: '
95
- message << errors.map { |name, error|
96
- "#{name} #{error}"
97
- }.join(', ')
98
- super(message)
99
- end
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 # RankingError class
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
- end # class
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
@@ -1,3 +1,3 @@
1
1
  module Ranker
2
- VERSION = '1.1.2'
2
+ VERSION = '1.1.3'
3
3
  end
@@ -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::RankingsError)
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::RankingsError)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ranker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Scharrenbroich