Class: Physical::Box

Inherits:
Cuboid show all
Defined in:
lib/physical/box.rb

Overview

Represents a physical box which items can be packed into.

Constant Summary collapse

DEFAULT_LENGTH =

The default dimensions of this box when unspecified

BigDecimal::INFINITY
DEFAULT_MAX_WEIGHT =

The default maximum weight of this box when unspecified

BigDecimal::INFINITY

Instance Attribute Summary collapse

Attributes inherited from Cuboid

#dimensions, #height, #id, #length, #properties, #weight, #width

Instance Method Summary collapse

Methods inherited from Cuboid

#==, #density, #volume

Constructor Details

#initialize(**kwargs) ⇒ Box

Returns a new instance of Box.

Parameters:

  • kwargs (Hash)

    ID, dimensions, weight, and properties

Options Hash (**kwargs):

  • :id (String)

    a unique identifier for this box

  • :dimensions (Array<Measured::Length>)

    the outer length, width, and height of this box

  • :inner_dimensions (Array<Measured::Length>)

    the inner length, width, and height of this box

  • :weight (Measured::Weight)

    the weight of the box itself (excluding what's inside)

  • :max_weight (Measured::Weight)

    the maximum weight this box can handle

  • :properties (Hash)

    additional custom properties for this box



41
42
43
44
45
46
47
48
# File 'lib/physical/box.rb', line 41

def initialize(**kwargs)
  inner_dimensions = kwargs.delete(:inner_dimensions) || []
  max_weight = kwargs.delete(:max_weight) || Measured::Weight(DEFAULT_MAX_WEIGHT, :g)
  super(**kwargs)
  @inner_dimensions = fill_dimensions(Types::Dimensions[inner_dimensions])
  @inner_length, @inner_width, @inner_height = *@inner_dimensions
  @max_weight = Types::Weight[max_weight]
end

Instance Attribute Details

#inner_dimensionsArray<Measured::Length> (readonly)

The inner length, width, and height of this box

Returns:

  • (Array<Measured::Length>)


16
17
18
# File 'lib/physical/box.rb', line 16

def inner_dimensions
  @inner_dimensions
end

#inner_heightMeasured::Length (readonly)

The inner height of this box

Returns:

  • (Measured::Length)


28
29
30
# File 'lib/physical/box.rb', line 28

def inner_height
  @inner_height
end

#inner_lengthMeasured::Length (readonly)

The inner length of this box

Returns:

  • (Measured::Length)


20
21
22
# File 'lib/physical/box.rb', line 20

def inner_length
  @inner_length
end

#inner_widthMeasured::Length (readonly)

The inner width of this box

Returns:

  • (Measured::Length)


24
25
26
# File 'lib/physical/box.rb', line 24

def inner_width
  @inner_width
end

#max_weightMeasured::Weight (readonly)

The maximum weight this box can handle

Returns:

  • (Measured::Weight)


32
33
34
# File 'lib/physical/box.rb', line 32

def max_weight
  @max_weight
end

Instance Method Details

#inner_volumeMeasured::Volume

Calculates and returns this box's volume based on the inner dimensions

Returns:

  • (Measured::Volume)


52
53
54
55
56
57
# File 'lib/physical/box.rb', line 52

def inner_volume
  Measured::Volume(
    inner_dimensions.map { |d| d.convert_to(:cm).value }.reduce(1, &:*),
    :ml
  )
end

#item_fits?(item) ⇒ Boolean

Returns true if the given item can fit inside this box

Parameters:

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
# File 'lib/physical/box.rb', line 62

def item_fits?(item)
  return false if item.weight > max_weight

  box_dimensions = inner_dimensions.sort
  item.dimensions.sort.each.with_index do |axis, index|
    return false if axis >= box_dimensions[index]
  end
  true
end