Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session 2 Editorial #105

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 123 additions & 9 deletions session_2/cricviz/app/models/cricketer.rb
Original file line number Diff line number Diff line change
@@ -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)
#
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -68,14 +142,54 @@ 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.
#
# 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
6 changes: 6 additions & 0 deletions session_2/student_registry/app/models/student.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Student < ApplicationRecord
validates :cgpa, numericality: {
greater_than_equal_to: 0.0,
less_than_equal_to: 10.0
}
end
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precision - Number of digits.
Scale - Number of digits after the decimal point.

Since CGPA has two digits after the decimal point and lies between 0 and 10, it will have scale of 2 and precison of 4.

But precision of 4 implies that 12.34 is valid CGPA, after it has 4 digits (precision) and 2 digits after decimal (scale).

Therefore, I have added a validation in app/models/student.rb to limit CGPA between 0 and 10.

t.text :address
t.integer :admission_year

t.timestamps
end
end
end
26 changes: 26 additions & 0 deletions session_2/student_registry/db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.