class Boxify::BoxCollection

Attributes

boxes[R]

Public Class Methods

new(boxes:) click to toggle source
# File lib/boxify/box.rb, line 29
def initialize(boxes:)
  @boxes = boxes
end

Public Instance Methods

box_with_minimum_height() click to toggle source
# File lib/boxify/box.rb, line 62
def box_with_minimum_height
  boxes_sorted_by_height.first
end
box_with_widest_surface_area() click to toggle source
# File lib/boxify/box.rb, line 58
def box_with_widest_surface_area
  boxes_sorted_by_surface_area.last
end
delete(box) click to toggle source
# File lib/boxify/box.rb, line 37
def delete(box)
  return if box.total_count == 0
  box.total_count -= 1
end
find_biggest_box_with_minimum_height() click to toggle source

Find biggest (widest surface) box with minimum height

# File lib/boxify/box.rb, line 76
def find_biggest_box_with_minimum_height
  more_than_one_box_with_widest_surface_area? ? box_with_minimum_height : box_with_widest_surface_area
end
find_eligible_boxes(space) click to toggle source
# File lib/boxify/box.rb, line 71
def find_eligible_boxes(space)
  unplaced.select{ |b| (b.width <= space.width) && (b.height <= space.height) }
end
flattened_dimensions() click to toggle source
# File lib/boxify/box.rb, line 46
def flattened_dimensions
  @flattened_dimensions ||= boxes.map { |b| [b.width, b.height, b.depth]}.flatten.uniq.sort
end
longest_edge() click to toggle source
# File lib/boxify/box.rb, line 50
def longest_edge
  @longest_edge ||= flattened_dimensions.max
end
more_than_one_box_with_widest_surface_area?() click to toggle source
# File lib/boxify/box.rb, line 66
def more_than_one_box_with_widest_surface_area?
  number_of_boxes = unplaced.select {|b| b.surface_area == box_with_widest_surface_area.surface_area }.count
  number_of_boxes > 1
end
second_longest_edge() click to toggle source
# File lib/boxify/box.rb, line 54
def second_longest_edge
  @second_longest_edge ||= flattened_dimensions[-2]
end
total_count() click to toggle source
# File lib/boxify/box.rb, line 42
def total_count
  boxes.map(&:total_count).inject(:+)
end
unplaced() click to toggle source
# File lib/boxify/box.rb, line 33
def unplaced
  boxes.select{ |b| b.total_count > 0 }
end

Private Instance Methods

boxes_sorted_by_height() click to toggle source
# File lib/boxify/box.rb, line 86
def boxes_sorted_by_height
  unplaced.sort { |x,y| x.height <=> y.height }
end
boxes_sorted_by_surface_area() click to toggle source
# File lib/boxify/box.rb, line 82
def boxes_sorted_by_surface_area
  unplaced.sort { |x,y| x.surface_area <=> y.surface_area }
end