Class: Physical::Box
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
-
#inner_dimensions ⇒ Array<Measured::Length>
readonly
The inner length, width, and height of this box.
-
#inner_height ⇒ Measured::Length
readonly
The inner height of this box.
-
#inner_length ⇒ Measured::Length
readonly
The inner length of this box.
-
#inner_width ⇒ Measured::Length
readonly
The inner width of this box.
-
#max_weight ⇒ Measured::Weight
readonly
The maximum weight this box can handle.
Attributes inherited from Cuboid
#dimensions, #height, #id, #length, #properties, #weight, #width
Instance Method Summary collapse
-
#initialize(**kwargs) ⇒ Box
constructor
A new instance of Box.
-
#inner_volume ⇒ Measured::Volume
Calculates and returns this box's volume based on the inner dimensions.
-
#item_fits?(item) ⇒ Boolean
Returns true if the given item can fit inside this box.
Methods inherited from Cuboid
Constructor Details
#initialize(**kwargs) ⇒ Box
Returns a new instance of 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_dimensions ⇒ Array<Measured::Length> (readonly)
The inner length, width, and height of this box
16 17 18 |
# File 'lib/physical/box.rb', line 16 def inner_dimensions @inner_dimensions end |
#inner_height ⇒ Measured::Length (readonly)
The inner height of this box
28 29 30 |
# File 'lib/physical/box.rb', line 28 def inner_height @inner_height end |
#inner_length ⇒ Measured::Length (readonly)
The inner length of this box
20 21 22 |
# File 'lib/physical/box.rb', line 20 def inner_length @inner_length end |
#inner_width ⇒ Measured::Length (readonly)
The inner width of this box
24 25 26 |
# File 'lib/physical/box.rb', line 24 def inner_width @inner_width end |
#max_weight ⇒ Measured::Weight (readonly)
The maximum weight this box can handle
32 33 34 |
# File 'lib/physical/box.rb', line 32 def max_weight @max_weight end |
Instance Method Details
#inner_volume ⇒ Measured::Volume
Calculates and returns this box's volume based on the inner dimensions
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
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 |