class Corraios::Packer
Attributes
containers[R]
Public Class Methods
new()
click to toggle source
# File lib/corraios/packer.rb, line 7 def initialize() @containers = [] end
Public Instance Methods
add_item(attributes)
click to toggle source
# File lib/corraios/packer.rb, line 11 def add_item(attributes) container = build_container(attributes) add_to_containers container end
amount_of_posts()
click to toggle source
# File lib/corraios/packer.rb, line 20 def amount_of_posts @containers.size end
total_weight()
click to toggle source
# File lib/corraios/packer.rb, line 16 def total_weight @containers.inject(0) { |sum, item| sum + item.weight }.round(3) end
Private Instance Methods
add_envelope_or_roll(envelope)
click to toggle source
# File lib/corraios/packer.rb, line 48 def add_envelope_or_roll(envelope) unless using_envelope? && try_merge(first_empty, envelope) convert_containers_into_package add_package(envelope.to_package) end end
add_package(package)
click to toggle source
# File lib/corraios/packer.rb, line 59 def add_package(package) unless try_merge(first_empty, package) @containers << package end end
add_to_containers(container)
click to toggle source
# File lib/corraios/packer.rb, line 31 def add_to_containers(container) if @containers.empty? @containers << container else if container.is_a? :package convert_containers_into_package unless using_package? add_package(container) else add_envelope_or_roll container end end end
build_container(attributes)
click to toggle source
# File lib/corraios/packer.rb, line 25 def build_container(attributes) container = infers_container(attributes) raise UnknownMeasures unless container container.new(attributes) end
convert_containers_into_package()
click to toggle source
# File lib/corraios/packer.rb, line 55 def convert_containers_into_package @containers = @containers.map &:to_package end
first_empty()
click to toggle source
# File lib/corraios/packer.rb, line 44 def first_empty @containers.last end
infers_container(attributes)
click to toggle source
# File lib/corraios/packer.rb, line 86 def infers_container(attributes) [Containers::Package, Containers::Envelope, Containers::Roll].find do |clazz| clazz.valid_fields?(attributes) end end
pack_all_into_one_package(container)
click to toggle source
# File lib/corraios/packer.rb, line 80 def pack_all_into_one_package(container) weight_sum = total_weight + container.weight measures = { height: 2, length: container.length, width: container.width } @containers = [ Containers::Package.new(measures.merge(weight: weight_sum)) ] end
try_merge(container, new_container)
click to toggle source
# File lib/corraios/packer.rb, line 65 def try_merge(container, new_container) if container.can_merge? new_container container.merge! new_container true end end
using_envelope?()
click to toggle source
# File lib/corraios/packer.rb, line 76 def using_envelope? @containers.size == 1 && @containers[0].is_a?(:envelope) end
using_package?()
click to toggle source
# File lib/corraios/packer.rb, line 72 def using_package? @containers.all? { |container| container.is_a? :package } end