Class: Physical::Cuboid

Inherits:
Object
  • Object
show all
Includes:
PropertyReaders
Defined in:
lib/physical/cuboid.rb

Overview

Represents a cube-shaped physical object with dimensions and weight.

Direct Known Subclasses

Box, Item, Pallet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, dimensions: [], weight: Measured::Weight(0, :g), properties: {}) ⇒ Cuboid

Returns a new instance of Cuboid.

Parameters:

  • id (String) (defaults to: nil)

    a unique identifier for this cuboid

  • dimensions (Array<Measured::Length>) (defaults to: [])

    the length, width, and height of this cuboid

  • weight (Measured::Weight) (defaults to: Measured::Weight(0, :g))

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

  • properties (Hash) (defaults to: {})

    additional custom properties for this cuboid



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

def initialize(id: nil, dimensions: [], weight: Measured::Weight(0, :g), properties: {})
  @id = id || SecureRandom.uuid
  @weight = Types::Weight[weight]
  @dimensions = []
  @dimensions = fill_dimensions(Types::Dimensions[dimensions])
  @length, @width, @height = *@dimensions
  @properties = properties
end

Instance Attribute Details

#dimensionsArray<Measured::Length> (readonly)

The length, width, and height of this cuboid

Returns:

  • (Array<Measured::Length>)


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

def dimensions
  @dimensions
end

#heightMeasured::Length (readonly)

The height of this cuboid

Returns:

  • (Measured::Length)


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

def height
  @height
end

#idString (readonly)

A unique identifier for this cuboid

Returns:

  • (String)


12
13
14
# File 'lib/physical/cuboid.rb', line 12

def id
  @id
end

#lengthMeasured::Length (readonly)

The length of this cuboid

Returns:

  • (Measured::Length)


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

def length
  @length
end

#propertiesHash (readonly)

Additional custom properties for this cuboid

Returns:

  • (Hash)


36
37
38
# File 'lib/physical/cuboid.rb', line 36

def properties
  @properties
end

#weightMeasured::Weight (readonly)

The weight of the cuboid itself (excluding what's inside)

Returns:

  • (Measured::Weight)


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

def weight
  @weight
end

#widthMeasured::Length (readonly)

The width of this cuboid

Returns:

  • (Measured::Length)


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

def width
  @width
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if the given object shares the same class and ID with this cuboid.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


69
70
71
72
# File 'lib/physical/cuboid.rb', line 69

def ==(other)
  other.is_a?(self.class) &&
    id == other&.id
end

#densityMeasured::Density

Calculates and returns this cuboid's density based on its volume and weight.

Returns:

  • (Measured::Density)


59
60
61
62
63
64
# File 'lib/physical/cuboid.rb', line 59

def density
  return Measured::Density(Float::INFINITY, :g_ml) if volume.value.zero?
  return Measured::Density(0.0, :g_ml) if volume.value.infinite?

  Measured::Density(weight.convert_to(:g).value / volume.convert_to(:ml).value, :g_ml)
end

#volumeMeasured::Volume

Calculates and returns this cuboid's volume based on its dimensions.

Returns:

  • (Measured::Volume)


53
54
55
# File 'lib/physical/cuboid.rb', line 53

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