Class: CompositeUnitMeasurements::Weight

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_unit_measurements/weight.rb

Overview

A parser handling weight measurements, particularly for composite units like kilogramme-gramme, pound-ounce, stone-pound etc.

Author:

Since:

  • 0.2.0

Constant Summary collapse

POUND_ALIASES =

Regex pattern for aliases of pound unit.

Author:

Since:

  • 0.2.0

/(?:#|lb|lbs|lbm|pound-mass|pound(?:s)?)/.freeze
OUNCE_ALIASES =

Regex pattern for aliases of ounce unit.

Author:

Since:

  • 0.2.0

/(?:oz|ounce(?:s)?)/.freeze
STONE_ALIASES =

Regex pattern for aliases of stone unit.

Author:

Since:

  • 0.2.0

/(?:st|stone(?:s)?)/.freeze
GRAMME_ALIASES =

Regex pattern for aliases of gramme unit.

Author:

Since:

  • 0.3.0

/(?:g|gram(?:s)?|gramme(?:s)?)/.freeze
KILOGRAMME_ALIASES =

Regex pattern for aliases of kilogramme unit.

Author:

Since:

  • 0.3.0

/(?:kg|kilogram(?:s)?|kilogramme(?:s)?)/.freeze
POUND_OUNCE =

Regex pattern for parsing a weight measurement in the format of pound-ounce.

Author:

Since:

  • 0.2.0

/\A#{ANY_NUMBER}\s*#{POUND_ALIASES}\s*#{ANY_NUMBER}\s*#{OUNCE_ALIASES}\z/.freeze
STONE_POUND =

Regex pattern for parsing a weight measurement in the format of stone-pound.

Author:

Since:

  • 0.2.0

/\A#{ANY_NUMBER}\s*#{STONE_ALIASES}\s*#{ANY_NUMBER}\s*#{POUND_ALIASES}\z/.freeze
KILOGRAMME_GRAMME =

Regex pattern for parsing a weight measurement in the format of kilogramme-gramme.

Author:

Since:

  • 0.3.0

/\A#{ANY_NUMBER}\s*#{KILOGRAMME_ALIASES}\s*#{ANY_NUMBER}\s*#{GRAMME_ALIASES}\z/.freeze

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ UnitMeasurements::Weight

Parses a given string into a UnitMeasurements::Weight object.

Examples:

Parse ‘kilogramme-gramme’ measurement:

CompositeUnitMeasurements::Weight.parse("4 kg 500 g") #=> 4.5 kg

Parse ‘pound-ounce’ measurement:

CompositeUnitMeasurements::Weight.parse("8 lb 12 oz") #=> 8.75 lb

Parse ‘stone-pound’ measurement:

CompositeUnitMeasurements::Weight.parse("2 st 6 lb") #=> 2.428571428571429 st

Parameters:

  • string (String)

    The string to parse for weight measurement.

Returns:

  • (UnitMeasurements::Weight)

    Returns a UnitMeasurements::Weight object if parsing is successful.

Raises:

  • (UnitMeasurements::ParseError)

    If the string does not match any known format.

Author:

Since:

  • 0.2.0



32
33
34
35
36
37
38
39
# File 'lib/composite_unit_measurements/weight.rb', line 32

def parse(string)
  case string
  when POUND_OUNCE       then parse_pound_ounce(string)
  when STONE_POUND       then parse_stone_pound(string)
  when KILOGRAMME_GRAMME then parse_kilogramme_gramme(string)
  else                        raise UnitMeasurements::ParseError, string
  end
end

.parse_kilogramme_gramme(string) ⇒ UnitMeasurements::Weight (private)

Parses a string representing a weight in the format of kilogramme-gramme into a UnitMeasurements::Weight object.

Parameters:

  • string (String)

    The string representing weight measurement in the format of kilogramme-gramme.

Returns:

  • (UnitMeasurements::Weight)

    Returns a UnitMeasurements::Weight object if parsing is successful.

See Also:

Author:

Since:

  • 0.3.0



95
96
97
98
99
100
101
# File 'lib/composite_unit_measurements/weight.rb', line 95

def parse_kilogramme_gramme(string)
  kilogramme, gramme = string.match(KILOGRAMME_GRAMME)&.captures

  if kilogramme && gramme
    UnitMeasurements::Weight.new(kilogramme, "kg") + UnitMeasurements::Weight.new(gramme, "g")
  end
end

.parse_pound_ounce(string) ⇒ UnitMeasurements::Weight (private)

Parses a string representing a weight in the format of pound-ounce into a UnitMeasurements::Weight object.

Parameters:

  • string (String)

    The string representing weight measurement in the format of pound-ounce.

Returns:

  • (UnitMeasurements::Weight)

    Returns a UnitMeasurements::Weight object if parsing is successful.

See Also:

Author:

Since:

  • 0.2.0



55
56
57
58
59
60
61
# File 'lib/composite_unit_measurements/weight.rb', line 55

def parse_pound_ounce(string)
  pound, ounce = string.match(POUND_OUNCE)&.captures

  if pound && ounce
    UnitMeasurements::Weight.new(pound, "lb") + UnitMeasurements::Weight.new(ounce, "oz")
  end
end

.parse_stone_pound(string) ⇒ UnitMeasurements::Weight (private)

Parses a string representing a weight in the format of stone-pound into a UnitMeasurements::Weight object.

Parameters:

  • string (String)

    The string representing weight measurement in the format of stone-pound.

Returns:

  • (UnitMeasurements::Weight)

    Returns a UnitMeasurements::Weight object if parsing is successful.

See Also:

Author:

Since:

  • 0.2.0



75
76
77
78
79
80
81
# File 'lib/composite_unit_measurements/weight.rb', line 75

def parse_stone_pound(string)
  stone, pound = string.match(STONE_POUND)&.captures

  if stone && pound
    UnitMeasurements::Weight.new(stone, "st") + UnitMeasurements::Weight.new(pound, "lb")
  end
end