39 lines
1.0 KiB
Ruby
39 lines
1.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module PlanetExpress
|
|
class ShipmentRepository < Repository
|
|
attr_reader :valid_l_shipments_by_month, :valid_shipments_by_month
|
|
|
|
def initialize(data:)
|
|
@data = []
|
|
@valid_l_shipments_by_month = Hash.new { |hash, key| hash[key] = [] }
|
|
@valid_shipments_by_month = Hash.new { |hash, key| hash[key] = [] }
|
|
data.each do |entry|
|
|
add(entry)
|
|
end
|
|
end
|
|
|
|
def rule_applied_in_month?(month, rule)
|
|
valid_shipments_by_month[month].any? do |shipment|
|
|
shipment.discounts.any? { |discount| discount.rule.instance_of?(rule) }
|
|
end
|
|
end
|
|
|
|
def l_shipment_count_for_provider_and_month(provider, month)
|
|
valid_l_shipments_by_month[month].select do |shipment|
|
|
shipment.provider == provider
|
|
end.length
|
|
end
|
|
|
|
def add(item)
|
|
data.push(item)
|
|
item.if_valid do
|
|
valid_shipments_by_month[item.month].push(item)
|
|
if item.l_shipment?
|
|
valid_l_shipments_by_month[item.month].push(item)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|