Sha256: a9370cecb0ccdbb7f9e37c1d07990052ffaddfa72070e37a915f0c8a44858ede

Contents?: true

Size: 701 Bytes

Versions: 2

Compression:

Stored size: 701 Bytes

Contents

# frozen_string_literal: true

require 'measured'

module Physical
  class Cuboid
    attr_reader :dimensions, :weight, :id

    def initialize(id: nil, dimensions: [], dimension_unit: :cm, weight: 0, weight_unit: :g)
      @id = id || SecureRandom.uuid
      @weight = Measured::Weight(weight, weight_unit).convert_to(:g)
      @dimensions = dimensions.map { |dimension| Measured::Length.new(dimension, dimension_unit).convert_to(:cm) }
      @dimensions.fill(Measured::Length(self.class::DEFAULT_LENGTH, dimension_unit), @dimensions.length..2)
    end

    def volume
      Measured::Volume(dimensions.map(&:value).reduce(&:*), :ml)
    end

    def ==(other)
      id == other.id
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
physical-0.1.1 lib/physical/cuboid.rb
physical-0.1.0 lib/physical/cuboid.rb