Class: CompositeUnitMeasurements::Length
- Inherits:
-
Object
- Object
- CompositeUnitMeasurements::Length
- Defined in:
- lib/composite_unit_measurements/length.rb
Overview
A parser handling length
measurements, particularly for composite units like foot-inch
, kilometre-metre
, mile-yard
, etc.
Constant Summary collapse
- FOOT_ALIASES =
Regex pattern for aliases of
foot
unit. /(?:'|ft|foot|feet)/.freeze
- INCH_ALIASES =
Regex pattern for aliases of
inch
unit. /(?:"|in|inch(?:es)?)/.freeze
- METRE_ALIASES =
Regex pattern for aliases of
metre
unit. /(?:m|meter(?:s)?|metre(?:s)?)/.freeze
- CENTIMETRE_ALIASES =
Regex pattern for aliases of
centimetre
unit. /(?:cm|centimeter(?:s)?|centimetre(?:s)?)/.freeze
- KILOMETRE_ALIASES =
Regex pattern for aliases of
kilometre
unit. /(?:km|kilometer(?:s)?|kilometre(?:s)?)/.freeze
- MILE_ALIASES =
Regex pattern for aliases of
mile
unit. /(?:mi|mile(?:s)?)/.freeze
- YARD_ALIASES =
Regex pattern for aliases of
yard
unit. /(?:yd|yard(?:s)?)/.freeze
- FOOT_INCH =
Regex pattern for parsing a length measurement in the format of
foot-inch
. /\A#{ANY_NUMBER}\s*#{FOOT_ALIASES}\s*#{ANY_NUMBER}\s*#{INCH_ALIASES}\z/.freeze
- METRE_CENTIMETRE =
Regex pattern for parsing a length measurement in the format of
metre-centimetre
. /\A#{ANY_NUMBER}\s*#{METRE_ALIASES}\s*#{ANY_NUMBER}\s*#{CENTIMETRE_ALIASES}\z/.freeze
- KILOMETRE_METRE =
Regex pattern for parsing a length measurement in the format of
kilometre-metre
. /\A#{ANY_NUMBER}\s*#{KILOMETRE_ALIASES}\s*#{ANY_NUMBER}\s*#{METRE_ALIASES}\z/.freeze
- MILE_YARD =
Regex pattern for parsing a length measurement in the format of
mile-yard
. /\A#{ANY_NUMBER}\s*#{MILE_ALIASES}\s*#{ANY_NUMBER}\s*#{YARD_ALIASES}\z/.freeze
Class Method Summary collapse
-
.parse(string) ⇒ UnitMeasurements::Length
Parses a given
string
into aUnitMeasurements::Length
object. -
.parse_foot_inch(string) ⇒ UnitMeasurements::Length
private
Parses a
string
representing a length in the format offoot-inch
into aUnitMeasurements::Length
object. -
.parse_kilometre_metre(string) ⇒ UnitMeasurements::Length
private
Parses a
string
representing a length in the format ofkilometre-metre
into aUnitMeasurements::Length
object. -
.parse_metre_centimetre(string) ⇒ UnitMeasurements::Length
private
Parses a
string
representing a length in the format ofmetre-centimetre
into aUnitMeasurements::Length
object. -
.parse_mile_yard(string) ⇒ UnitMeasurements::Length
private
Parses a
string
representing a length in the format ofmile-yard
into aUnitMeasurements::Length
object.
Class Method Details
.parse(string) ⇒ UnitMeasurements::Length
Parses a given string
into a UnitMeasurements::Length
object.
35 36 37 38 39 40 41 42 43 |
# File 'lib/composite_unit_measurements/length.rb', line 35 def parse(string) case string when FOOT_INCH then parse_foot_inch(string) when KILOMETRE_METRE then parse_kilometre_metre(string) when METRE_CENTIMETRE then parse_metre_centimetre(string) when MILE_YARD then parse_mile_yard(string) else raise UnitMeasurements::ParseError, string end end |
.parse_foot_inch(string) ⇒ UnitMeasurements::Length (private)
Parses a string
representing a length in the format of foot-inch
into a UnitMeasurements::Length
object.
59 60 61 62 63 64 65 |
# File 'lib/composite_unit_measurements/length.rb', line 59 def parse_foot_inch(string) foot, inch = string.match(FOOT_INCH)&.captures if foot && inch UnitMeasurements::Length.new(foot, "ft") + UnitMeasurements::Length.new(inch, "in") end end |
.parse_kilometre_metre(string) ⇒ UnitMeasurements::Length (private)
Parses a string
representing a length in the format of kilometre-metre
into a UnitMeasurements::Length
object.
99 100 101 102 103 104 105 |
# File 'lib/composite_unit_measurements/length.rb', line 99 def parse_kilometre_metre(string) kilometre, metre = string.match(KILOMETRE_METRE)&.captures if kilometre && metre UnitMeasurements::Length.new(kilometre, "km") + UnitMeasurements::Length.new(metre, "m") end end |
.parse_metre_centimetre(string) ⇒ UnitMeasurements::Length (private)
Parses a string
representing a length in the format of metre-centimetre
into a UnitMeasurements::Length
object.
79 80 81 82 83 84 85 |
# File 'lib/composite_unit_measurements/length.rb', line 79 def parse_metre_centimetre(string) metre, centimetre = string.match(METRE_CENTIMETRE)&.captures if metre && centimetre UnitMeasurements::Length.new(metre, "m") + UnitMeasurements::Length.new(centimetre, "cm") end end |
.parse_mile_yard(string) ⇒ UnitMeasurements::Length (private)
Parses a string
representing a length in the format of mile-yard
into a UnitMeasurements::Length
object.
119 120 121 122 123 124 125 |
# File 'lib/composite_unit_measurements/length.rb', line 119 def parse_mile_yard(string) mile, yard = string.match(MILE_YARD)&.captures if mile && yard UnitMeasurements::Length.new(mile, "mi") + UnitMeasurements::Length.new(yard, "yd") end end |