Sha256: 2524614c65771bc7041970402a70c2588499dfafa439a75b267eb46a5815934c

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Physical
  class Package
    extend Forwardable
    attr_reader :container, :items, :void_fill_density, :id

    def initialize(id: nil, container: Physical::Box.new, items: [], void_fill_density: 0, void_fill_density_unit: :g)
      @id = id || SecureRandom.uuid
      @void_fill_density = measured_void_fill_density(void_fill_density, void_fill_density_unit)
      @container = container
      @items = Set[*items]
    end

    delegate [:dimensions, :width, :length, :height, :depth, :x, :y, :z] => :container

    def <<(item)
      @items.add(item)
    end

    def >>(item)
      @items.delete(item)
    end

    def weight
      container.weight + items.map(&:weight).reduce(Measured::Weight(0, :g), &:+) + void_fill_weight
    end

    def remaining_volume
      container.volume - items.map(&:volume).reduce(Measured::Volume(0, :ml), &:+)
    end

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

      Measured::Weight(void_fill_density.value * remaining_volume.value, void_fill_density.unit)
    end

    def measured_void_fill_density(void_fill_density, void_fill_density_unit)
      case void_fill_density
      when Measured::Weight
        void_fill_density
      else
        Measured::Weight(void_fill_density, void_fill_density_unit)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
physical-0.1.4 lib/physical/package.rb