diff --git a/session_2/cricviz/app/models/cricketer.rb b/session_2/cricviz/app/models/cricketer.rb index 7084b0e5..4abcf42c 100644 --- a/session_2/cricviz/app/models/cricketer.rb +++ b/session_2/cricviz/app/models/cricketer.rb @@ -1,15 +1,15 @@ class Cricketer < ApplicationRecord # Select players from the country 'Australia' - scope :australian_players, -> { raise NotImplementedError } + scope :australian_players, -> { where(country: 'Australia') } # Select players with the role 'Batter' - scope :batters, -> { raise NotImplementedError } + scope :batters, -> { where(role: 'Batter') } # Select players with the role 'Bowler' - scope :bowlers, -> { raise NotImplementedError } + scope :bowlers, -> { where(role: 'Bowler') } # Sort players by the descending number of matches played - scope :descending_by_matches, -> { raise NotImplementedError } + scope :descending_by_matches, -> { order(matches: :desc) } # Batting average: Runs scored / (Number of innings in which player has been out) # @@ -19,7 +19,11 @@ class Cricketer < ApplicationRecord # - If the player has not batted yet, return nil # - If the player has been not out in all innings, return runs scored. def batting_average - raise NotImplementedError + return nil if runs_scored.nil? || innings_batted.nil? || not_out.nil? + + return runs_scored if innings_batted == not_out + + runs_scored.fdiv(innings_batted - not_out) end # Batting strike rate: (Runs Scored x 100) / (Balls Faced) @@ -29,12 +33,82 @@ def batting_average # data is incomplete # - If the player has not batted yet, return nil def batting_strike_rate - raise NotImplementedError + return nil if runs_scored.nil? || balls_faced.nil? || balls_faced == 0 + + (runs_scored * 100).fdiv(balls_faced) end # Create records for the classical batters def self.import_classical_batters - raise NotImplementedError + Cricketer.create( + name: 'Sachin Tendulkar', + country: 'India', + role: 'Batter', + matches: 200, + innings_batted: 329, + not_out: 33, + runs_scored: 15921, + balls_faced: nil, + high_score: 248, + centuries: 51, + half_centuries: 68 + ) + + Cricketer.create( + name: 'Rahul Dravid', + country: 'India', + role: 'Batter', + matches: 164, + innings_batted: 286, + not_out: 32, + runs_scored: 13288, + balls_faced: 31258, + high_score: 270, + centuries: 36, + half_centuries: 63 + ) + + Cricketer.create( + name: 'Kumar Sangakkara', + country: 'Sri Lanka', + role: 'Wicketkeeper', + matches: 134, + innings_batted: 233, + not_out: 17, + runs_scored: 12400, + balls_faced: 22882, + high_score: 319, + centuries: 38, + half_centuries: 52 + ) + + Cricketer.create( + name: 'Ricky Ponting', + country: 'Australia', + role: 'Batter', + matches: 168, + innings_batted: 287, + not_out: 29, + runs_scored: 13378, + balls_faced: 22782, + high_score: 257, + centuries: 41, + half_centuries: 62 + ) + + Cricketer.create( + name: 'Brian Lara', + country: 'West Indies', + role: 'Batter', + matches: 131, + innings_batted: 232, + not_out: 6, + runs_scored: 11953, + balls_faced: 19753, + high_score: 400, + centuries: 34, + half_centuries: 48 + ) end # Update the current data with an innings scorecard. @@ -68,7 +142,43 @@ def self.import_classical_batters # `ActiveRecord::RecordNotFound` exception with the player's name as # the message. def self.update_innings(batting_scorecard, bowling_scorecard) - raise NotImplementedError + batting_scorecard.each do |row| + player = Cricketer.find_by(name: row[0]) + + raise ActiveRecord::RecordNotFound, row[0] if player.nil? + + player.not_out += 1 if !row[1] + + player.runs_scored += row[2] + player.high_score = row[2] if row[2] > player.high_score + + if row[2] >= 100 + player.centuries += 1 + elsif row[2] >= 50 + player.half_centuries += 1 + end + + player.innings_batted += 1 if row[3] + player.balls_faced += row[3] + + player.fours_scored += row[4] + player.sixes_scored += row[5] + + player.save + end + + bowling_scorecard.each do |row| + player = Cricketer.find_by(name: row[0]) + + raise ActiveRecord::RecordNotFound, row[0] if player.nil? + + player.innings_bowled += 1 + player.balls_bowled += row[1] + player.runs_given += row[3] + player.wickets_taken += row[4] + + player.save + end end # Delete the record associated with a player. @@ -76,6 +186,10 @@ def self.update_innings(batting_scorecard, bowling_scorecard) # Note: If you cannot find a player with given name, raise an # `ActiveRecord::RecordNotFound` exception. def self.ban(name) - raise NotImplementedError + player = Cricketer.find_by(name: name) + + raise ActiveRecord::RecordNotFound, name if player.nil? + + player.destroy end end diff --git a/session_2/student_registry/app/models/student.rb b/session_2/student_registry/app/models/student.rb new file mode 100644 index 00000000..13e92d16 --- /dev/null +++ b/session_2/student_registry/app/models/student.rb @@ -0,0 +1,6 @@ +class Student < ApplicationRecord + validates :cgpa, numericality: { + greater_than_equal_to: 0.0, + less_than_equal_to: 10.0 + } +end diff --git a/session_2/student_registry/db/migrate/20210123124824_create_students.rb b/session_2/student_registry/db/migrate/20210123124824_create_students.rb new file mode 100644 index 00000000..e6429f7e --- /dev/null +++ b/session_2/student_registry/db/migrate/20210123124824_create_students.rb @@ -0,0 +1,14 @@ +class CreateStudents < ActiveRecord::Migration[6.1] + def change + create_table :students do |t| + t.string :name + t.string :roll_number + t.string :branch + t.decimal :cgpa, precision: 4, scale: 2 + t.text :address + t.integer :admission_year + + t.timestamps + end + end +end diff --git a/session_2/student_registry/db/schema.rb b/session_2/student_registry/db/schema.rb new file mode 100644 index 00000000..00bc80ad --- /dev/null +++ b/session_2/student_registry/db/schema.rb @@ -0,0 +1,26 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2021_01_23_124824) do + + create_table "students", force: :cascade do |t| + t.string "name" + t.string "roll_number" + t.string "branch" + t.decimal "cgpa", precision: 4, scale: 2 + t.text "address" + t.integer "admission_year" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + +end