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
-
.convert_length(value, from:, to:) ⇒ BigDecimal?
Converts a length between any two supported units.
-
.convert_weight(value, from:, to:) ⇒ BigDecimal?
Converts a weight between any two supported units.
-
.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.
-
.to_centimeters(value, unit: DEFAULT_LENGTH_UNIT) ⇒ BigDecimal?
Converts a length to centimeters.
-
.to_kilograms(value, unit: DEFAULT_WEIGHT_UNIT) ⇒ BigDecimal?
Converts a weight to kilograms.
Class Method Details
.convert_length(value, from:, to:) ⇒ BigDecimal?
Converts a length between any two supported units.
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.
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.
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.
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.
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 |