Class: Physical::Package

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/physical/package.rb

Overview

Represents a physical package which has a container (box) and items.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, container: nil, items: [], void_fill_density: Measured::Density(0, :g_ml), dimensions: nil, weight: nil, description: nil, properties: {}) ⇒ Package

Returns a new instance of Package.

Parameters:

  • id (String) (defaults to: nil)

    a unique identifier for this package

  • container (Physical::Cuboid) (defaults to: nil)

    the container (Box) for this package

  • items (Array<Physical::Item>) (defaults to: [])

    the items contained by this package

  • void_fill_density (Measured::Density) (defaults to: Measured::Density(0, :g_ml))

    the density of the void fill in this package

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

    the length, width, and height of this package's container

  • weight (Measured::Weight) (defaults to: nil)

    the weight of this package's container

  • description (String) (defaults to: nil)

    a description for this package

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

    additional custom properties for this package's container



44
45
46
47
48
49
50
51
52
53
# File 'lib/physical/package.rb', line 44

def initialize(id: nil, container: nil, items: [], void_fill_density: Measured::Density(0, :g_ml), dimensions: nil, weight: nil, description: nil, properties: {})
  @id = id || SecureRandom.uuid
  @void_fill_density = Types::Density[void_fill_density]
  @container = container || Physical::Box.new(dimensions: dimensions || [], weight: weight || Measured::Weight(0, :g), properties: properties)
  @description = description

  @items = Set[*items]
  @items_weight = @items.map(&:weight).reduce(Measured::Weight(0, :g), &:+)
  @used_volume = @items.map(&:volume).reduce(Measured::Volume(0, :ml), &:+)
end

Instance Attribute Details

#containerPhysical::Cuboid (readonly)

The container (Box) for this package

Returns:



14
15
16
# File 'lib/physical/package.rb', line 14

def container
  @container
end

#descriptionString (readonly)

The description for this package

Returns:

  • (String)


34
35
36
# File 'lib/physical/package.rb', line 34

def description
  @description
end

#dimensionsObject (readonly)

The container's dimensions



67
# File 'lib/physical/package.rb', line 67

delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container

#heightObject (readonly)

The container's height



67
# File 'lib/physical/package.rb', line 67

delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container

#idString (readonly)

A unique identifier for this package

Returns:

  • (String)


10
11
12
# File 'lib/physical/package.rb', line 10

def id
  @id
end

#itemsArray<Physical::Item> (readonly)

The items contained by this package

Returns:



18
19
20
# File 'lib/physical/package.rb', line 18

def items
  @items
end

#items_weightMeasured::Weight (readonly)

The weight of the items in this package

Returns:

  • (Measured::Weight)


26
27
28
# File 'lib/physical/package.rb', line 26

def items_weight
  @items_weight
end

#lengthObject (readonly)

The container's length



67
# File 'lib/physical/package.rb', line 67

delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container

#propertiesObject (readonly)

The container's additional custom properties



67
# File 'lib/physical/package.rb', line 67

delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container

#used_volumeMeasured::Volume (readonly)

The total volume used by the items in this package

Returns:

  • (Measured::Volume)


30
31
32
# File 'lib/physical/package.rb', line 30

def used_volume
  @used_volume
end

#void_fill_densityMeasured::Density (readonly)

The density of the void fill in this package

Returns:

  • (Measured::Density)


22
23
24
# File 'lib/physical/package.rb', line 22

def void_fill_density
  @void_fill_density
end

#volumeObject (readonly)

The container's volume



67
# File 'lib/physical/package.rb', line 67

delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container

#weightMeasured::Weight (readonly)

Sums container weight, items weight, and void fill weight and returns the total.

Returns:

  • (Measured::Weight)


67
# File 'lib/physical/package.rb', line 67

delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container

Instance Method Details

#<<(other) ⇒ Object Also known as: add

Adds an item to the package.

Parameters:



71
72
73
74
75
# File 'lib/physical/package.rb', line 71

def <<(other)
  @items.add(other)
  @items_weight += other.weight
  @used_volume += other.volume
end

#>>(other) ⇒ Object Also known as: delete

Removes an item from the package.

Parameters:



80
81
82
83
84
# File 'lib/physical/package.rb', line 80

def >>(other)
  @items.delete(other)
  @items_weight -= other.weight
  @used_volume -= other.volume
end

#densityMeasured::Density

Returns the density of this package based on its volume and weight.

Returns:

  • (Measured::Density)


118
119
120
121
122
123
# File 'lib/physical/package.rb', line 118

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

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

#items_valueMoney?

Sums and returns the cost from all items in this package. Item cost is optional, therefore we only return a sum if all items have a cost. Otherwise, nil is returned.

Returns:

  • (Money, nil)


97
98
99
100
# File 'lib/physical/package.rb', line 97

def items_value
  items_cost = items.map(&:cost)
  items_cost.reduce(&:+) if items_cost.compact.size == items_cost.size
end

#remaining_volumeMeasured::Volume

Calculates and returns remaining volume in this package.

Returns:

  • (Measured::Volume)


112
113
114
# File 'lib/physical/package.rb', line 112

def remaining_volume
  container.inner_volume - used_volume
end

#void_fill_weightMeasured::Weight

Calculates and returns the weight of the void fill in this package.

Returns:

  • (Measured::Weight)


104
105
106
107
108
# File 'lib/physical/package.rb', line 104

def void_fill_weight
  return Measured::Weight(0, :g) if container.volume.value.infinite?

  Measured::Weight(void_fill_density.convert_to(:g_ml).value * remaining_volume.convert_to(:ml).value, :g)
end