Class: Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/schedule.rb

Overview

This class is used to retrieve season schedule information and to query for information related to the schedule.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year) ⇒ Schedule

Loads a single season schedule from a schedule text file



13
14
15
16
# File 'lib/schedule.rb', line 13

def initialize(year)
  @games = []
  read_file(get_sked_filename(year))
end

Instance Attribute Details

#gamesObject

Returns the value of attribute games.



9
10
11
# File 'lib/schedule.rb', line 9

def games
  @games
end

Instance Method Details

#get_opening_dayObject

Returns the date of opening day as a string with the format YYYYMMDD



20
21
22
# File 'lib/schedule.rb', line 20

def get_opening_day
  games[0].date
end

#get_season_lengthObject

Returns an integer representing the number of games in the season specified



26
27
28
# File 'lib/schedule.rb', line 26

def get_season_length
  games[games.size-1].home_game_number.to_i
end

#get_sked_filename(year) ⇒ Object



32
33
34
35
# File 'lib/schedule.rb', line 32

def get_sked_filename(year)
  #'schedules/' + year.to_s + 'SKED.TXT'
  File.expand_path(File.dirname(__FILE__) + '/schedules/' + year.to_s + 'SKED.TXT')
end

#read_file(filename) ⇒ Object

Reads the data from a schedule file Each line in the schedule file represents a single game. here is a sample of what a single line in the file looks like:

  "20090405","0","Sun","ATL","NL",1,"PHI","NL",1,"n","",""
this is interpreted as:
   date, 0, day, visiting team, visiting league, visiting game number, home team, home league, home game number, day or night (d or n), "", ""


44
45
46
47
48
49
50
51
# File 'lib/schedule.rb', line 44

def read_file(filename)
  contents = ''
  File.open(filename, "r") do |infile|
    while (line = infile.gets)
      @games << ScheduleGame.new(line)
    end
  end
end