lib/physical/cuboid.rb in physical-0.1.4 vs lib/physical/cuboid.rb in physical-0.2.0

- old
+ new

@@ -4,15 +4,15 @@ 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: {}) + def initialize(id: nil, dimensions: [], weight: Measured::Weight(0, :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) + @weight = Types::Weight[weight] + @dimensions = [] + @dimensions = fill_dimensions(Types::Dimensions[dimensions]) @length, @width, @height = *@dimensions.reverse @properties = properties end alias :x :length @@ -24,8 +24,16 @@ 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.sort end end end