Class: Physical::Package
- Inherits:
-
Object
- Object
- Physical::Package
- Extended by:
- Forwardable
- Defined in:
- lib/physical/package.rb
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#items_weight ⇒ Object
readonly
Returns the value of attribute items_weight.
-
#used_volume ⇒ Object
readonly
Returns the value of attribute used_volume.
-
#void_fill_density ⇒ Object
readonly
Returns the value of attribute void_fill_density.
Instance Method Summary collapse
- #<<(other) ⇒ Object (also: #add)
- #>>(other) ⇒ Object (also: #delete)
- #density ⇒ Object
-
#initialize(id: nil, container: nil, items: [], void_fill_density: Measured::Density(0, :g_ml), dimensions: nil, weight: nil, properties: {}) ⇒ Package
constructor
A new instance of Package.
-
#items_value ⇒ Object
Cost is optional.
- #remaining_volume ⇒ Object
- #void_fill_weight ⇒ Object
- #weight ⇒ Object
Constructor Details
#initialize(id: nil, container: nil, items: [], void_fill_density: Measured::Density(0, :g_ml), dimensions: nil, weight: nil, properties: {}) ⇒ Package
Returns a new instance of Package.
8 9 10 11 12 13 14 15 16 |
# File 'lib/physical/package.rb', line 8 def initialize(id: nil, container: nil, items: [], void_fill_density: Measured::Density(0, :g_ml), dimensions: nil, weight: 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) @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 ⇒ Object (readonly)
Returns the value of attribute container.
6 7 8 |
# File 'lib/physical/package.rb', line 6 def container @container end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/physical/package.rb', line 6 def id @id end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
6 7 8 |
# File 'lib/physical/package.rb', line 6 def items @items end |
#items_weight ⇒ Object (readonly)
Returns the value of attribute items_weight.
6 7 8 |
# File 'lib/physical/package.rb', line 6 def items_weight @items_weight end |
#used_volume ⇒ Object (readonly)
Returns the value of attribute used_volume.
6 7 8 |
# File 'lib/physical/package.rb', line 6 def used_volume @used_volume end |
#void_fill_density ⇒ Object (readonly)
Returns the value of attribute void_fill_density.
6 7 8 |
# File 'lib/physical/package.rb', line 6 def void_fill_density @void_fill_density end |
Instance Method Details
#<<(other) ⇒ Object Also known as: add
20 21 22 23 24 |
# File 'lib/physical/package.rb', line 20 def <<(other) @items.add(other) @items_weight += other.weight @used_volume += other.volume end |
#>>(other) ⇒ Object Also known as: delete
27 28 29 30 31 |
# File 'lib/physical/package.rb', line 27 def >>(other) @items.delete(other) @items_weight -= other.weight @used_volume -= other.volume end |
#density ⇒ Object
56 57 58 59 60 61 |
# File 'lib/physical/package.rb', line 56 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 ⇒ Object
Cost is optional. We will only return an aggregate if all items have cost defined. Otherwise we will return nil.
41 42 43 44 |
# File 'lib/physical/package.rb', line 41 def items_value items_cost = items.map(&:cost) items_cost.reduce(&:+) if items_cost.compact.size == items_cost.size end |
#remaining_volume ⇒ Object
52 53 54 |
# File 'lib/physical/package.rb', line 52 def remaining_volume container.inner_volume - used_volume end |
#void_fill_weight ⇒ Object
46 47 48 49 50 |
# File 'lib/physical/package.rb', line 46 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 |
#weight ⇒ Object
34 35 36 |
# File 'lib/physical/package.rb', line 34 def weight container.weight + items_weight + void_fill_weight end |