ranker 1.1.1 → 1.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab92d39440ca181c5a9f7b0c8f0747384f6ee539
4
- data.tar.gz: 39353e69dae425e89759f7f38d56c12364207c7c
3
+ metadata.gz: 95ba813411999220469ff0cfa6abdeb92dd80cd0
4
+ data.tar.gz: 7c089dfa7754a24d2f35a87dd605a8e01b0dfe11
5
5
  SHA512:
6
- metadata.gz: df861f01de167e2cc07f8b956faf69b0bc5011ea53aeb12e6b5b86037a1e8ab093f27a4e6f1dd53379cfadaac9cfe7f26bd6630ab073546e6a1a874ccf2d2894
7
- data.tar.gz: e3d2e2c5bcd34f4983da21422dfa13c8764529cfc57132eed8b5fac3325103ad3002813c245f12fcaf232a9ffdd4035952e85712f24ee1414f51d2db86986b36
6
+ metadata.gz: aaf519717eb324031eb13c5fac87b581d6b60f4135876d8f43a5d2c5dd7172e536c5a623c48f2b31f5dccd3608d29af31e4122b479ffab2ae38f71577e638ad1
7
+ data.tar.gz: c8fa18492eaaba497d03e57cfee32ec6d6c3f0f0ae7d1f16e140ff63fc255c72672628769cc5d93e7bd56f8fbdb114385573888f594007763b0bacd5460d7ca8
data/README.md CHANGED
@@ -14,7 +14,7 @@ Installation
14
14
  With bundler, add the `ranker` gem to your `Gemfile`.
15
15
 
16
16
  ```ruby
17
- gem "ranker", "~> 1.0"
17
+ gem "ranker"
18
18
  ```
19
19
 
20
20
  Require the `ranker` gem in your application.
@@ -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 create(rank, score, rankables)
61
+ def create_ranking(rank, score, rankables)
50
62
  scores.concat(Array.new(rankables.count, score))
51
- self << Ranking.new(self, self.count, rank, score, rankables)
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
@@ -66,7 +66,7 @@ module Ranker::Strategies
66
66
  # Methods:
67
67
 
68
68
  def create_ranking(rank, score, rankables)
69
- rankings.create(rank, score, rankables)
69
+ rankings.create_ranking(rank, score, rankables)
70
70
  end
71
71
 
72
72
  def execute
@@ -1,3 +1,3 @@
1
1
  module Ranker
2
- VERSION = '1.1.1'
2
+ VERSION = '1.1.2'
3
3
  end
@@ -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.create(index + 1, value, [value])
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.create(index + 1, value, [value])
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.create(index + 1, value, [value])
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.create(index + 1, value, [value])
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.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-01-10 00:00:00.000000000 Z
11
+ date: 2014-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coveralls