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
poundunit. /(?:#|lb|lbs|lbm|pound-mass|pound(?:s)?)/.freeze
- OUNCE_ALIASES =
Regex pattern for aliases of
ounceunit. /(?:oz|ounce(?:s)?)/.freeze
- STONE_ALIASES =
Regex pattern for aliases of
stoneunit. /(?:st|stone(?:s)?)/.freeze
- GRAMME_ALIASES =
Regex pattern for aliases of
grammeunit. /(?:g|gram(?:s)?|gramme(?:s)?)/.freeze
- KILOGRAMME_ALIASES =
Regex pattern for aliases of
kilogrammeunit. /(?:kg|kilogram(?:s)?|kilogramme(?:s)?)/.freeze
- TONNE_ALIASES =
Regex pattern for aliases of
tonneor metric tonne unit. /(?:t|tonne(?:s)?|metric tonne(?: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
- TONNE_KILOGRAMME =
Regex pattern for parsing a weight measurement in the format of
tonne-kilogramme. /\A#{ANY_NUMBER}\s*#{TONNE_ALIASES}\s*#{ANY_NUMBER}\s*#{KILOGRAMME_ALIASES}\z/.freeze
Class Method Summary collapse
-
.parse(string) ⇒ UnitMeasurements::Weight
Parses a given
stringinto aUnitMeasurements::Weightobject. -
.parse_kilogramme_gramme(string) ⇒ UnitMeasurements::Weight
private
Parses a
stringrepresenting a weight in the format ofkilogramme-grammeinto aUnitMeasurements::Weightobject. -
.parse_pound_ounce(string) ⇒ UnitMeasurements::Weight
private
Parses a
stringrepresenting a weight in the format ofpound-ounceinto aUnitMeasurements::Weightobject. -
.parse_stone_pound(string) ⇒ UnitMeasurements::Weight
private
Parses a
stringrepresenting a weight in the format ofstone-poundinto aUnitMeasurements::Weightobject. -
.parse_tonne_kilogramme(string) ⇒ UnitMeasurements::Weight
private
Parses a
stringrepresenting a weight in the format oftonne-kilogrammeinto aUnitMeasurements::Weightobject.
Class Method Details
.parse(string) ⇒ UnitMeasurements::Weight
Parses a given string into a UnitMeasurements::Weight object.
34 35 36 37 38 39 40 41 42 |
# File 'lib/composite_unit_measurements/weight.rb', line 34 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) when TONNE_KILOGRAMME then parse_tonne_kilogramme(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.
98 99 100 101 102 103 104 |
# File 'lib/composite_unit_measurements/weight.rb', line 98 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.
58 59 60 61 62 63 64 |
# File 'lib/composite_unit_measurements/weight.rb', line 58 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.
78 79 80 81 82 83 84 |
# File 'lib/composite_unit_measurements/weight.rb', line 78 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 |
.parse_tonne_kilogramme(string) ⇒ UnitMeasurements::Weight (private)
Parses a string representing a weight in the format of tonne-kilogramme into a UnitMeasurements::Weight object.
118 119 120 121 122 123 124 |
# File 'lib/composite_unit_measurements/weight.rb', line 118 def parse_tonne_kilogramme(string) tonne, kilogramme = string.match(TONNE_KILOGRAMME)&.captures if tonne && kilogramme UnitMeasurements::Weight.new(tonne, "t") + UnitMeasurements::Weight.new(kilogramme, "kg") end end |