Class: BoxPacker::Box

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/box_packer/box.rb

Direct Known Subclasses

Container, Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dimensions, opts = {}) ⇒ Box

Returns a new instance of Box.



9
10
11
12
# File 'lib/box_packer/box.rb', line 9

def initialize(dimensions, opts = {})
  @dimensions = dimensions
  @position = opts[:position] || Position[0, 0, 0]
end

Instance Attribute Details

#dimensionsObject

Returns the value of attribute dimensions.



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

def dimensions
  @dimensions
end

#positionObject

Returns the value of attribute position.



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

def position
  @position
end

Instance Method Details

#>=(other) ⇒ Object



21
22
23
# File 'lib/box_packer/box.rb', line 21

def >=(other)
  dimensions >= other.dimensions
end

#orient!Object



17
18
19
# File 'lib/box_packer/box.rb', line 17

def orient!
  @dimensions = Dimensions[*dimensions.to_a.sort!.reverse!]
end

#sub_boxes(item) ⇒ Object



25
26
27
28
29
# File 'lib/box_packer/box.rb', line 25

def sub_boxes(item)
  sub_boxes = sub_boxes_args(item).select { |(d, _)| d.volume > 0 }
  sub_boxes.map! { |args| Box.new(*args) }
  sub_boxes.sort_by!(&:volume).reverse!
end