Class: BoxPacker::Packing

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/box_packer/packing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(total_volume, total_weight) ⇒ Packing

Returns a new instance of Packing.



7
8
9
10
11
# File 'lib/box_packer/packing.rb', line 7

def initialize(total_volume, total_weight)
  super([])
  @remaining_volume = total_volume
  @remaining_weight = total_weight
end

Instance Attribute Details

#remaining_volumeObject (readonly)

Returns the value of attribute remaining_volume.



5
6
7
# File 'lib/box_packer/packing.rb', line 5

def remaining_volume
  @remaining_volume
end

#remaining_weightObject (readonly)

Returns the value of attribute remaining_weight.



5
6
7
# File 'lib/box_packer/packing.rb', line 5

def remaining_weight
  @remaining_weight
end

Instance Method Details

#<<(item) ⇒ Object



13
14
15
16
17
# File 'lib/box_packer/packing.rb', line 13

def <<(item)
  @remaining_volume -= item.volume
  @remaining_weight -= item.weight if weight?(item)
  super
end

#fit?(item) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/box_packer/packing.rb', line 19

def fit?(item)
  return false if include?(item)
  return false if remaining_volume < item.volume
  return false if weight?(item) && remaining_weight < item.weight
  true
end

#to_sObject



26
27
28
29
30
31
# File 'lib/box_packer/packing.rb', line 26

def to_s
  s = "|  Packing| Remaining Volume:#{remaining_volume}"
  s << " Remaining Weight:#{remaining_weight}" if remaining_weight
  s << "\n"
  s << map(&:to_s).join
end