Module: BaseballStats

Extended by:
BaseballStats
Includes:
Database, Import
Included in:
BaseballStats
Defined in:
lib/baseball_stats.rb,
lib/baseball_stats/player.rb,
lib/baseball_stats/batting.rb,
lib/baseball_stats/version.rb,
lib/baseball_stats/database.rb

Defined Under Namespace

Modules: Database, Import Classes: Batting, Player

Constant Summary collapse

VERSION =
"0.0.3"

Constants included from Database

Database::DEFAULT_ENV

Constants included from Import

Import::SEED_BATTINGS_CSV, Import::SEED_PLAYERS_CSV

Instance Attribute Summary

Attributes included from Database

#configuration, #db_dir, #migrations_paths

Instance Method Summary collapse

Methods included from Database

#connection, #env

Methods included from Import

#import_battings_from_csv, #import_players_from_csv, #seed_data

Instance Method Details

#most_improved_player(year) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/baseball_stats.rb', line 19

def most_improved_player(year)
  battings_for_year          = Batting.with_batting_average.where('year_id = ? AND at_bats >= 200', year).order(player_id: :asc)
  battings_for_previous_year = Batting.with_batting_average.where('year_id = ? AND at_bats >= 200', year-1).order(player_id: :asc)

  most_improved_batting_average = 0.0
  most_improved_player          = nil

  battings_for_year.each do |batting|
    next unless prev_year_batting = battings_for_previous_year.where(player_id: batting.player_id).first
    improvement = batting.batting_average - prev_year_batting.batting_average
    next unless improvement > most_improved_batting_average
    most_improved_batting_average = batting.batting_average
    most_improved_player = batting.player_id
  end

  Player.where(player_id: most_improved_player).first
end

#slugging_percentage_for_team_and_year(team_id, year) ⇒ Object



37
38
39
40
41
42
# File 'lib/baseball_stats.rb', line 37

def slugging_percentage_for_team_and_year(team_id, year)
  battings_for_team = Batting.includes(:player).with_slugging_percentage.where(team_id: team_id, year_id: year).order('slugging_percentage DESC')
  battings_for_team.collect {|batting|
    { player: batting.player_name, slugging_percentage: "#{batting.slugging_percentage.to_f.round(3)}" }
  }
end

#tripple_crown_winner(year, league) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/baseball_stats.rb', line 44

def tripple_crown_winner(year, league)
  highest_batting_average = Batting.with_batting_average.tripple_crown_eligible.where(year_id: year, league: league).order('batting_average DESC').first
  most_home_runs          = Batting.tripple_crown_eligible.where(year_id: year, league: league).order(home_runs: :desc).first
  most_rbis               = Batting.tripple_crown_eligible.where(year_id: year, league: league).order(runs_batted_in: :desc).first

  if highest_batting_average and most_home_runs and most_rbis and
     highest_batting_average.player_id == most_home_runs.player_id and highest_batting_average.player_id == most_rbis.player_id
    most_rbis.player
  else
    '(No winner)'
  end
end