Class: CompositeUnitMeasurements::Weight
- Inherits:
-
Object
- Object
- CompositeUnitMeasurements::Weight
- 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.
Constant Summary collapse
- POUND_ALIASES =
Regex pattern for aliases of
pound
unit. /(?:#|lb|lbs|lbm|pound-mass|pound(?:s)?)/.freeze
- OUNCE_ALIASES =
Regex pattern for aliases of
ounce
unit. /(?:oz|ounce(?:s)?)/.freeze
- STONE_ALIASES =
Regex pattern for aliases of
stone
unit. /(?:st|stone(?:s)?)/.freeze
- GRAMME_ALIASES =
Regex pattern for aliases of
gramme
unit. /(?:g|gram(?:s)?|gramme(?:s)?)/.freeze
- KILOGRAMME_ALIASES =
Regex pattern for aliases of
kilogramme
unit. /(?:kg|kilogram(?:s)?|kilogramme(?:s)?)/.freeze
- POUND_OUNCE =
Regex pattern for parsing a weight measurement in the format of
pound-ounce
. /\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
. /\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
. /\A#{ANY_NUMBER}\s*#{KILOGRAMME_ALIASES}\s*#{ANY_NUMBER}\s*#{GRAMME_ALIASES}\z/.freeze
Class Method Summary collapse
-
.parse(string) ⇒ UnitMeasurements::Weight
Parses a given
string
into aUnitMeasurements::Weight
object. -
.parse_kilogramme_gramme(string) ⇒ UnitMeasurements::Weight
private
Parses a
string
representing a weight in the format ofkilogramme-gramme
into aUnitMeasurements::Weight
object. -
.parse_pound_ounce(string) ⇒ UnitMeasurements::Weight
private
Parses a
string
representing a weight in the format ofpound-ounce
into aUnitMeasurements::Weight
object. -
.parse_stone_pound(string) ⇒ UnitMeasurements::Weight
private
Parses a
string
representing a weight in the format ofstone-pound
into aUnitMeasurements::Weight
object.
Class Method Details
.parse(string) ⇒ UnitMeasurements::Weight
Parses a given string
into a UnitMeasurements::Weight
object.
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.
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.
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.
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 |