Class: CompositeUnitMeasurements::Length

Inherits:
Object
  • Object
show all
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.

Author:

Since:

  • 0.2.0

Constant Summary collapse

FOOT_ALIASES =

Regex pattern for aliases of foot unit.

Author:

Since:

  • 0.2.0

/(?:'|ft|foot|feet)/.freeze
INCH_ALIASES =

Regex pattern for aliases of inch unit.

Author:

Since:

  • 0.2.0

/(?:"|in|inch(?:es)?)/.freeze
METRE_ALIASES =

Regex pattern for aliases of metre unit.

Author:

Since:

  • 0.3.0

/(?:m|meter(?:s)?|metre(?:s)?)/.freeze
CENTIMETRE_ALIASES =

Regex pattern for aliases of centimetre unit.

Author:

Since:

  • 0.3.0

/(?:cm|centimeter(?:s)?|centimetre(?:s)?)/.freeze
KILOMETRE_ALIASES =

Regex pattern for aliases of kilometre unit.

Author:

Since:

  • 0.3.0

/(?:km|kilometer(?:s)?|kilometre(?:s)?)/.freeze
MILE_ALIASES =

Regex pattern for aliases of mile unit.

Author:

Since:

  • 0.5.0

/(?:mi|mile(?:s)?)/.freeze
YARD_ALIASES =

Regex pattern for aliases of yard unit.

Author:

Since:

  • 0.5.0

/(?:yd|yard(?:s)?)/.freeze
FOOT_INCH =

Regex pattern for parsing a length measurement in the format of foot-inch.

Author:

Since:

  • 0.2.0

/\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.

Author:

Since:

  • 0.3.0

/\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.

Author:

Since:

  • 0.3.0

/\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.

Author:

Since:

  • 0.5.0

/\A#{ANY_NUMBER}\s*#{MILE_ALIASES}\s*#{ANY_NUMBER}\s*#{YARD_ALIASES}\z/.freeze

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ UnitMeasurements::Length

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

Examples:

Parse ‘metre-centimetre’ measurement:

CompositeUnitMeasurements::Length.parse("6 m 50 cm") #=> 6.5 m

Parse ‘kilometre-metre’ measurement:

CompositeUnitMeasurements::Length.parse("5 km 500 m") #=> 5.5 km

Parse ‘foot-inch’ measurement:

CompositeUnitMeasurements::Length.parse("5 ft 6 in") #=> 5.5 ft

Parse ‘mile-yard’ measurement:

CompositeUnitMeasurements::Length.parse("20 mi 220 yd") #=> 20.125 mi

Parameters:

  • string (String)

    The string to parse for length measurement.

Returns:

  • (UnitMeasurements::Length)

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

Raises:

  • (UnitMeasurements::ParseError)

    If the string does not match any known format.

Author:

Since:

  • 0.2.0



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.

Parameters:

  • string (String)

    The string representing length measurement in the format of foot-inch.

Returns:

  • (UnitMeasurements::Length)

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

See Also:

Author:

Since:

  • 0.2.0



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.

Parameters:

  • string (String)

    The string representing length measurement in the format of kilometre-metre.

Returns:

  • (UnitMeasurements::Length)

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

See Also:

Author:

Since:

  • 0.3.0



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.

Parameters:

  • string (String)

    The string representing length measurement in the format of metre-centimetre.

Returns:

  • (UnitMeasurements::Length)

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

See Also:

Author:

Since:

  • 0.3.0



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.

Parameters:

  • string (String)

    The string representing length measurement in the format of mile-yard.

Returns:

  • (UnitMeasurements::Length)

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

See Also:

Author:

Since:

  • 0.5.0



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