Fix rubocop style offenses
This commit is contained in:
parent
3e64b35558
commit
574f186f0f
13
Rakefile
13
Rakefile
@ -8,10 +8,10 @@ RSpec::Core::RakeTask.new(:spec)
|
||||
|
||||
task default: :spec
|
||||
|
||||
task :process, [:input_file] do |t, args|
|
||||
task :process, [:input_file] do |_t, args|
|
||||
file = File.open(args.input_file, 'r')
|
||||
|
||||
shipping_option_repository = PlanetExpress::ShippingOptionRepository.new()
|
||||
shipping_option_repository = PlanetExpress::ShippingOptionRepository.new
|
||||
shipment_repository = PlanetExpress::ShipmentRepository.new(data: [])
|
||||
discount_budget_for_month_repository = PlanetExpress::DiscountBudgetForMonthRepository.new
|
||||
|
||||
@ -22,16 +22,19 @@ task :process, [:input_file] do |t, args|
|
||||
PlanetExpress::SShipmentDiscountRule.new(
|
||||
discount_budget_for_month_repository: discount_budget_for_month_repository,
|
||||
shipping_option_repository: shipping_option_repository,
|
||||
shipment_repository: shipment_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)
|
||||
shipment_repository: shipment_repository
|
||||
)
|
||||
]
|
||||
|
||||
PlanetExpress::ShipmentDiscountCalculator.new(
|
||||
shipment_repository: shipment_repository,
|
||||
rules: rules).run(shipment_reader)
|
||||
rules: rules
|
||||
).run(shipment_reader)
|
||||
|
||||
shipment_repository.each do |shipment|
|
||||
puts shipment_formatter.format(shipment)
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'date'
|
||||
|
||||
# Nasty monkey patching to make date objects know about the beginning of a month
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PlanetExpress
|
||||
Discount = Struct.new(:amount, :rule) do
|
||||
def inspect
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PlanetExpress
|
||||
class DiscountBudgetForMonthRepository
|
||||
DEFAULT_BUDGET = 1000
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PlanetExpress
|
||||
class Repository
|
||||
include Enumerable
|
||||
@ -12,10 +14,8 @@ module PlanetExpress
|
||||
data
|
||||
end
|
||||
|
||||
def each
|
||||
data.each do |item|
|
||||
yield item
|
||||
end
|
||||
def each(&block)
|
||||
data.each(&block)
|
||||
end
|
||||
|
||||
def add(item)
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PlanetExpress
|
||||
class LShipmentDiscountRule < Rule
|
||||
protected
|
||||
|
@ -8,7 +8,7 @@ module PlanetExpress
|
||||
|
||||
def initialize(discount_budget_for_month_repository:,
|
||||
shipping_option_repository:,
|
||||
shipment_repository: )
|
||||
shipment_repository:)
|
||||
@discount_budget_for_month_repository = discount_budget_for_month_repository
|
||||
@shipping_option_repository = shipping_option_repository
|
||||
@shipment_repository = shipment_repository
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PlanetExpress
|
||||
class SShipmentDiscountRule < Rule
|
||||
protected
|
||||
|
@ -1,6 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PlanetExpress
|
||||
class ShipmentDiscountCalculator
|
||||
attr_reader :shipment_repository, :rules
|
||||
|
||||
def initialize(shipment_repository:,
|
||||
rules:)
|
||||
@shipment_repository = shipment_repository
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PlanetExpress
|
||||
class InvalidShipment
|
||||
attr_reader :raw_string
|
||||
@ -6,8 +8,8 @@ module PlanetExpress
|
||||
@raw_string = raw_string
|
||||
end
|
||||
|
||||
def if_valid(&block)
|
||||
return
|
||||
def if_valid
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -38,7 +38,7 @@ module PlanetExpress
|
||||
shipping_option.provider
|
||||
end
|
||||
|
||||
def if_valid(&block)
|
||||
def if_valid
|
||||
yield self
|
||||
end
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PlanetExpress
|
||||
class ShipmentFormatter
|
||||
def format(shipment)
|
||||
@ -23,7 +25,7 @@ module PlanetExpress
|
||||
end
|
||||
|
||||
def format_money(amount)
|
||||
amount.to_s.rjust(3,'0').insert(-3, '.')
|
||||
amount.to_s.rjust(3, '0').insert(-3, '.')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'csv'
|
||||
|
||||
module PlanetExpress
|
||||
@ -16,7 +18,7 @@ module PlanetExpress
|
||||
shipping_option = find_shipping_option(row)
|
||||
|
||||
if date.nil? || shipping_option.nil?
|
||||
yield InvalidShipment.new(row.join(" "))
|
||||
yield InvalidShipment.new(row.join(' '))
|
||||
else
|
||||
yield Shipment.new(date, shipping_option)
|
||||
end
|
||||
@ -30,11 +32,9 @@ module PlanetExpress
|
||||
end
|
||||
|
||||
def parse_date(row)
|
||||
begin
|
||||
return Date.iso8601(row[0])
|
||||
rescue ArgumentError => _e
|
||||
nil
|
||||
end
|
||||
Date.iso8601(row[0])
|
||||
rescue ArgumentError => _e
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,3 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module PlanetExpress
|
||||
class ShipmentRepository < Repository
|
||||
def rule_applied_in_month?(month, rule)
|
||||
|
@ -3,6 +3,7 @@
|
||||
module PlanetExpress
|
||||
class ShippingOptionRepository < Repository
|
||||
attr_reader :data
|
||||
|
||||
def initialize(data: nil)
|
||||
@data = data || default_data
|
||||
end
|
||||
|
@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
|
||||
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
||||
spec.require_paths = ['lib']
|
||||
|
||||
spec.add_development_dependency('rubocop', '~> 1.11')
|
||||
spec.add_development_dependency('factory_bot', '~> 6.1')
|
||||
spec.add_development_dependency('rubocop', '~> 1.11')
|
||||
spec.add_development_dependency('simplecov', '~> 0.21')
|
||||
end
|
||||
|
@ -6,10 +6,10 @@ RSpec.describe PlanetExpress::DiscountBudgetForMonthRepository do
|
||||
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))
|
||||
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
|
||||
@ -24,9 +24,9 @@ RSpec.describe PlanetExpress::DiscountBudgetForMonthRepository do
|
||||
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))
|
||||
subject.for_month(april_date).object_id
|
||||
))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,7 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
FactoryBot.define do
|
||||
factory :shipping_option, class: PlanetExpress::ShippingOption do
|
||||
sequence(:provider) { 'LP' }
|
||||
sequence(:package_size) { ['S', 'M', 'L'].sample }
|
||||
sequence(:package_size) { %w[S M L].sample }
|
||||
sequence(:price_in_cents) { |i| i * 100 }
|
||||
end
|
||||
end
|
||||
|
@ -9,7 +9,8 @@ RSpec.describe PlanetExpress::LShipmentDiscountRule do
|
||||
described_class.new(
|
||||
discount_budget_for_month_repository: budget_repo,
|
||||
shipping_option_repository: shipping_option_repo,
|
||||
shipment_repository: shipment_repo)
|
||||
shipment_repository: shipment_repo
|
||||
)
|
||||
end
|
||||
|
||||
let(:correct_shipping_option) do
|
||||
|
@ -13,13 +13,12 @@ RSpec.describe PlanetExpress::Repository do
|
||||
|
||||
subject { described_class.new(data: entries) }
|
||||
|
||||
|
||||
describe '#where' do
|
||||
context 'no data' do
|
||||
let(:entries) { [] }
|
||||
|
||||
it 'returns an empty array' do
|
||||
expect(subject.where()).to eq([])
|
||||
expect(subject.where).to eq([])
|
||||
end
|
||||
end
|
||||
|
||||
@ -29,7 +28,7 @@ RSpec.describe PlanetExpress::Repository do
|
||||
end
|
||||
|
||||
it 'returns all items' do
|
||||
expect(subject.where()).to eq(subject.all)
|
||||
expect(subject.where).to eq(subject.all)
|
||||
end
|
||||
end
|
||||
|
||||
@ -38,10 +37,10 @@ RSpec.describe PlanetExpress::Repository do
|
||||
repository_item_class.new(150, 'Testing')
|
||||
end
|
||||
|
||||
let(:entries) { repository_items + [expected_item] }
|
||||
let(:entries) { repository_items + [expected_item] }
|
||||
|
||||
it 'returns a list with the expected item' do
|
||||
expect(subject.where(test_attribute_a: 150, test_attribute_b: "Testing")).to eq([expected_item])
|
||||
expect(subject.where(test_attribute_a: 150, test_attribute_b: 'Testing')).to eq([expected_item])
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -9,11 +9,11 @@ RSpec.describe PlanetExpress::SShipmentDiscountRule do
|
||||
described_class.new(
|
||||
discount_budget_for_month_repository: budget_repo,
|
||||
shipping_option_repository: shipping_option_repo,
|
||||
shipment_repository: shipment_repo)
|
||||
shipment_repository: shipment_repo
|
||||
)
|
||||
end
|
||||
|
||||
context 's_shipment' do
|
||||
|
||||
context 'more expensive than cheapest option' do
|
||||
let(:shipping_option) do
|
||||
shipping_option_repo.find_by_provider_and_package_size('MR', 'S')
|
||||
|
@ -5,7 +5,7 @@ RSpec.describe PlanetExpress::ShipmentDiscountCalculator do
|
||||
PlanetExpress::ShippingOptionRepository.new(data: [])
|
||||
end
|
||||
let(:rule) do
|
||||
double("Rule")
|
||||
double('Rule')
|
||||
end
|
||||
let(:rules) do
|
||||
[rule]
|
||||
|
@ -8,7 +8,7 @@ RSpec.describe PlanetExpress::ShipmentFormatter do
|
||||
describe '#format' do
|
||||
context 'invalid shipment' do
|
||||
let(:shipment) do
|
||||
PlanetExpress::InvalidShipment.new("Invalid Shipment")
|
||||
PlanetExpress::InvalidShipment.new('Invalid Shipment')
|
||||
end
|
||||
|
||||
it 'returns a well formatted string' do
|
||||
|
@ -9,7 +9,7 @@ RSpec.describe PlanetExpress::ShipmentReader do
|
||||
|
||||
describe '#each' do
|
||||
context 'with a file' do
|
||||
let(:input) { File.read(File.expand_path('../fixtures/shipment_input_file.txt', __FILE__)) }
|
||||
let(:input) { File.read(File.expand_path('fixtures/shipment_input_file.txt', __dir__)) }
|
||||
let(:result) { subject.to_a }
|
||||
|
||||
it 'parses the file' do
|
||||
|
@ -17,7 +17,7 @@ RSpec.describe PlanetExpress::ShipmentRepository do
|
||||
end
|
||||
|
||||
context 'with invalid shipments' do
|
||||
let(:items) { [PlanetExpress::InvalidShipment.new("")] }
|
||||
let(:items) { [PlanetExpress::InvalidShipment.new('')] }
|
||||
it 'returns false' do
|
||||
result = subject.rule_applied_in_month?(Date.today.begin_of_month,
|
||||
PlanetExpress::Rule)
|
||||
@ -53,7 +53,7 @@ RSpec.describe PlanetExpress::ShipmentRepository do
|
||||
end
|
||||
|
||||
let(:other_rule) do
|
||||
-> () {}
|
||||
-> {}
|
||||
end
|
||||
|
||||
let(:shipment) do
|
||||
@ -80,18 +80,20 @@ RSpec.describe PlanetExpress::ShipmentRepository do
|
||||
it 'returns 0' do
|
||||
result = subject.l_shipment_count_for_provider_and_month(
|
||||
'LP',
|
||||
Date.today.begin_of_month)
|
||||
Date.today.begin_of_month
|
||||
)
|
||||
|
||||
expect(result).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with invalid shipments' do
|
||||
let(:items) { [PlanetExpress::InvalidShipment.new("")] }
|
||||
let(:items) { [PlanetExpress::InvalidShipment.new('')] }
|
||||
it 'returns 0' do
|
||||
result = subject.l_shipment_count_for_provider_and_month(
|
||||
'LP',
|
||||
Date.today.begin_of_month)
|
||||
Date.today.begin_of_month
|
||||
)
|
||||
|
||||
expect(result).to eq(0)
|
||||
end
|
||||
@ -142,9 +144,6 @@ RSpec.describe PlanetExpress::ShipmentRepository do
|
||||
expect(result).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user