-
Notifications
You must be signed in to change notification settings - Fork 46
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
Maddie Shields - Edges - Hotel #41
base: master
Are you sure you want to change the base?
Conversation
…ailable rooms and potentially remove Room class
…ck is before Today and to raise an error if so
HotelWhat We're Looking For
|
def initialize(input) | ||
@id = input[:id] | ||
@block_id = input[:block_id].nil? ? nil : input[:block_id] | ||
@room = input[:room] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ternary on line 9 is redundant. You check if input[:block_id]
is nil
, if it is use nil
explicitly otherwise use input[:block_id]
. Why not just say @block_id = input[:block_id]
?
private | ||
|
||
class OldStartDateError < StandardError; end | ||
class InvalidDateError < StandardError; end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that you made all these custom exceptions. You probably don't want to make them private
though, because then people outside this class won't be able to access it (for example to rescue those specific exceptions).
lib/reservation_tracker.rb
Outdated
def check_prior_today?(start_date) | ||
if start_date < Date.today | ||
raise OldStartDateError.new("You cannot have a start date prior to Today") | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue that these checks around the dates ought to be part of the responsibility of the DateRange
class.
lib/reservation_tracker.rb
Outdated
def check_dates_validity?(start_date, end_date) | ||
unless (start_date.is_a?(Date) && end_date.is_a?(Date)) | ||
raise InvalidDateError.new("That is not a Date type") | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The convention in Ruby is that methods that end in a ?
are expected to return either true or false. That's not quite what these are doing - raising exceptions is similar, but distinct, and the return value here isn't going to be a boolean.
def find_block_id(id) | ||
check_id(id) | ||
return @blocks.find { |block| block.id == id } | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of this method is a little misleading: it finds a block by ID, it does not find a block ID. Something like find_block
or find_block_by_id
might be better.
block_id = input[:block_id] | ||
room = get_first_available_room(requested_dates) | ||
|
||
reservation_data = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you need to do something different here if this room is part of a block? For example, making sure that the block has a room, and taking that room from the block?
def check_rate_validity?(rate) | ||
valid_types = /^(10|\d)(\.\d{1,2})?$/ | ||
if valid_types.match(rate.to_s) && !(rate.is_a?(Float) || rate.is_a?(Integer)) | ||
raise InvalidRateError.new("That is not a valid Rate amount") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are going a little overboard here on type checking here. Trust whoever gives you this variable to give you a value you can use, and trust Ruby to throw an error if something really bad happens. See Metz ch 5 for more on this.
def reservations_overlaps?(requested_dates) | ||
matching_reservations = @reservations.find_all do |reservation| | ||
date_range = reservation.date_range | ||
reservation if date_range.overlaps?(requested_dates) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have several things to say here.
First, this is a great example of loose coupling. Rather than pulling out the dates and doing a bunch of math yourself, you call a method and let the date range take care of it. Right on!
Second, I would like to see an explicit return here (i.e. return
instead of matching_reservations =
, or end with return matching_reservations
).
Third, I think you're somewhat misusing find_all
. The block to find_all
should produce a true or false value, whereas yours produces either the reservation
(if the date range overlaps) or the nil
(if it does not). This ends up working, but is not particularly clear. I would rewrite this loop as:
matching_reservations = @reservations.find_all do |reservation|
reservation.date_range.overlaps?(requested_dates)
end
describe "#overlaps? method (when it does overlap)" do | ||
it "checks if the range in a reservation overlaps with the requested_dates if the requested_dates for the same dates" do | ||
start_date = Date.today | ||
end_date = start_date + 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work implementing this long list of tests, I appreciate your diligence.
it "raises an error if there are no rooms available for requested dates" do | ||
reservation_tracker = Hotel::ReservationTracker.new | ||
|
||
17.times do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work getting this edge case!
Hotel
Congratulations! You're submitting your assignment!
Comprehension Questions