Created
June 5, 2013 09:50
-
-
Save jmeirow/5712823 to your computer and use it in GitHub Desktop.
contest calendar classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'securerandom' | |
| require 'pp' | |
| require 'date' | |
| # all of the associations will reference a contest season | |
| class Database | |
| def self.where &block | |
| @@data[self].select &block | |
| end | |
| def self.all | |
| @@data[self] | |
| end | |
| def save | |
| @@data ||= Hash.new | |
| @@data[self.class] ||= Array.new | |
| if !@id | |
| @id = SecureRandom.uuid | |
| @@data[self.class] << self | |
| end | |
| self | |
| end | |
| def self.find id | |
| @@data[self].select {|x| x.id == id }.first | |
| end | |
| def self.dump | |
| @@data | |
| end | |
| end | |
| class ContestSeason < Database | |
| attr_accessor :id, :name, :from_date, :to_date | |
| def initialize name, from_date, to_date | |
| @name, @from_date, @to_date = name, from_date, to_date | |
| end | |
| def self.current | |
| (ContestSeason.where {|x| Date.today >= x.from_date && Date.today <= x.to_date }).first | |
| end | |
| end | |
| class District < Database | |
| attr_accessor :id, :description, :contest_season_id | |
| def initialize (description, contest_season_id) | |
| @description, @contest_season_id = description, contest_season_id | |
| end | |
| def divisions | |
| DistrictDivision.where {|x| x.district_id == @id } | |
| end | |
| end | |
| class DistrictDivision < Database | |
| attr_accessor :id, :district_id, :division_id, :contest_season_id | |
| def initialize district_id, division_id, contest_season_id | |
| @district_id, @division_id, @contest_season_id = district_id, division_id, contest_season_id | |
| end | |
| end | |
| class Division < Database | |
| attr_accessor :id, :description, :contest_season_id | |
| def initialize description, contest_season_id | |
| @description, @contest_season_id = description, contest_season_id | |
| end | |
| def district | |
| DistrictDivision.where {|x| x.division_id == @id }.collect {|x| District.find x.district_id } | |
| end | |
| def areas | |
| DivisionArea.where {|x| x.division_id == @id }.collect {|x| Area.find x.area_id } | |
| end | |
| def add_area area | |
| raise "different contest seasons " if area.contest_season_id != contest_season_id | |
| DivisionArea.new(id,area.id, contest_season_id).save | |
| end | |
| end | |
| class DivisionArea < Database | |
| attr_accessor :id, :division_id, :area_id, :contest_season_id | |
| def initialize division_id, area_id, contest_season_id | |
| @division_id, @area_id, @contest_season_id = division_id, area_id, contest_season_id | |
| end | |
| end | |
| class Area < Database | |
| attr_accessor :id, :description, :contest_season_id | |
| def initialize description, contest_season_id | |
| @description, @contest_season_id = description, contest_season_id | |
| end | |
| def division | |
| DivisionArea.where {|x| x.area_id == @id }.collect{ |x| Division.find x.division_id } | |
| end | |
| def clubs | |
| AreaClub.where {|x| x.area_id == @id}.collect {|x| Club.find x.club_id } | |
| end | |
| def add_club club | |
| raise "different contest seasons " if club.contest_season_id != contest_season_id | |
| AreaClub.new(id, club.id, contest_season_id).save | |
| end | |
| def reserve contest_date | |
| raise "wrong territory type for this date!" if contest_date.level != "area" | |
| AreaContestDate.new(id, contest_date, contest_season_id).save | |
| allowed -= 1 | |
| save | |
| end | |
| def release area | |
| raise "wrong territory type for this date!" if area.class != Area | |
| AreaContestDate.new(area.id, self, contest_season_id).save | |
| allowed -= 1 | |
| save | |
| end | |
| end | |
| class AreaClub < Database | |
| attr_accessor :id, :area_id, :club_id, :contest_season_id | |
| def initialize area_id, club_id, contest_season_id | |
| @area_id, @club_id, @contest_season_id = area_id, club_id, contest_season_id | |
| end | |
| end | |
| class AreaContestDate < Database | |
| attr_accessor :id, :area_id, :contest_date, :contest_season_id | |
| def initialize area_id, contest_date, contest_season_id | |
| @area_id, @contest_date, @contest_season_id = area_id, contest_date, contest_season_id | |
| end | |
| end | |
| class Club < Database | |
| attr_accessor :id, :description, :contest_season_id | |
| def initialize description, contest_season_id | |
| @description, @contest_season_id = description, contest_season_id | |
| end | |
| def area | |
| AreaClub.where {|x| x.club_id == @id }.collect{ |x| Area.find x.area_id } | |
| end | |
| end | |
| class Person < Database | |
| attr_accessor :id, :first_name, :last_name, :phone, :email, :contest_season_id | |
| def initialize first_name, last_name, phone, email, contest_season_id | |
| @first_name, @last_name, @phone, @email, @contest_season_id = first_name, last_name, phone, email, contest_season_id | |
| end | |
| def represents | |
| club | |
| end | |
| end | |
| class Contest < Database | |
| attr_accessor :id, :description, :contest_season_id | |
| def initialize description, contest_season_id | |
| @description, @contest_season_id = description, contest_season_id | |
| end | |
| def add_club_person club_id, person_id | |
| x = ClubPersonContest.new(club_id, person_id, id) | |
| x.level = "club" | |
| x.place = 0 | |
| x.save | |
| x | |
| end | |
| end | |
| class ClubPersonContest < Database | |
| attr_accessor :id, :club_id, :person_id, :contest_id, :place, :level | |
| def initialize club_id, person_id, contest_id | |
| @club_id, @person_id, @contest_id = club_id, person_id, contest_id | |
| end | |
| def advance | |
| if level == "club" | |
| @level = "area" | |
| elsif @level == "area" | |
| @level = "division" | |
| elsif level == "division" | |
| @level = "district" | |
| end | |
| end | |
| def club | |
| ClubPersonContest.where {|x| x.id == id }.collect {|x| Club.find x.club_id } | |
| end | |
| def person | |
| ClubPersonContest.where {|x| x.id == id }.collect {|x| Person.find x.person_id } | |
| end | |
| def contest | |
| ClubPersonContest.where {|x| x.id == id }.collect {|x| Contest.find x.contest_id } | |
| end | |
| end | |
| class ContestDate < Database | |
| attr_accessor :id, :date, :level, :allowed | |
| def initialize level, date, allowed | |
| @level, @date, @allowed = level, date, 1 | |
| end | |
| end | |
| season = ContestSeason.new("Fall 2013", Date.strptime("01/01/2013","%m/%d/%Y" ), Date.strptime("12/31/2013","%m/%d/%Y") ).save | |
| humorous_speech = Contest.new("Humorous Speech", season.id).save | |
| table_topics = Contest.new("Table Topics", season.id).save | |
| d28 = District.new( "District 28", ContestSeason.current.id).save | |
| divE = Division.new("Division E", ContestSeason.current.id).save | |
| area21 = Area.new("Area 21", ContestSeason.current.id).save | |
| area22 = Area.new("Area 22", ContestSeason.current.id).save | |
| area23 = Area.new("Area 23", ContestSeason.current.id).save | |
| area24 = Area.new("Area 24", ContestSeason.current.id).save | |
| DistrictDivision.new(d28.id, divE.id, season.id).save | |
| divE.add_area area21 | |
| divE.add_area area22 | |
| divE.add_area area23 | |
| divE.add_area area24 | |
| speakeasy = Club.new("SpeakEasy Toastmasters of Shelby Twp", season.id).save | |
| area21.add_club speakeasy | |
| joe = Person.new("Joe", "Meirow", "586-623-9987", "joe.meirow@mail.com" ,season.id ).save | |
| contestant = humorous_speech.add_club_person speakeasy.id, joe.id | |
| puts "District-----------------------------------" | |
| pp District.all | |
| puts "Division-----------------------------------" | |
| pp Division.all | |
| puts "DistrictDivision-----------------------------------" | |
| pp DistrictDivision.all | |
| puts "Area-----------------------------------" | |
| pp Area.all | |
| puts "DivisionArea-----------------------------------" | |
| pp DivisionArea.all | |
| puts "Club-----------------------------------" | |
| pp Club.all | |
| puts "Contest-----------------------------------" | |
| pp Contest.all | |
| puts "Person-----------------------------------" | |
| pp Person.all | |
| puts "ClubPersonContest-----------------------------------" | |
| pp ClubPersonContest.all | |
| (Date.new(2013,9,1)..Date.new(2013,10,20)).to_a.each do |x| | |
| ContestDate.new("area",x,1).save | |
| end | |
| (Date.new(2013,10,30)..Date.new(2013,12,31)).to_a.each do |x| | |
| ContestDate.new("division",x,1).save | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment