Class: SpaceCalculator::Dimensions

Inherits:
Object
  • Object
show all
Defined in:
backend/app/model/space_calculator.rb

Constant Summary collapse

WORKING_UNIT =
:millimeters
UNIT_CONVERSIONS =
{
  :millimeters => {
    :centimeters => 0.1,
    :meters => 0.001,
    :inches => 0.0393701,
    :feet => 0.00328084,
    :yards => 0.00328084/3.0,
  },
  :centimeters => {
    :millimeters => 10.0,
    :meters => 0.01,
    :inches => 0.393701,
    :feet => 0.0328084,
    :yards => 0.0328084/3.0,
  },
  :meters => {
    :millimeters => 1000.0,
    :centimeters => 100.0,
    :inches => 39.3701,
    :feet => 3.28084,
    :yards => 3.28084/3.0,
  },
  :inches => {
    :millimeters => 25.4,
    :centimeters => 2.54,
    :meters => 0.0254,
    :feet => 1.0/12.0,
    :yards => 1.0/36.0,
  },
  :feet => {
    :millimeters => 25.4*12.0,
    :centimeters => 2.54*12.0,
    :meters => 0.3048,
    :inches => 12.0,
    :yards => 1.0/3.0,
  },
  :yards => {
    :millimeters => 25.4*36.0,
    :centimeters => 2.54*36.0,
    :meters => 0.3048*3.0,
    :inches => 36.0,
    :feet => 3.0,
  },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, depth, unit = nil) ⇒ Dimensions

Returns a new instance of Dimensions.



400
401
402
403
404
405
406
407
408
409
410
411
# File 'backend/app/model/space_calculator.rb', line 400

def initialize(width, height, depth, unit = nil)
  @provided_unit = (unit ? unit.intern : :inches)
  unless UNIT_CONVERSIONS.has_key?(@provided_unit)
    raise UnsupportedUnitException.new("Provided unit not supported: #{@provided_unit}")
  end
  if width.nil? || height.nil? || depth.nil?
    raise MissingDimensionException.new("Values must be provided for width (#{width}), height (#{height}) and depth (#{depth})")
  end
  @width = convert(width.to_f)
  @height = convert(height.to_f)
  @depth = convert(depth.to_f)
end

Instance Attribute Details

#depthObject (readonly)

Returns the value of attribute depth



350
351
352
# File 'backend/app/model/space_calculator.rb', line 350

def depth
  @depth
end

#heightObject (readonly)

Returns the value of attribute height



350
351
352
# File 'backend/app/model/space_calculator.rb', line 350

def height
  @height
end

#provided_unitObject (readonly)

Returns the value of attribute provided_unit



350
351
352
# File 'backend/app/model/space_calculator.rb', line 350

def provided_unit
  @provided_unit
end

#widthObject (readonly)

Returns the value of attribute width



350
351
352
# File 'backend/app/model/space_calculator.rb', line 350

def width
  @width
end

Instance Method Details

#bigger_in_any_than?(other_dim) ⇒ Boolean

Returns:

  • (Boolean)


421
422
423
# File 'backend/app/model/space_calculator.rb', line 421

def bigger_in_any_than?(other_dim)
  @width > other_dim.width || @height > other_dim.height || @depth > other_dim.depth
end

#convert(val) ⇒ Object



414
415
416
417
418
# File 'backend/app/model/space_calculator.rb', line 414

def convert(val)
  return val if @provided_unit == WORKING_UNIT
  conv = UNIT_CONVERSIONS.fetch(@provided_unit, {}).fetch(WORKING_UNIT)
  val * conv
end

#has_missing_dimension?Boolean

Returns:

  • (Boolean)


426
427
428
429
# File 'backend/app/model/space_calculator.rb', line 426

def has_missing_dimension?
  # if initialized with nil the .to_f will coerce it to 0.0
  @width == 0.0 || @height == 0.0 || @depth == 0.0
end