tennis 0.0.2 → 0.0.4

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: b058e15112d8268ef3ae828804e72656cb26dff3
4
- data.tar.gz: 10aabb8eaacd31a048224e0fac0066a24db2f690
3
+ metadata.gz: 5bd5759605bb85cc0627c04bcbcee66593b513a8
4
+ data.tar.gz: 0fa38355782c45b1fe77347a51da96657b0a2831
5
5
  SHA512:
6
- metadata.gz: 422cbc0250672d9c11fd09dc75f0a54eef705b185b0a07c2e2c2331860002f438437f712cfbdcebc03e680425dddec5acfc2ef442953b9ee3e683a2d45b579fe
7
- data.tar.gz: 81f5eb54ccb0eb62774fc9a513c749f98abe7735932e371ce7c8f234c030330aa37f688358d6b00b2b0c6879079e1bd7d1d1ba3706cb446b960bb571d9817ce8
6
+ metadata.gz: 10074593f568f7929736fa2258a01f91fbe8761f955abdf01696b4a6498c546749cb85c2dd7764552fb9029bab95a414bdeb9c2c468cf513ad672cb2849efe12
7
+ data.tar.gz: 152ca820399495c84fbc3dc12413e07d0004d8908f53c72334e371b3eecea6d06d7b83a5966fd3eeb04c77afbb98174973c1587c692bbab5741daecf06915828
data/.rbenv-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.0
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
data/Gemfile CHANGED
@@ -2,5 +2,3 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in tennis.gemspec
4
4
  gemspec
5
-
6
- gem 'rspec'
data/Rakefile CHANGED
@@ -1,3 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
2
5
 
3
6
  task :default => :spec
7
+
@@ -1,3 +1,3 @@
1
- module Tennis
2
- VERSION = "0.0.2"
1
+ class Tennis
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/tennis.rb CHANGED
@@ -1,103 +1,101 @@
1
1
  require "tennis/version"
2
2
 
3
- module Tennis
4
- class << self
5
- def initialize(scores)
6
- @scores = scores != 'default-1' && scores != 'default-2' ? scores.split(/[-,,]/).map(&:to_i) : scores
7
- @result = (1 if scores == 'default-1') || (2 if scores == 'default-2') || :default
8
- if @result == :default
9
- # to check if score for only 1 set has been input
10
- validation_1 = @scores.length == 2
11
- # to check if any input > 7
12
- validation_2 = @scores.any? { |score| score > 7 }
13
- @result = :error if validation_1 || validation_2
14
- end
3
+ class Tennis
4
+ def initialize(scores)
5
+ @scores = scores != 'default-1' && scores != 'default-2' ? scores.split(/[-,,]/).map(&:to_i) : scores
6
+ @result = (1 if scores == 'default-1') || (2 if scores == 'default-2') || :default
7
+ if @result == :default
8
+ # to check if score for only 1 set has been input
9
+ validation_1 = @scores.length == 2
10
+ # to check if any input > 7
11
+ validation_2 = @scores.any? { |score| score > 7 }
12
+ @result = :error if validation_1 || validation_2
15
13
  end
14
+ end
16
15
 
17
- # returns who won the match
18
- # :incomplete_match (bad input/incomplete match)
19
- # :error (bad input for sure)
20
- # 1 (player-1 won)
21
- # 2 (player-2 won)
22
- def result
23
- return @result if @result != :default
24
- return @result = (@scores.length == 4) ? two_sets : three_sets
25
- end
16
+ # returns who won the match
17
+ # :incomplete_match (bad input/incomplete match)
18
+ # :error (bad input for sure)
19
+ # 1 (player-1 won)
20
+ # 2 (player-2 won)
21
+ def result
22
+ return @result if @result != :default
23
+ return @result = (@scores.length == 4) ? two_sets : three_sets
24
+ end
26
25
 
27
- # returns an array of points
28
- # returns (points_player_1 , points_player_2)
29
- # returns (0,0) for bad input
30
- def points
31
- @result = self.result
32
- (return [0, 0]) if @result == :error
33
- return (complete_match_points if @result == 1 || @result == 2) || incomplete_match_points
34
- end
26
+ # returns an array of points
27
+ # returns (points_player_1 , points_player_2)
28
+ # returns (0,0) for bad input
29
+ def points
30
+ @result = self.result
31
+ (return [0, 0]) if @result == :error
32
+ return (complete_match_points if @result == 1 || @result == 2) || incomplete_match_points
33
+ end
35
34
 
