Guest User

Untitled

a guest
May 27th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. module Groupments
  4. class GroupShipments
  5.  
  6. def self.call(shipments:)
  7. new(shipments: shipments).call.grouped_shipments
  8. end
  9.  
  10. attr_reader :grouped_shipments
  11.  
  12. def initialize(shipments:)
  13. @shipments = shipments
  14. end
  15.  
  16. def call
  17. group_shipments
  18.  
  19. self
  20. end
  21.  
  22. private
  23.  
  24. attr_reader :shipments
  25.  
  26. def group_shipments
  27. @grouped_shipments = (non_groupable_shipments +
  28. grouped_courier_shipments +
  29. grouped_parcel_shipments
  30. ).compact
  31. end
  32.  
  33. def non_groupable_shipments
  34. shipments - courier_shipments - groupable_parcel_shipments
  35. end
  36.  
  37. def grouped_courier_shipments
  38. [courier_shipment]
  39. end
  40.  
  41. def grouped_parcel_shipments
  42. [parcel_shipment_without_hard_goods]
  43. end
  44.  
  45. def parcel_shipment_without_hard_goods
  46. build_shipment_items_for(groupable_parcel_shipments)
  47. end
  48.  
  49. def courier_shipment
  50. build_shipment_items_for(courier_shipments)
  51. end
  52.  
  53. def courier_shipments
  54. @_courier_shipments ||= shipments.select(&:courier?)
  55. end
  56.  
  57. def groupable_parcel_shipments
  58. @_groupable_parcel_shipments ||= shipments.select do |shipment|
  59. shipment.parcel? && !shipment.hard_goods?
  60. end
  61. end
  62.  
  63. def build_shipment_items_for(shipments)
  64. return if shipments.empty?
  65.  
  66. shipments.first.tap do |shipment|
  67. shipment.shipment_items = shipments.flat_map(&:shipment_items)
  68. end
  69. end
  70. end
  71. end
Add Comment
Please, Sign In to add comment