Class: BoxPacker::Item

Inherits:
Box
  • Object
show all
Defined in:
lib/box_packer/item.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 = {}) ⇒ Item

Returns a new instance of Item.



9
10
11
12
13
14
# File 'lib/box_packer/item.rb', line 9

def initialize(dimensions, opts = {})
  super(Dimensions[*dimensions])
  @label = opts[:label].to_s
  @weight = opts[:weight]
  @colour = opts[:colour] || '%06x' % (rand * 0xffffff)
end

Instance Attribute Details

#colourObject (readonly)

Returns the value of attribute colour.



7
8
9
# File 'lib/box_packer/item.rb', line 7

def colour
  @colour
end

#labelObject

Returns the value of attribute label.



6
7
8
# File 'lib/box_packer/item.rb', line 6

def label
  @label
end

#weightObject

Returns the value of attribute weight.



6
7
8
# File 'lib/box_packer/item.rb', line 6

def weight
  @weight
end

Instance Method Details

#rotate_to_fit_into(box) ⇒ Object



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

def rotate_to_fit_into(box)
  each_rotation do |rotation|
    if box.dimensions >= rotation
      @dimensions = rotation
      return true
    end
  end
  false
end

#to_sObject



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

def to_s
  s = '|     Item|'
  s << " #{label}" if label
  s << " #{dimensions} #{position} Volume:#{volume}"
  s << " Weight:#{weight}" if weight
  s << "\n"
end