36
- private
35
+ private
37
36
 
38
- # helper method: called by RESULT method for valid matches with 2 sets
39
- def two_sets
40
- set_results = []
41
- [0, 2].each do |i|
42
- # tie breaker (assuming a 7 point tie breaker) or a 7-5 scores
43
- if @scores[i] == 7 || @scores[i+1] == 7
44
- set_results << (@scores[i] == 7 ? 1 : 2)
45
- # regular set victory - 6 games with a margin of 2
46
- else
47
- return :incomplete_match if ( @scores[i] - @scores[i + 1] ).abs < 2
48
- set_results << (@scores[i] == 6 ? 1 : 2)
49
- end
37
+ # helper method: called by RESULT method for valid matches with 2 sets
38
+ def two_sets
39
+ set_results = []
40
+ [0, 2].each do |i|
41
+ # tie breaker (assuming a 7 point tie breaker) or a 7-5 scores
42
+ if @scores[i] == 7 || @scores[i+1] == 7
43
+ set_results << (@scores[i] == 7 ? 1 : 2)
44
+ # regular set victory - 6 games with a margin of 2
45
+ else
46
+ return :incomplete_match if ( @scores[i] - @scores[i + 1] ).abs < 2
47
+ set_results << (@scores[i] == 6 ? 1 : 2)
50
48
  end
51
- # incomplete match e.g: 6-4,5-3
52
- return (set_results[0] if set_results[0] == set_results[1]) || :incomplete_match
53
49
  end
50
+ # incomplete match e.g: 6-4,5-3
51
+ return (set_results[0] if set_results[0] == set_results[1]) || :incomplete_match
52
+ end
54
53
 
55
- # helper method: called by RESULT method for valid matches with 3 sets
56
- def three_sets
57
- set_results = []
58
- [0, 2, 4].each do |i|
59
- # tie breaker (assuming a 7 point tie breaker) or a 7-5 score
60
- if @scores[i] == 7 || @scores[i + 1] == 7
61
- set_results << (@scores[i] == 7 ? 1 : 2)
62
- # regular set victory - 6 games with a margin of 2
63
- else
64
- return :incomplete_match if (@scores[i] - @scores[i + 1]).abs < 2
65
- set_results << (@scores[i] == 6 ? 1 : 2)
66
- end
54
+ # helper method: called by RESULT method for valid matches with 3 sets
55
+ def three_sets
56
+ set_results = []
57
+ [0, 2, 4].each do |i|
58
+ # tie breaker (assuming a 7 point tie breaker) or a 7-5 score
59
+ if @scores[i] == 7 || @scores[i + 1] == 7
60
+ set_results << (@scores[i] == 7 ? 1 : 2)
61
+ # regular set victory - 6 games with a margin of 2
62
+ else
63
+ return :incomplete_match if (@scores[i] - @scores[i + 1]).abs < 2
64
+ set_results << (@scores[i] == 6 ? 1 : 2)
67
65
  end
68
- # checks if the result has been decided in the first 2 sets
69
- # but the 3rd set is also present in the input
70
- return :error if set_results[0] == set_results[1]
71
- return set_results.count(1) ? 1 : 2
72
66
  end
67
+ # checks if the result has been decided in the first 2 sets
68
+ # but the 3rd set is also present in the input
69
+ return :error if set_results[0] == set_results[1]
70
+ return set_results.count(1) ? 1 : 2
71
+ end
73
72
 
74
- # helper method: called by POINTS for complete matches
75
- def complete_match_points
76
- points = [0, 0]
77
- @result = self.result
78
- points[@result - 1] = (@scores.length == 6) ? 12 : 14
79
- runner_up = (@result == 1) ? 2 : 1
80
- runner_up_points = player_points(runner_up)
81
- points[runner_up - 1] = runner_up_points < 8 ? runner_up_points : 8
82
- return points
83
- end
73
+ # helper method: called by POINTS for complete matches
74
+ def complete_match_points
75
+ points = [0, 0]
76
+ @result = self.result
77
+ points[@result - 1] = (@scores.length == 6) ? 12 : 14
78
+ runner_up = (@result == 1) ? 2 : 1
79
+ runner_up_points = player_points(runner_up)
80
+ points[runner_up - 1] = runner_up_points < 8 ? runner_up_points : 8
81
+ return points
82
+ end
84
83
 
