Class: Boxify::BoxCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/boxify/box.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#boxesObject (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_heightObject



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_areaObject



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_heightObject

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_dimensionsObject



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_edgeObject



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

Returns:

  • (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_edgeObject



54
55
56
# File 'lib/boxify/box.rb', line 54

def second_longest_edge
  @second_longest_edge ||= flattened_dimensions[-2]
end

#total_countObject



42
43
44
# File 'lib/boxify/box.rb', line 42

def total_count
  boxes.map(&:total_count).inject(:+)
end

#unplacedObject



33
34
35
# File 'lib/boxify/box.rb', line 33

def unplaced
  boxes.select{ |b| b.total_count > 0 }
end