fixture_list_generator 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8a99ed4d9febfe355983ef2d2f5b961cdd248814
4
+ data.tar.gz: f9da39d59ede1049d27cfce1a36d1bc9b27443ae
5
+ SHA512:
6
+ metadata.gz: 8d21dd262052e2d7f8f7cdc5470bce92c0dc85087f526ce065765d9a0b14b3aceea8e688731e2ee6c937535fc50c73aa467142496eba8987fb18e62ef09973b4
7
+ data.tar.gz: 80475b729c9868bb3875ebe84b571831092dee5a98d789e7352f4fab548b65ce137db9976f0400c5ad9eeecb9772788718f538f86d3161ace4584b9f3ac77f46
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.idea/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fixture_list_generator.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Greg Stewart
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # FixtureListGenerator
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fixture_list_generator'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fixture_list_generator
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/fixture_list_generator/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.test_files = FileList['test/*_test.rb']
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fixture_list_generator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fixture_list_generator"
8
+ spec.version = FixtureListGenerator::VERSION
9
+ spec.authors = ["Greg Stewart"]
10
+ spec.email = ["gregs@tcias.co.uk"]
11
+ spec.summary = %q{Fixture List Generator}
12
+ spec.description = %q{A gem to generate a fixture list from a list of IDs}
13
+ spec.homepage = "https://github.com/gregstewart/fixture_list_generator"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,98 @@
1
+ require "fixture_list_generator/version"
2
+
3
+ # All code in the gem is namespaced under this module.
4
+ module FixtureListGenerator
5
+ def FixtureListGenerator.generate array_of_teams
6
+ number_of_fixtures = (array_of_teams.length-1)*2
7
+ fixtures = Array.new(number_of_fixtures) { {"week" => nil, "matches" => []} }
8
+
9
+ build_fixtures_collection(array_of_teams, fixtures)
10
+
11
+ matches = build_team_matches_for_the_season(array_of_teams)
12
+
13
+ populate_fixtures_from_matches(fixtures, matches)
14
+
15
+ fixtures
16
+ end
17
+
18
+ # merges the possible fixtures with possible matches
19
+ def self.populate_fixtures_from_matches(fixtures, matches)
20
+ while !matches.empty? do
21
+ current_match = matches.pop
22
+
23
+ set_fixture(current_match, fixtures)
24
+ end
25
+ end
26
+
27
+ # builds a list of all possible match combinations
28
+ def self.build_team_matches_for_the_season(array_of_teams)
29
+ duplicate_array_of_teams = array_of_teams.dup
30
+ matches = []
31
+ while !duplicate_array_of_teams.empty? do
32
+ current_team = duplicate_array_of_teams.pop
33
+
34
+ array_of_teams.each do |opponent|
35
+ if opponent != current_team
36
+
37
+ match_hash = {:home => current_team, :away => opponent}
38
+ matches.push match_hash
39
+ end
40
+ end
41
+ end
42
+ matches
43
+ end
44
+
45
+ # builds a skeleton of weekly fixtures and matches to be played
46
+ def self.build_fixtures_collection(array_of_teams, fixtures)
47
+ fixtures.map.with_index do |week, i|
48
+ week["week"] = i+1
49
+ week["matches"] = Array.new(array_of_teams.length/2) { {:home => nil, :away => nil} }
50
+ end
51
+ end
52
+
53
+ # inserts a match into a slot in a weekly fixture
54
+ def self.set_fixture(current_match, fixtures)
55
+ unless has_match_been_played fixtures, current_match
56
+ fixtures.each do |fixture|
57
+ unless has_team_played_this_week fixture["matches"], current_match
58
+ fixture["matches"].each do |match|
59
+
60
+ if match[:home].nil?
61
+ match[:home] = current_match[:home]
62
+ match[:away] = current_match[:away]
63
+ return
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ # determines whether the match combination has already been played this season
72
+ def self.has_match_been_played(fixtures, current_match)
73
+ match_has_been_played = false
74
+ fixtures.each do | fixture |
75
+ fixture["matches"].each do |match|
76
+
77
+ if match == current_match
78
+ puts "#{match == current_match}"
79
+ match_has_been_played = true
80
+ end
81
+ end
82
+ end
83
+
84
+ match_has_been_played
85
+ end
86
+
87
+ # determines whether a team in a match has already played this week
88
+ def self.has_team_played_this_week(matches, current_match)
89
+ team_has_played = false
90
+ matches.each do |match|
91
+ if match[:home] == current_match[:home] || match[:away] == current_match[:away] || match[:home] == current_match[:away] || match[:away] == current_match[:home]
92
+ team_has_played = true
93
+ end
94
+ end
95
+
96
+ team_has_played
97
+ end
98
+ end
@@ -0,0 +1,4 @@
1
+ module FixtureListGenerator
2
+ # The current version of Fixture List Generator.
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,81 @@
1
+ require "minitest/autorun"
2
+
3
+ require "fixture_list_generator"
4
+
5
+ describe "Fixture List Generator" do
6
+ before do
7
+ @list = [1,2,3,4]
8
+ @expected_number_of_fixture_weeks = (@list.length-1)*2
9
+ @expected_number_of_games_per_week = @list.length/2
10
+ end
11
+
12
+ it "must not be empty" do
13
+ generated_fixture_list = FixtureListGenerator::generate(@list)
14
+ generated_fixture_list.wont_be_nil
15
+ end
16
+
17
+ it "returns an array with a length of #{@expected_number_of_fixture_weeks}" do
18
+ generated_fixture_list = FixtureListGenerator::generate(@list)
19
+ generated_fixture_list.count.must_equal @expected_number_of_fixture_weeks
20
+ end
21
+
22
+ describe "each team" do
23
+ it "plays an even number of home and away games" do
24
+ generated_fixture_list = FixtureListGenerator::generate(@list)
25
+ team_one_match_count = { :away => 0, :home => 0 }
26
+ team_two_match_count = { :away => 0, :home => 0 }
27
+ team_three_match_count = { :away => 0, :home => 0 }
28
+ team_four_match_count = { :away => 0, :home => 0 }
29
+ generated_fixture_list.each do | week |
30
+ week["matches"].each do | match |
31
+ if match[:home] == 1
32
+ team_one_match_count[:home] += 1
33
+ elsif match[:away] == 1
34
+ team_one_match_count[:away] += 1
35
+ end
36
+
37
+ if match[:home] == 2
38
+ team_two_match_count[:home] += 1
39
+ elsif match[:away] == 2
40
+ team_two_match_count[:away] += 1
41
+ end
42
+
43
+ if match[:home] == 3
44
+ team_three_match_count[:home] += 1
45
+ elsif match[:away] == 3
46
+ team_three_match_count[:away] += 1
47
+ end
48
+
49
+ if match[:home] == 4
50
+ team_four_match_count[:home] += 1
51
+ elsif match[:away] == 4
52
+ team_four_match_count[:away] += 1
53
+ end
54
+ end
55
+ end
56
+
57
+ games_to_be_played = @expected_number_of_fixture_weeks/2
58
+ team_one_match_count[:home].must_equal games_to_be_played
59
+ team_one_match_count[:away].must_equal games_to_be_played
60
+ team_two_match_count[:home].must_equal games_to_be_played
61
+ team_two_match_count[:away].must_equal games_to_be_played
62
+ team_three_match_count[:home].must_equal games_to_be_played
63
+ team_three_match_count[:away].must_equal games_to_be_played
64
+ team_four_match_count[:home].must_equal games_to_be_played
65
+ team_four_match_count[:away].must_equal games_to_be_played
66
+ end
67
+ end
68
+
69
+ describe "each week" do
70
+ it "has a #{@expected_number_of_games_per_week}" do
71
+ generated_fixture_list = FixtureListGenerator::generate(@list)
72
+ generated_fixture_list[0]["matches"].length.must_equal @expected_number_of_games_per_week
73
+ generated_fixture_list[1]["matches"].length.must_equal @expected_number_of_games_per_week
74
+ generated_fixture_list[2]["matches"].length.must_equal @expected_number_of_games_per_week
75
+ generated_fixture_list[3]["matches"].length.must_equal @expected_number_of_games_per_week
76
+ generated_fixture_list[4]["matches"].length.must_equal @expected_number_of_games_per_week
77
+ generated_fixture_list[5]["matches"].length.must_equal @expected_number_of_games_per_week
78
+ end
79
+
80
+ end
81
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fixture_list_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Greg Stewart
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A gem to generate a fixture list from a list of IDs
42
+ email:
43
+ - gregs@tcias.co.uk
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - fixture_list_generator.gemspec
54
+ - lib/fixture_list_generator.rb
55
+ - lib/fixture_list_generator/version.rb
56
+ - test/fixture_list_test.rb
57
+ homepage: https://github.com/gregstewart/fixture_list_generator
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.4.3
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Fixture List Generator
81
+ test_files:
82
+ - test/fixture_list_test.rb