class Boxxer::Handler
Attributes
containers[R]
Public Class Methods
new(containers:, contents:)
click to toggle source
# File lib/boxxer/handler.rb, line 7 def initialize(containers:, contents:) @available_containers = containers.sort_by { |container| container[:net_limit] } @largest_container = @available_containers.last @contents = contents.sort_by { |content| content[:weight] }.reverse @containers = [] end
Public Instance Methods
call()
click to toggle source
# File lib/boxxer/handler.rb, line 14 def call raise Error, "Largest container net_limit is #{@largest_container[:net_limit]}, but maximum weight is #{content_max_weight}" if invalid_options? until @contents.empty? do container = Container.new(matching_container) complete_container(container) @containers << container end self end
container_count()
click to toggle source
# File lib/boxxer/handler.rb, line 33 def container_count @containers.count end
total_gross_weight()
click to toggle source
# File lib/boxxer/handler.rb, line 25 def total_gross_weight @containers.sum(&:gross_weight).truncate(3) end
total_net_weight()
click to toggle source
# File lib/boxxer/handler.rb, line 29 def total_net_weight @containers.sum(&:net_weight).truncate(3) end
Private Instance Methods
complete_container(container)
click to toggle source
# File lib/boxxer/handler.rb, line 39 def complete_container(container) loop do fittable_conteiner = @contents.find { |content| container.fittable?(content[:weight]) } fittable_conteiner.nil? ? break : container.add_content(@contents.delete_at(@contents.index(fittable_conteiner))) end end
content_max_weight()
click to toggle source
# File lib/boxxer/handler.rb, line 46 def content_max_weight @contents.max { |c| c[:weight] } end
invalid_options?()
click to toggle source
# File lib/boxxer/handler.rb, line 50 def invalid_options? content_max_weight[:weight] > @largest_container[:net_limit] end
matching_container()
click to toggle source
# File lib/boxxer/handler.rb, line 54 def matching_container @available_containers.find { |container| container[:net_limit] >= @contents.sum { |content| content[:weight] } } || @largest_container end