Class: StatsForYear
- Inherits:
-
Object
- Object
- StatsForYear
- Defined in:
- lib/bb_analytics/models/stats_for_year.rb
Constant Summary collapse
- FANTASY_HR =
4
- FANTASY_RBI =
1
- FANTASY_SB =
1
- FANTASY_CS =
-1
Instance Attribute Summary collapse
-
#at_bats ⇒ Object
Returns the value of attribute at_bats.
-
#batting_average ⇒ Object
Returns the value of attribute batting_average.
-
#caught_stealing ⇒ Object
Returns the value of attribute caught_stealing.
-
#doubles ⇒ Object
Returns the value of attribute doubles.
-
#fantasy_points ⇒ Object
Returns the value of attribute fantasy_points.
-
#games ⇒ Object
Returns the value of attribute games.
-
#hits ⇒ Object
Returns the value of attribute hits.
-
#home_runs ⇒ Object
Returns the value of attribute home_runs.
-
#player_external_id ⇒ Object
Returns the value of attribute player_external_id.
-
#runs ⇒ Object
Returns the value of attribute runs.
-
#runs_batted_in ⇒ Object
Returns the value of attribute runs_batted_in.
-
#slugging_percentage ⇒ Object
Returns the value of attribute slugging_percentage.
-
#stolen_bases ⇒ Object
Returns the value of attribute stolen_bases.
-
#team ⇒ Object
Returns the value of attribute team.
-
#triples ⇒ Object
Returns the value of attribute triples.
-
#year ⇒ Object
Returns the value of attribute year.
Class Method Summary collapse
- .find_highest_batting_average(year, min_at_bats) ⇒ Object
- .find_highest_home_runs(year, min_at_bats) ⇒ Object
- .find_highest_rbi(year, min_at_bats) ⇒ Object
- .find_team_for_year(year, team) ⇒ Object
-
.find_triple_crown_winner(year, min_at_bats) ⇒ Object
finders.
- .most_improved_batting_average(from_year, to_year) ⇒ Object
- .most_improved_fantasy_points(from_year, to_year) ⇒ Object
Instance Method Summary collapse
- #baseball_player ⇒ Object
-
#build_from_result_row(row) ⇒ Object
end finders.
- #calculate_batting_average! ⇒ Object
- #calculate_fantasy_points! ⇒ Object
- #calculate_slugging_percentage! ⇒ Object
- #reload ⇒ Object
- #save ⇒ Object
Instance Attribute Details
#at_bats ⇒ Object
Returns the value of attribute at_bats.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def at_bats @at_bats end |
#batting_average ⇒ Object
Returns the value of attribute batting_average.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def batting_average @batting_average end |
#caught_stealing ⇒ Object
Returns the value of attribute caught_stealing.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def caught_stealing @caught_stealing end |
#doubles ⇒ Object
Returns the value of attribute doubles.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def doubles @doubles end |
#fantasy_points ⇒ Object
Returns the value of attribute fantasy_points.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def fantasy_points @fantasy_points end |
#games ⇒ Object
Returns the value of attribute games.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def games @games end |
#hits ⇒ Object
Returns the value of attribute hits.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def hits @hits end |
#home_runs ⇒ Object
Returns the value of attribute home_runs.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def home_runs @home_runs end |
#player_external_id ⇒ Object
Returns the value of attribute player_external_id.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def player_external_id @player_external_id end |
#runs ⇒ Object
Returns the value of attribute runs.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def runs @runs end |
#runs_batted_in ⇒ Object
Returns the value of attribute runs_batted_in.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def runs_batted_in @runs_batted_in end |
#slugging_percentage ⇒ Object
Returns the value of attribute slugging_percentage.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def slugging_percentage @slugging_percentage end |
#stolen_bases ⇒ Object
Returns the value of attribute stolen_bases.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def stolen_bases @stolen_bases end |
#team ⇒ Object
Returns the value of attribute team.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def team @team end |
#triples ⇒ Object
Returns the value of attribute triples.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def triples @triples end |
#year ⇒ Object
Returns the value of attribute year.
8 9 10 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 8 def year @year end |
Class Method Details
.find_highest_batting_average(year, min_at_bats) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 97 def self.find_highest_batting_average year, min_at_bats highest_batting_average = 0 # find the max value in case there are folks who are tied DataStore.instance.db.execute("select max(batting_average) from stats_for_years where at_bats > ? and year = ?", [min_at_bats, year]) do |row| highest_batting_average = row[0] end stats_to_return = [] # now grab everyone with the high value DataStore.instance.db.execute("select * from stats_for_years where at_bats > ? and year = ? and batting_average = ?", [min_at_bats, year, highest_batting_average]) do |row| stats = StatsForYear.new stats.build_from_result_row row stats_to_return << stats end stats_to_return end |
.find_highest_home_runs(year, min_at_bats) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 115 def self.find_highest_home_runs year, min_at_bats highest_home_runs = 0 # find the max value in case there are folks who are tied DataStore.instance.db.execute("select max(home_runs) from stats_for_years where at_bats > ? and year = ?", [min_at_bats, year]) do |row| highest_home_runs = row[0] end stats_to_return = [] # now grab everyone with the high value DataStore.instance.db.execute("select * from stats_for_years where at_bats > ? and year = ? and home_runs = ?", [min_at_bats, year, highest_home_runs]) do |row| stats = StatsForYear.new stats.build_from_result_row row stats_to_return << stats end stats_to_return end |
.find_highest_rbi(year, min_at_bats) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 133 def self.find_highest_rbi year, min_at_bats highest_rbi = 0 # find the max value in case there are folks who are tied DataStore.instance.db.execute("select max(runs_batted_in) from stats_for_years where at_bats > ? and year = ?", [min_at_bats, year]) do |row| highest_rbi = row[0] end stats_to_return = [] # now grab everyone with the high value DataStore.instance.db.execute("select * from stats_for_years where at_bats > ? and year = ? and runs_batted_in = ?", [min_at_bats, year, highest_rbi]) do |row| stats = StatsForYear.new stats.build_from_result_row row stats_to_return << stats end stats_to_return end |
.find_team_for_year(year, team) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 151 def self.find_team_for_year year, team teammates = [] DataStore.instance.db.execute("select * from stats_for_years where year = ? and team = ?", [year, team]) do |row| stats = StatsForYear.new stats.build_from_result_row row teammates << stats end teammates end |
.find_triple_crown_winner(year, min_at_bats) ⇒ Object
finders
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 85 def self.find_triple_crown_winner year, min_at_bats home_run_winners = find_highest_home_runs year, min_at_bats rbi_winners = find_highest_rbi year, min_at_bats batting_crown_winners = find_highest_batting_average year, min_at_bats first_group = [] home_run_winners.each{ |hr| first_group << hr if rbi_winners.any?{ |rbi| rbi.player_external_id == hr.player_external_id } } second_group = [] first_group.each{ |fg| second_group << fg if batting_crown_winners.any?{ |bc| bc.player_external_id == fg.player_external_id } } second_group.first end |
.most_improved_batting_average(from_year, to_year) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 186 def self.most_improved_batting_average from_year, to_year sql = 'select a.player_external_id, (a.batting_average - b.batting_average) ' + 'from stats_for_years a, stats_for_years b ' + 'where a.year = ? and b.year = ? ' + 'and a.player_external_id = b.player_external_id' parse_most_improved sql, from_year, to_year end |
.most_improved_fantasy_points(from_year, to_year) ⇒ Object
195 196 197 198 199 200 201 202 203 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 195 def self.most_improved_fantasy_points from_year, to_year sql = 'select a.player_external_id, (a.fantasy_points - b.fantasy_points) ' + 'from stats_for_years a, stats_for_years b ' + 'where a.year = ? and b.year = ? ' + 'and a.player_external_id = b.player_external_id' parse_most_improved sql, from_year, to_year end |
Instance Method Details
#baseball_player ⇒ Object
182 183 184 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 182 def baseball_player BaseballPlayer.find_by_external_id self.player_external_id end |
#build_from_result_row(row) ⇒ Object
end finders
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 163 def build_from_result_row row self.player_external_id = row[0] self.year = row[1] self.team = row[2] self.games = row[3] self.at_bats = row[4] self.runs = row[5] self.hits = row[6] self.doubles = row[7] self.triples = row[8] self.home_runs = row[9] self.runs_batted_in = row[10] self.stolen_bases = row[11] self.caught_stealing = row[12] self.batting_average = row[13] self.slugging_percentage = row[14] self.fantasy_points = row[15] end |
#calculate_batting_average! ⇒ Object
68 69 70 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 68 def calculate_batting_average! self.batting_average = (((self.hits.to_f / self.at_bats.to_f) * 1000).to_i rescue nil) end |
#calculate_fantasy_points! ⇒ Object
78 79 80 81 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 78 def calculate_fantasy_points! self.fantasy_points = ((self.home_runs * FANTASY_HR) + (self.runs_batted_in * FANTASY_RBI) + (self.stolen_bases * FANTASY_SB) + (self.caught_stealing * FANTASY_CS) rescue nil) end |
#calculate_slugging_percentage! ⇒ Object
72 73 74 75 76 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 72 def calculate_slugging_percentage! self.slugging_percentage = (((((self.hits - self.doubles - self.triples - self.home_runs) + (2 * self.doubles) + (3 * self.triples) + (4 * self.home_runs)).to_f / self.at_bats.to_f) * 1000).to_i rescue nil) end |
#reload ⇒ Object
61 62 63 64 65 66 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 61 def reload DataStore.instance.db.execute("select * from stats_for_years where player_external_id = ? and year = ? and team = ?", [self.player_external_id, self.year, self.team]) do |row| self.build_from_result_row row end self end |
#save ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bb_analytics/models/stats_for_year.rb', line 12 def save # first see if we can update an existing record DataStore.instance.db.execute("select * from stats_for_years where player_external_id = ? and year = ? and team = ?", [self.player_external_id, self.year, self.team]) do |row| DataStore.instance.db.execute "update stats_for_years " + "set year = ?, team = ?, games = ?, at_bats = ?, runs =?, " + "hits = ?, doubles = ?, triples = ?, home_runs = ?, runs_batted_in = ?, " + "stolen_bases = ?, caught_stealing = ?, batting_average = ?, slugging_percentage = ?, fantasy_points = ? " + "where player_external_id = ?", [(self.year.present? ? self.year : row[1]), (self.team.present? ? self.team : row[2]), (self.games.present? ? self.games : row[3]), (self.at_bats.present? ? self.at_bats : row[4]), (self.runs.present? ? self.runs : row[5]), (self.hits.present? ? self.hits : row[6]), (self.doubles.present? ? self.doubles : row[7]), (self.triples.present? ? self.triples : row[8]), (self.home_runs.present? ? self.home_runs : row[9]), (self.runs_batted_in.present? ? self.runs_batted_in : row[10]), (self.stolen_bases.present? ? self.stolen_bases : row[11]), (self.caught_stealing.present? ? self.caught_stealing : row[12]), (self.batting_average.present? ? self.batting_average : row[13]), (self.slugging_percentage.present? ? self.slugging_percentage : row[14]), (self.fantasy_points.present? ? self.fantasy_points : row[15]), self.player_external_id] return end # if not, just insert DataStore.instance.db.execute "insert into stats_for_years values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", [self.player_external_id, self.year, self.team, self.games, self.at_bats, self.runs, self.hits, self.doubles, self.triples, self.home_runs, self.runs_batted_in, self.stolen_bases, self.caught_stealing, self.batting_average, self.slugging_percentage, self.fantasy_points] end |