Final wiring of the individual components
This commit is contained in:
parent
859619de72
commit
5719237879
31
Rakefile
31
Rakefile
@ -2,7 +2,38 @@
|
||||
|
||||
require 'bundler/gem_tasks'
|
||||
require 'rspec/core/rake_task'
|
||||
require 'planet_express'
|
||||
|
||||
RSpec::Core::RakeTask.new(: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
|
||||
|
27
lib/planet_express/shipment_discount_calculator.rb
Normal file
27
lib/planet_express/shipment_discount_calculator.rb
Normal 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
|
@ -11,7 +11,6 @@ module PlanetExpress
|
||||
where(provider: provider, package_size: package_size).first
|
||||
end
|
||||
|
||||
|
||||
def lowest_price_for_package_size(package_size)
|
||||
where(package_size: package_size).map(&:price_in_cents).min
|
||||
end
|
||||
|
58
spec/shipment_discount_calculator_spec.rb
Normal file
58
spec/shipment_discount_calculator_spec.rb
Normal 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
|
@ -5,6 +5,7 @@ require 'factory_bot'
|
||||
require 'simplecov'
|
||||
SimpleCov.start
|
||||
require 'planet_express'
|
||||
require 'rspec/mocks'
|
||||
|
||||
RSpec.configure do |config|
|
||||
# Enable flags like --only-failures and --next-failure
|
||||
@ -23,4 +24,6 @@ RSpec.configure do |config|
|
||||
config.before(:suite) do
|
||||
FactoryBot.find_definitions
|
||||
end
|
||||
|
||||
config.mock_with :rspec
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user