Implement discount budget for month repository
- Provide a repository to retrieve a discount budget for a given month
This commit is contained in:
parent
815615a03c
commit
c4528b1c92
@ -5,6 +5,7 @@ require 'planet_express/version'
|
|||||||
require 'planet_express/repository'
|
require 'planet_express/repository'
|
||||||
|
|
||||||
require 'planet_express/discount_budget'
|
require 'planet_express/discount_budget'
|
||||||
|
require 'planet_express/discount_budget_for_month_repository'
|
||||||
|
|
||||||
require 'planet_express/shipping_option'
|
require 'planet_express/shipping_option'
|
||||||
require 'planet_express/shipping_option_repository'
|
require 'planet_express/shipping_option_repository'
|
||||||
|
35
lib/planet_express/discount_budget_for_month_repository.rb
Normal file
35
lib/planet_express/discount_budget_for_month_repository.rb
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
module PlanetExpress
|
||||||
|
class DiscountBudgetForMonthRepository
|
||||||
|
DEFAULT_BUDGET = 1000
|
||||||
|
attr_reader :data
|
||||||
|
|
||||||
|
def initialize(data: {})
|
||||||
|
@data = data
|
||||||
|
end
|
||||||
|
|
||||||
|
def for_month(date)
|
||||||
|
return budget_for_month(date) if budget_for_month?(date)
|
||||||
|
|
||||||
|
add_budget_for_month(date)
|
||||||
|
budget_for_month(date)
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def budget_for_month(date)
|
||||||
|
data[start_of_month(date)]
|
||||||
|
end
|
||||||
|
|
||||||
|
def budget_for_month?(date)
|
||||||
|
!budget_for_month(date).nil?
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_budget_for_month(date)
|
||||||
|
data[start_of_month(date)] = DiscountBudget.new(budget: DEFAULT_BUDGET)
|
||||||
|
end
|
||||||
|
|
||||||
|
def start_of_month(date)
|
||||||
|
Date.new(date.year, date.month, 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
33
spec/discount_budget_for_month_spec.rb
Normal file
33
spec/discount_budget_for_month_spec.rb
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
RSpec.describe PlanetExpress::DiscountBudgetForMonthRepository do
|
||||||
|
subject { described_class.new }
|
||||||
|
let(:march_date_a) { Date.new(2021, 3, 6) }
|
||||||
|
let(:march_date_b) { Date.new(2021, 3, 6) }
|
||||||
|
|
||||||
|
describe '#for_month' do
|
||||||
|
|
||||||
|
it 'returns the same discount budget for the same month' do
|
||||||
|
expect(subject.for_month(march_date_a).object_id).to(
|
||||||
|
eq(subject.for_month(march_date_b).object_id))
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'not yet initialized month' do
|
||||||
|
it 'returns a discount budget' do
|
||||||
|
result = subject.for_month(march_date_a)
|
||||||
|
|
||||||
|
expect(result).to be_an_instance_of(PlanetExpress::DiscountBudget)
|
||||||
|
expect(result.available_budget).to eq(PlanetExpress::DiscountBudgetForMonthRepository::DEFAULT_BUDGET)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'other month' do
|
||||||
|
let(:april_date) { Date.new(2021, 4, 12) }
|
||||||
|
it 'returns a different discount budget' do
|
||||||
|
|
||||||
|
expect(subject.for_month(march_date_a).object_id).to_not(eq(
|
||||||
|
subject.for_month(april_date).object_id))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -5,7 +5,6 @@ RSpec.describe PlanetExpress::DiscountBudget do
|
|||||||
subject { described_class.new(budget: budget) }
|
subject { described_class.new(budget: budget) }
|
||||||
|
|
||||||
describe '#apply_discount' do
|
describe '#apply_discount' do
|
||||||
|
|
||||||
context 'with sufficient remaining budget' do
|
context 'with sufficient remaining budget' do
|
||||||
it 'returns the full discount' do
|
it 'returns the full discount' do
|
||||||
expect(subject.apply_discount(120)).to eq(120)
|
expect(subject.apply_discount(120)).to eq(120)
|
||||||
@ -14,7 +13,7 @@ RSpec.describe PlanetExpress::DiscountBudget do
|
|||||||
end
|
end
|
||||||
|
|
||||||
context 'with insufficient remaining budget' do
|
context 'with insufficient remaining budget' do
|
||||||
it 'returns the full discount' do
|
it 'returns only the remaining budget' do
|
||||||
expect(subject.apply_discount(650)).to eq(500)
|
expect(subject.apply_discount(650)).to eq(500)
|
||||||
expect(subject.available_budget).to eq(0)
|
expect(subject.available_budget).to eq(0)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user