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