Module: Spree::Measurement

Defined in:
lib/spree/measurement.rb

Overview

Unit conversion for the physical measurements Spree stores: dimensions in millimeters, centimeters, inches or feet, and weights in grams, kilograms, pounds or ounces.

Spree keeps measurements in whatever unit the merchant typed, next to the unit itself, so any calculation combining two records has to normalize first. Volume is where this stops being cosmetic — a carton measured in inches has roughly sixteen times the cubic value of the same numbers read as centimeters, so cubic meters derived from raw dimension columns are wrong for most of the world.

Stateless module functions over two unit tables, deliberately not a value object: callers already hold a number and a unit string.

Constant Summary collapse

LENGTH_IN_CENTIMETERS =

Centimeters per unit of length.

{
  'mm' => BigDecimal('0.1'),
  'cm' => BigDecimal('1'),
  'in' => BigDecimal('2.54'),
  'ft' => BigDecimal('30.48')
}.freeze
WEIGHT_IN_KILOGRAMS =

Kilograms per unit of weight.

{
  'g' => BigDecimal('0.001'),
  'kg' => BigDecimal('1'),
  'lb' => BigDecimal('0.45359237'),
  'oz' => BigDecimal('0.028349523125')
}.freeze
CENTIMETERS_PER_METER =
BigDecimal('100')
DEFAULT_LENGTH_UNIT =
'cm'.freeze
DEFAULT_WEIGHT_UNIT =
'kg'.freeze

Class Method Summary collapse

Class Method Details

.convert_length(value, from:, to:) ⇒ BigDecimal?

Converts a length between any two supported units.

Parameters:

  • value (Numeric, String, nil)
  • from (String, nil)
  • to (String, nil)

Returns:

  • (BigDecimal, nil) —

    nil when the value is missing



64
65
66
# File 'lib/spree/measurement.rb', line 64

def convert_length(value, from:, to:)
  rescale(to_centimeters(value, unit: from), LENGTH_IN_CENTIMETERS, to, DEFAULT_LENGTH_UNIT)
end

.convert_weight(value, from:, to:) ⇒ BigDecimal?

Converts a weight between any two supported units.

Parameters:

  • value (Numeric, String, nil)
  • from (String, nil)
  • to (String, nil)

Returns:

  • (BigDecimal, nil) —

    nil when the value is missing



74
75
76
# File 'lib/spree/measurement.rb', line 74

def convert_weight(value, from:, to:)
  rescale(to_kilograms(value, unit: from), WEIGHT_IN_KILOGRAMS, to, DEFAULT_WEIGHT_UNIT)
end

.cubic_meters(length, width, height, unit: DEFAULT_LENGTH_UNIT) ⇒ BigDecimal?

The cubic meters a box of these dimensions occupies — the CBM figure freight is quoted and tiered on.

Parameters:

  • length (Numeric, nil)
  • width (Numeric, nil)
  • height (Numeric, nil)
  • unit (String, nil) (defaults to: DEFAULT_LENGTH_UNIT) —

    the unit all three are expressed in

Returns:

  • (BigDecimal, nil) —

    nil unless all three dimensions are present and positive — a box with a missing or zero side has no volume worth reporting, and returning zero would read as "measured, and empty"



88
89
90
91
92
93
# File 'lib/spree/measurement.rb', line 88

def cubic_meters(length, width, height, unit: DEFAULT_LENGTH_UNIT)
  sides = [length, width, height].map { |side| to_centimeters(side, unit: unit) }
  return if sides.any? { |side| side.nil? || side <= 0 }

  sides.inject(:*) / (CENTIMETERS_PER_METER**3)
end

.to_centimeters(value, unit: DEFAULT_LENGTH_UNIT) ⇒ BigDecimal?

Converts a length to centimeters.

Parameters:

  • value (Numeric, String, nil)
  • unit (String, nil) (defaults to: DEFAULT_LENGTH_UNIT) —

    one of LENGTH_IN_CENTIMETERS's keys; an unknown or blank unit is read as centimeters

Returns:

  • (BigDecimal, nil) —

    nil when the value is missing



44
45
46
# File 'lib/spree/measurement.rb', line 44

def to_centimeters(value, unit: DEFAULT_LENGTH_UNIT)
  convert(value, LENGTH_IN_CENTIMETERS, unit, DEFAULT_LENGTH_UNIT)
end

.to_kilograms(value, unit: DEFAULT_WEIGHT_UNIT) ⇒ BigDecimal?

Converts a weight to kilograms.

Parameters:

  • value (Numeric, String, nil)
  • unit (String, nil) (defaults to: DEFAULT_WEIGHT_UNIT) —

    one of WEIGHT_IN_KILOGRAMS's keys; an unknown or blank unit is read as kilograms

Returns:

  • (BigDecimal, nil) —

    nil when the value is missing



54
55
56
# File 'lib/spree/measurement.rb', line 54

def to_kilograms(value, unit: DEFAULT_WEIGHT_UNIT)
  convert(value, WEIGHT_IN_KILOGRAMS, unit, DEFAULT_WEIGHT_UNIT)
end