Sha256: 778383e12be92b13e902e93a2978e9f1bd052aad85300c7bc50e0e96846cb48d

Contents?: true

Size: 929 Bytes

Versions: 2

Compression:

Stored size: 929 Bytes

Contents

# frozen_string_literal: true

require 'measured'

module Physical
  class Cuboid
    attr_reader :dimensions, :length, :width, :height, :weight, :id, :properties

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

    alias :x :length
    alias :y :width
    alias :z :height
    alias :depth :height

    def volume
      Measured::Volume(dimensions.map { |d| d.convert_to(:cm).value }.reduce(1, &:*), :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.4 lib/physical/cuboid.rb
physical-0.1.3 lib/physical/cuboid.rb