class Physical::Cuboid

Constants

NORMALIZED_METHOD_REGEX

Attributes

dimensions[R]
height[R]
id[R]
length[R]
properties[R]
weight[R]
width[R]

Public Class Methods

new(id: nil, dimensions: [], weight: Measured::Weight(0, :g), properties: {}) click to toggle source
# File lib/physical/cuboid.rb, line 9
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

Public Instance Methods

==(other) click to toggle source
# File lib/physical/cuboid.rb, line 28
def ==(other)
  other.is_a?(self.class) &&
    id == other&.id
end
density() click to toggle source
# File lib/physical/cuboid.rb, line 22
def density
  return Measured::Density(Float::INFINITY, :g_ml) if volume.value.zero?
  return Measured::Density(0.0, :g_ml) if volume.value.infinite?
  Measured::Density(weight.convert_to(:g).value / volume.convert_to(:ml).value, :g_ml)
end
volume() click to toggle source
# File lib/physical/cuboid.rb, line 18
def volume
  Measured::Volume(dimensions.map { |d| d.convert_to(:cm).value }.reduce(1, &:*), :ml)
end

Private Instance Methods

fill_dimensions(dimensions) click to toggle source
# File lib/physical/cuboid.rb, line 56
def fill_dimensions(dimensions)
  dimensions.fill(dimensions.length..2) do |index|
    @dimensions[index] || Measured::Length(self.class::DEFAULT_LENGTH, :cm)
  end
end
method_missing(method) click to toggle source
Calls superclass method
# File lib/physical/cuboid.rb, line 37
def method_missing(method)
  symbolized_properties = properties.symbolize_keys
  method_name = normalize_method_name(method)
  if symbolized_properties.key?(method_name)
    symbolized_properties[method_name]
  else
    super
  end
end
normalize_method_name(method) click to toggle source
# File lib/physical/cuboid.rb, line 52
def normalize_method_name(method)
  method.to_s.sub(NORMALIZED_METHOD_REGEX, '\1').to_sym
end
respond_to_missing?(method, *args) click to toggle source
Calls superclass method
# File lib/physical/cuboid.rb, line 47
def respond_to_missing?(method, *args)
  method_name = normalize_method_name(method)
  properties.symbolize_keys.key?(method_name) || super
end