Class: Boxify::BoxCollection
- Inherits:
-
Object
- Object
- Boxify::BoxCollection
- Defined in:
- lib/boxify/box.rb
Instance Attribute Summary collapse
-
#boxes ⇒ Object
readonly
Returns the value of attribute boxes.
Instance Method Summary collapse
- #box_with_minimum_height ⇒ Object
- #box_with_widest_surface_area ⇒ Object
- #delete(box) ⇒ Object
-
#find_biggest_box_with_minimum_height ⇒ Object
Find biggest (widest surface) box with minimum height.
- #find_eligible_boxes(space) ⇒ Object
- #flattened_dimensions ⇒ Object
-
#initialize(boxes:) ⇒ BoxCollection
constructor
A new instance of BoxCollection.
- #longest_edge ⇒ Object
- #more_than_one_box_with_widest_surface_area? ⇒ Boolean
- #second_longest_edge ⇒ Object
- #total_count ⇒ Object
- #unplaced ⇒ Object
Constructor Details
#initialize(boxes:) ⇒ BoxCollection
Returns a new instance of BoxCollection.
29 30 31 |
# File 'lib/boxify/box.rb', line 29 def initialize(boxes:) @boxes = boxes end |
Instance Attribute Details
#boxes ⇒ Object (readonly)
Returns the value of attribute boxes.
27 28 29 |
# File 'lib/boxify/box.rb', line 27 def boxes @boxes end |
Instance Method Details
#box_with_minimum_height ⇒ Object
62 63 64 |
# File 'lib/boxify/box.rb', line 62 def box_with_minimum_height boxes_sorted_by_height.first end |
#box_with_widest_surface_area ⇒ Object
58 59 60 |
# File 'lib/boxify/box.rb', line 58 def box_with_widest_surface_area boxes_sorted_by_surface_area.last end |
#delete(box) ⇒ Object
37 38 39 40 |
# 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 ⇒ Object
Find biggest (widest surface) box with minimum height
76 77 78 |
# 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) ⇒ Object
71 72 73 |
# 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 ⇒ Object
46 47 48 |
# 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 ⇒ Object
50 51 52 |
# File 'lib/boxify/box.rb', line 50 def longest_edge @longest_edge ||= flattened_dimensions.max end |
#more_than_one_box_with_widest_surface_area? ⇒ Boolean
66 67 68 69 |
# 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 ⇒ Object
54 55 56 |
# File 'lib/boxify/box.rb', line 54 def second_longest_edge @second_longest_edge ||= flattened_dimensions[-2] end |
#total_count ⇒ Object
42 43 44 |
# File 'lib/boxify/box.rb', line 42 def total_count boxes.map(&:total_count).inject(:+) end |
#unplaced ⇒ Object
33 34 35 |
# File 'lib/boxify/box.rb', line 33 def unplaced boxes.select{ |b| b.total_count > 0 } end |