85
- # helper method: called by POINTS for incomplete matches
86
- def incomplete_match_points
87
- points = [0, 0]
88
- player_1_points = player_points(1)
89
- player_2_points = player_points(2)
90
- points[0] = player_1_points < 10 ? player_1_points : 10
91
- points[1] = player_2_points < 10 ? player_2_points : 10
92
- return points
93
- end
84
+ # helper method: called by POINTS for incomplete matches
85
+ def incomplete_match_points
86
+ points = [0, 0]
87
+ player_1_points = player_points(1)
88
+ player_2_points = player_points(2)
89
+ points[0] = player_1_points < 10 ? player_1_points : 10
90
+ points[1] = player_2_points < 10 ? player_2_points : 10
91
+ return points
92
+ end
94
93
 
95
- # helper method: returns the POINTS of a player given the player number
96
- def player_points(player)
97
- player_scores = []
98
- @scores.each_with_index { |score, index| (player_scores << score; player +=2) if index == (player - 1) }
99
- player_scores = player_scores.sort! { |x, y| y <=> x }
100
- return player_scores[0] + player_scores[1]
101
- end
94
+ # helper method: returns the POINTS of a player given the player number
95
+ def player_points(player)
96
+ player_scores = []
97
+ @scores.each_with_index { |score, index| (player_scores << score; player +=2) if index == (player - 1) }
98
+ player_scores = player_scores.sort! { |x, y| y <=> x }
99
+ return player_scores[0] + player_scores[1]
102
100
  end
103
101
  end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --diff unified
3
+ --require spec/spec_helper
4
+ --format documentation
data/spec/spec_helper.rb CHANGED
@@ -16,6 +16,11 @@
16
16
  # users commonly want.
17
17
  #
18
18
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
19
+
20
+ require 'bundler/setup'
21
+ Bundler.setup
22
+ require 'tennis'
23
+
19
24
  RSpec.configure do |config|
20
25
  # rspec-expectations config goes here. You can use an alternate
21
26
  # assertion/expectation library such as wrong or the stdlib/minitest
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe Tennis, "#packge" do
4
+ it 'has a version number' do
5
+ expect(Tennis::VERSION).not_to be nil
6
+ end
7
+ end
8
+
9
+ describe Tennis, "#scores" do
10
+ it "finds the winner properly in two sets" do
11
+ score = Tennis.new("6-4, 6-4")
12
+ expect(score.result).to eq 1
13
+ end
14
+ end
15
+
16
+ describe Tennis, "#points" do
17
+ end
data/tennis.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["cpg+git@amahi.org"]
11
11
  spec.summary = %q{A gem to manage tennis scores.}
12
12
  spec.description = %q{A gem to manage and validate tennis scores.}
13
- spec.homepage = ""
13
+ spec.homepage = "https://github.com/racketlogger/tennis"
14
14
  spec.license = "GPL"
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3"
23
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tennis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Puchol
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3'
41
55
  description: A gem to manage and validate tennis scores.
42
56
  email:
43
57
  - cpg+git@amahi.org
@@ -46,16 +60,20 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - ".gitignore"
63
+ - ".rbenv-version"
49
64
  - ".rspec"
65
+ - ".travis.yml"
50
66
  - Gemfile
51
67
  - LICENSE
52
68
  - README.md
53
69
  - Rakefile
54
70
  - lib/tennis.rb
55
71
  - lib/tennis/version.rb
72
+ - spec/spec.opts
56
73
  - spec/spec_helper.rb
74
+ - spec/tennis_spec.rb
57
75
  - tennis.gemspec
58
- homepage: ''
76
+ homepage: https://github.com/racketlogger/tennis
59
77
  licenses:
60
78
  - GPL
61
79
  metadata: {}
@@ -80,4 +98,6 @@ signing_key:
80
98
  specification_version: 4
81
99
  summary: A gem to manage tennis scores.
82
100
  test_files:
101
+ - spec/spec.opts
83
102
  - spec/spec_helper.rb
103
+ - spec/tennis_spec.rb