1
0

Final wiring of the individual components

This commit is contained in:
Tim Kächele 2021-03-07 22:16:25 +01:00
parent 859619de72
commit 5719237879
5 changed files with 119 additions and 1 deletions

View File

@ -2,7 +2,38 @@
require 'bundler/gem_tasks' require 'bundler/gem_tasks'
require 'rspec/core/rake_task' require 'rspec/core/rake_task'
require 'planet_express'
RSpec::Core::RakeTask.new(:spec) RSpec::Core::RakeTask.new(:spec)
task default: :spec task default: :spec
task :process, [:input_file] do |t, args|
file = File.open(args.input_file, 'r')
shipping_option_repository = PlanetExpress::ShippingOptionRepository.new()
shipment_repository = PlanetExpress::ShipmentRepository.new(data: [])
discount_budget_for_month_repository = PlanetExpress::DiscountBudgetForMonthRepository.new
shipment_reader = PlanetExpress::ShipmentReader.new(file, shipping_option_repository)
shipment_formatter = PlanetExpress::ShipmentFormatter.new
rules = [
PlanetExpress::SShipmentDiscountRule.new(
discount_budget_for_month_repository: discount_budget_for_month_repository,
shipping_option_repository: shipping_option_repository,
shipment_repository: shipment_repository),
PlanetExpress::LShipmentDiscountRule.new(
discount_budget_for_month_repository: discount_budget_for_month_repository,
shipping_option_repository: shipping_option_repository,
shipment_repository: shipment_repository)
]
PlanetExpress::ShipmentDiscountCalculator.new(
shipment_repository: shipment_repository,
rules: rules).run(shipment_reader)
shipment_repository.each do |shipment|
puts shipment_formatter.format(shipment)
end
end

View File

@ -0,0 +1,27 @@
module PlanetExpress
class ShipmentDiscountCalculator
attr_reader :shipment_repository, :rules
def initialize(shipment_repository:,
rules:)
@shipment_repository = shipment_repository
@rules = rules
end
def run(shipment_reader)
shipment_reader.each do |shipment|
shipment.if_valid do |valid_shipment|
apply_rules(valid_shipment)
end
shipment_repository.add(shipment)
end
end
private
def apply_rules(valid_shipment)
rules.each do |rule|
rule.apply_to(valid_shipment)
end
end
end
end

View File

@ -11,7 +11,6 @@ module PlanetExpress
where(provider: provider, package_size: package_size).first where(provider: provider, package_size: package_size).first
end end
def lowest_price_for_package_size(package_size) def lowest_price_for_package_size(package_size)
where(package_size: package_size).map(&:price_in_cents).min where(package_size: package_size).map(&:price_in_cents).min
end end

View File

@ -0,0 +1,58 @@
# frozen_string_literal: true
RSpec.describe PlanetExpress::ShipmentDiscountCalculator do
let(:shipment_repository) do
PlanetExpress::ShippingOptionRepository.new(data: [])
end
let(:rule) do
double("Rule")
end
let(:rules) do
[rule]
end
subject do
described_class.new(shipment_repository: shipment_repository,
rules: rules)
end
describe '#run' do
context 'with valid shipment' do
let(:shipment_reader) do
[PlanetExpress::Shipment.new(Date.today, build(:shipping_option))]
end
it 'does call the rule on the invalid shipment' do
expect(rule).to receive(:apply_to)
subject.run(shipment_reader)
end
it 'adds the shipment to the shipment_repository' do
expect(rule).to receive(:apply_to)
expect do
subject.run(shipment_reader)
end.to(change { shipment_repository.all.length }.by(1))
end
end
context 'with invalid shipment' do
let(:shipment_reader) do
[PlanetExpress::InvalidShipment.new('Invalid')]
end
it 'does not call the rule with the invalid shipment' do
expect(rule).to_not receive(:apply_to)
subject.run(shipment_reader)
end
it 'adds the shipment to the shipment_repository' do
expect do
subject.run(shipment_reader)
end.to(change { shipment_repository.all.length }.by(1))
end
end
end
end

View File

@ -5,6 +5,7 @@ require 'factory_bot'
require 'simplecov' require 'simplecov'
SimpleCov.start SimpleCov.start
require 'planet_express' require 'planet_express'
require 'rspec/mocks'
RSpec.configure do |config| RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure # Enable flags like --only-failures and --next-failure
@ -23,4 +24,6 @@ RSpec.configure do |config|
config.before(:suite) do config.before(:suite) do
FactoryBot.find_definitions FactoryBot.find_definitions
end end
config.mock_with :rspec
end end