Class: Physical::Package
- Inherits:
-
Object
- Object
- Physical::Package
- 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
-
#container ⇒ Physical::Cuboid
readonly
The container (Box) for this package.
-
#description ⇒ String
readonly
The description for this package.
-
#dimensions ⇒ Object
readonly
The container's dimensions.
-
#height ⇒ Object
readonly
The container's height.
-
#id ⇒ String
readonly
A unique identifier for this package.
-
#items ⇒ Array<Physical::Item>
readonly
The items contained by this package.
-
#items_weight ⇒ Measured::Weight
readonly
The weight of the items in this package.
-
#length ⇒ Object
readonly
The container's length.
-
#properties ⇒ Object
readonly
The container's additional custom properties.
-
#used_volume ⇒ Measured::Volume
readonly
The total volume used by the items in this package.
-
#void_fill_density ⇒ Measured::Density
readonly
The density of the void fill in this package.
-
#volume ⇒ Object
readonly
The container's volume.
-
#weight ⇒ Measured::Weight
readonly
Sums container weight, items weight, and void fill weight and returns the total.
Instance Method Summary collapse
-
#<<(other) ⇒ Object
(also: #add)
Adds an item to the package.
-
#>>(other) ⇒ Object
(also: #delete)
Removes an item from the package.
-
#density ⇒ Measured::Density
Returns the density of this package based on its volume and weight.
-
#initialize(id: nil, container: nil, items: [], void_fill_density: Measured::Density(0, :g_ml), dimensions: nil, weight: nil, description: nil, properties: {}) ⇒ Package
constructor
A new instance of Package.
-
#items_value ⇒ Money?
Sums and returns the cost from all items in this package.
-
#remaining_volume ⇒ Measured::Volume
Calculates and returns remaining volume in this package.
-
#void_fill_weight ⇒ Measured::Weight
Calculates and returns the weight of the void fill in this package.
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.
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
#container ⇒ Physical::Cuboid (readonly)
The container (Box) for this package
14 15 16 |
# File 'lib/physical/package.rb', line 14 def container @container end |
#description ⇒ String (readonly)
The description for this package
34 35 36 |
# File 'lib/physical/package.rb', line 34 def description @description end |
#dimensions ⇒ Object (readonly)
The container's dimensions
67 |
# File 'lib/physical/package.rb', line 67 delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container |
#height ⇒ Object (readonly)
The container's height
67 |
# File 'lib/physical/package.rb', line 67 delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container |
#id ⇒ String (readonly)
A unique identifier for this package
10 11 12 |
# File 'lib/physical/package.rb', line 10 def id @id end |
#items ⇒ Array<Physical::Item> (readonly)
The items contained by this package
18 19 20 |
# File 'lib/physical/package.rb', line 18 def items @items end |
#items_weight ⇒ Measured::Weight (readonly)
The weight of the items in this package
26 27 28 |
# File 'lib/physical/package.rb', line 26 def items_weight @items_weight end |
#length ⇒ Object (readonly)
The container's length
67 |
# File 'lib/physical/package.rb', line 67 delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container |
#properties ⇒ Object (readonly)
The container's additional custom properties
67 |
# File 'lib/physical/package.rb', line 67 delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container |
#used_volume ⇒ Measured::Volume (readonly)
The total volume used by the items in this package
30 31 32 |
# File 'lib/physical/package.rb', line 30 def used_volume @used_volume end |
#void_fill_density ⇒ Measured::Density (readonly)
The density of the void fill in this package
22 23 24 |
# File 'lib/physical/package.rb', line 22 def void_fill_density @void_fill_density end |
#volume ⇒ Object (readonly)
The container's volume
67 |
# File 'lib/physical/package.rb', line 67 delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container |
#weight ⇒ Measured::Weight (readonly)
Sums container weight, items weight, and void fill weight and returns the total.
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.
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.
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 |
#density ⇒ Measured::Density
Returns the density of this package based on its volume and weight.
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_value ⇒ Money?
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.
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_volume ⇒ Measured::Volume
Calculates and returns remaining volume in this package.
112 113 114 |
# File 'lib/physical/package.rb', line 112 def remaining_volume container.inner_volume - used_volume end |
#void_fill_weight ⇒ Measured::Weight
Calculates and returns the weight of the void fill in this package.
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 |