Class: BoxPacker::Container

Inherits:
Box
  • Object
show all
Defined in:
lib/box_packer/container.rb

Instance Attribute Summary collapse

Attributes inherited from Box

#dimensions, #position

Instance Method Summary collapse

Methods inherited from Box

#>=, #orient!, #sub_boxes

Constructor Details

#initialize(dimensions, opts = {}, &b) ⇒ Container

Returns a new instance of Container.



17
18
19
20
21
22
23
24
25
# File 'lib/box_packer/container.rb', line 17

def initialize(dimensions, opts = {}, &b)
  super(Dimensions[*dimensions])
  @label = opts[:label]
  @weight_limit = opts[:weight_limit]
  @packings_limit = opts[:packings_limit]
  @items = opts[:items] || []
  orient!
  instance_exec(&b) if b
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



15
16
17
# File 'lib/box_packer/container.rb', line 15

def items
  @items
end

#labelObject

Returns the value of attribute label.



14
15
16
# File 'lib/box_packer/container.rb', line 14

def label
  @label
end

#packed_successfullyObject (readonly)

Returns the value of attribute packed_successfully.



15
16
17
# File 'lib/box_packer/container.rb', line 15

def packed_successfully
  @packed_successfully
end

#packingObject (readonly)

Returns the value of attribute packing.



15
16
17
# File 'lib/box_packer/container.rb', line 15

def packing
  @packing
end

#packingsObject (readonly)

Returns the value of attribute packings.



15
16
17
# File 'lib/box_packer/container.rb', line 15

def packings
  @packings
end

#packings_limitObject

Returns the value of attribute packings_limit.



14
15
16
# File 'lib/box_packer/container.rb', line 14

def packings_limit
  @packings_limit
end

#weight_limitObject

Returns the value of attribute weight_limit.



14
15
16
# File 'lib/box_packer/container.rb', line 14

def weight_limit
  @weight_limit
end

Instance Method Details

#<<(item) ⇒ Object



34
35
36
# File 'lib/box_packer/container.rb', line 34

def <<(item)
  items << item.dup
end

#add_item(dimensions, opts = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/box_packer/container.rb', line 27

def add_item(dimensions, opts = {})
  quantity = opts.delete(:quantity) || 1
  quantity.times do
    items << Item.new(dimensions, opts)
  end
end

#draw!(filename, opts = {}) ⇒ Object



68
69
70
71
72
# File 'lib/box_packer/container.rb', line 68

def draw!(filename, opts = {})
  exporter = SVGExporter.new(self, opts)
  exporter.draw
  exporter.save(filename)
end

#new_packing!Object



53
54
55
56
# File 'lib/box_packer/container.rb', line 53

def new_packing!
  @packing = Packing.new(volume, weight_limit)
  @packings << @packing
end

#pack!Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/box_packer/container.rb', line 42

def pack!
  prepare_to_pack!
  return unless packable?
  if @packed_successfully = Packer.pack(self)
    packings.count
  else
    @packings = []
    false
  end
end

#to_sObject



58
59
60
61
62
63
64
65
66
# File 'lib/box_packer/container.rb', line 58

def to_s
  s = "\n|Container|"
  s << " #{label}" if label
  s << " #{dimensions}"
  s << " Weight Limit:#{weight_limit}" if weight_limit
  s << " Packings Limit:#{packings_limit}" if packings_limit
  s << "\n"
  s << (packed_successfully ? packings.map(&:to_s).join : '|         | Did Not Pack!')
end