Last active
November 18, 2024 14:38
-
-
Save cored/fb4513fb43bd9adaa0ecff0da6b4c972 to your computer and use it in GitHub Desktop.
inventory.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Inventory | |
NotCheckedInCustomer = Class.new(StandardError) | |
def initialize | |
@customers = [] | |
end | |
Customer = Struct.new(:license_number, | |
:usage_type, | |
:checkin_time, | |
:checkout_time, keyword_init: true) do | |
def self.of(license_number:, usage_type:) | |
new(license_number: license_number, usage_type: usage_type, checkin_time: Time.now) | |
end | |
def wait_time | |
checkout_time - checkin_time | |
end | |
end | |
def checkin(license_number:, usage_type:) | |
customer = Customer.of(license_number: license_number, | |
usage_type: usage_type) | |
@customers << customer | |
customer | |
end | |
def checkout(customer) | |
raise NotCheckedInCustomer unless @customers.include?(customer) | |
customer.checkout_time = Time.now | |
end | |
def past_seven_days_customers | |
customers.select do |customer| | |
customer.checkin_time > Time.now - 7 * 24 * 60 * 60 | |
end | |
end | |
private | |
attr_reader :customers | |
end | |
RSpec.describe Inventory do | |
subject(:inventory) { described_class.new } | |
describe "#checkin" do | |
it "checks in a new customer" do | |
expect( | |
inventory.checkin( | |
license_number: "AA002", | |
usage_type: "Recreational").license_number | |
).to eq("AA002") | |
end | |
it "records the time of checkin" do | |
expect( | |
inventory.checkin( | |
license_number: "AA002", | |
usage_type: "Recreational").checkin_time | |
).to be_within(1).of(Time.now) | |
end | |
end | |
describe "#checkout" do | |
it "checks out by adding a checkout time" do | |
customer = inventory.checkin(license_number: "AA002", | |
usage_type: "Recreational") | |
expect(inventory.checkout(customer)).to be_within(1).of(Time.now) | |
end | |
context "checking out without a checkin customer" do | |
it "raises an error" do | |
customer = Inventory::Customer.new | |
expect { inventory.checkout(customer) }.to raise_error(Inventory::NotCheckedInCustomer) | |
end | |
end | |
end | |
describe "#past_seven_days_customers" do | |
it "returns all customers with wait time within the past seven days" do | |
customer1 = inventory.checkin(license_number: "AA002", | |
usage_type: "Recreational") | |
customer2 = inventory.checkin(license_number: "AA003", | |
usage_type: "Recreational") | |
customer3 = inventory.checkin(license_number: "AA004", | |
usage_type: "Recreational") | |
inventory.checkout(customer1) | |
inventory.checkout(customer2) | |
inventory.checkout(customer3) | |
expect(inventory.past_seven_days_customers).to eq([customer1, customer2, customer3]) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment