Sha256: f485b8c4a5f4c9004c99ae8d0f951a591f872c5219ab8902a061da7daf9ff805

Contents?: true

Size: 882 Bytes

Versions: 2

Compression:

Stored size: 882 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: [], weight: Measured::Weight(0, :g), properties: {})
      @id = id || SecureRandom.uuid
      @weight = Types::Weight[weight]
      @dimensions = []
      @dimensions = fill_dimensions(Types::Dimensions[dimensions])
      @length, @width, @height = *@dimensions
      @properties = properties
    end

    def volume
      Measured::Volume(dimensions.map { |d| d.convert_to(:cm).value }.reduce(1, &:*), :ml)
    end

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

    private

    def fill_dimensions(dimensions)
      dimensions.fill(dimensions.length..2) do |index|
        @dimensions[index] || Measured::Length(self.class::DEFAULT_LENGTH, :cm)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
physical-0.3.1 lib/physical/cuboid.rb
physical-0.3.0 lib/physical/cuboid.rb