lib/physical/package.rb in physical-0.4.8 vs lib/physical/package.rb in physical-0.4.9
- old
+ new
@@ -1,56 +1,53 @@
# frozen_string_literal: true
module Physical
class Package
extend Forwardable
- attr_reader :container, :items, :void_fill_density, :id
+ attr_reader :id, :container, :items, :void_fill_density, :items_weight, :used_volume
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
delegate [:dimensions, :width, :length, :height, :properties, :volume] => :container
def <<(other)
@items.add(other)
+ @items_weight += other.weight
+ @used_volume += other.volume
end
alias_method :add, :<<
def >>(other)
@items.delete(other)
+ @items_weight -= other.weight
+ @used_volume -= other.volume
end
alias_method :delete, :>>
def weight
container.weight + items_weight + void_fill_weight
end
# Cost is optional. We will only return an aggregate if all items
- # have cost defined. Otherwise we will retun nil.
+ # have cost defined. Otherwise we will return nil.
# @return Money
def items_value
items_cost = items.map(&:cost)
items_cost.reduce(&:+) if items_cost.compact.size == items_cost.size
end
- # @return [Measured::Weight]
- def items_weight
- items.map(&:weight).reduce(Measured::Weight(0, :g), &:+)
- end
-
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
-
- # @return [Measured::Volume]
- def used_volume
- items.map(&:volume).reduce(Measured::Volume(0, :ml), &:+)
end
def remaining_volume
container.inner_volume - used_volume
end