Class: CompositeUnitMeasurements::Time
- Inherits:
-
Object
- Object
- CompositeUnitMeasurements::Time
- Defined in:
- lib/composite_unit_measurements/time.rb
Overview
A parser handling time
measurements, particularly for composite units like hour:minute:second,microsecond, minute-second
, hour-minute
, etc.
Constant Summary collapse
- HOUR_ALIASES =
Regex pattern for aliases of
hour
unit. /(?:h|hr|hour(?:s)?)/.freeze
- MINUTE_ALIASES =
Regex pattern for aliases of
minute
unit. /(?:min|minute(?:s)?)/.freeze
- SECOND_ALIASES =
Regex pattern for aliases of
second
unit. /(?:s|sec|second(?:s)?)/.freeze
- DAY_ALIASES =
Regex pattern for aliases of
day
unit. /(?:d|day(?:s)?)/.freeze
- WEEK_ALIASES =
Regex pattern for aliases of
week
unit. /(?:wk|week(?:s)?)/.freeze
- MONTH_ALIASES =
Regex pattern for aliases of
month
unit. /(?:mo|month(?:s)?)/.freeze
- HOUR_MINUTE =
Regex pattern for parsing a time measurement in the format of
hour-minute
. /\A#{ANY_NUMBER}\s*#{HOUR_ALIASES}\s*#{ANY_NUMBER}\s*#{MINUTE_ALIASES}\z/.freeze
- DURATION =
Regex pattern for parsing duration in the format of
hour:minute:second
or hour:minute:second,microsecond. /\A(?<hour>#{REAL_NUMBER}):(?<min>#{REAL_NUMBER}):(?:(?<sec>#{REAL_NUMBER}))?(?:,(?<msec>#{REAL_NUMBER}))?\z/.freeze
- MINUTE_SECOND =
Regex pattern for parsing a time measurement in the format of
minute-second
. /\A#{ANY_NUMBER}\s*#{MINUTE_ALIASES}\s*#{ANY_NUMBER}\s*#{SECOND_ALIASES}\z/.freeze
- WEEK_DAY =
Regex pattern for parsing a time measurement in the format of
week-day
. /\A#{ANY_NUMBER}\s*#{WEEK_ALIASES}\s*#{ANY_NUMBER}\s*#{DAY_ALIASES}\z/.freeze
- MONTH_DAY =
Regex pattern for parsing a time measurement in the format of
month-day
. /\A#{ANY_NUMBER}\s#{MONTH_ALIASES}\s*#{ANY_NUMBER}\s*#{DAY_ALIASES}\z/.freeze
Class Method Summary collapse
-
.parse(string) ⇒ UnitMeasurements::Time
Parses a given
string
into aUnitMeasurements::Time
object. -
.parse_duration(string) ⇒ UnitMeasurements::Time
private
Parses a
string
representing time duration in the format of hour:minute:second,microsecond orhour:minute:second
into aUnitMeasurements::Time
object. -
.parse_hour_minute(string) ⇒ UnitMeasurements::Time
private
Parses a
string
representing a time in the format ofhour-minute
into aUnitMeasurements::Time
object. -
.parse_minute_second(string) ⇒ UnitMeasurements::Time
private
Parses a
string
representing a time in the format ofminute-second
into aUnitMeasurements::Time
object. -
.parse_month_day(string) ⇒ UnitMeasurements::Time
private
Parses a
string
representing a time in the format ofmonth-day
into aUnitMeasurements::Time
object. -
.parse_week_day(string) ⇒ UnitMeasurements::Time
private
Parses a
string
representing a time in the format ofweek-day
into aUnitMeasurements::Time
object.
Class Method Details
.parse(string) ⇒ UnitMeasurements::Time
Parses a given string
into a UnitMeasurements::Time
object.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/composite_unit_measurements/time.rb', line 37 def parse(string) case string when HOUR_MINUTE then parse_hour_minute(string) when DURATION then parse_duration(string) when MINUTE_SECOND then parse_minute_second(string) when WEEK_DAY then parse_week_day(string) when MONTH_DAY then parse_month_day(string) else raise UnitMeasurements::ParseError, string end end |
.parse_duration(string) ⇒ UnitMeasurements::Time (private)
Parses a string
representing time duration in the format of hour:minute:second,microsecond or hour:minute:second
into a UnitMeasurements::Time
object.
84 85 86 87 88 89 90 91 92 |
# File 'lib/composite_unit_measurements/time.rb', line 84 def parse_duration(string) hour, minute, second, microsecond = string.match(DURATION)&.captures raise ArgumentError, "Invalid Duration" if [hour, minute, second, microsecond].all?(&:nil?) UnitMeasurements::Time.new((hour || 0), "h") + UnitMeasurements::Time.new((minute || 0), "min") + UnitMeasurements::Time.new((second || 0), "s") + UnitMeasurements::Time.new((microsecond || 0), "μs") end |
.parse_hour_minute(string) ⇒ UnitMeasurements::Time (private)
Parses a string
representing a time in the format of hour-minute
into a UnitMeasurements::Time
object.
62 63 64 65 66 67 68 |
# File 'lib/composite_unit_measurements/time.rb', line 62 def parse_hour_minute(string) hour, minute = string.match(HOUR_MINUTE)&.captures if hour && minute UnitMeasurements::Time.new(hour, "h") + UnitMeasurements::Time.new(minute, "min") end end |
.parse_minute_second(string) ⇒ UnitMeasurements::Time (private)
Parses a string
representing a time in the format of minute-second
into a UnitMeasurements::Time
object.
106 107 108 109 110 111 112 |
# File 'lib/composite_unit_measurements/time.rb', line 106 def parse_minute_second(string) minute, second = string.match(MINUTE_SECOND)&.captures if minute && second UnitMeasurements::Time.new(minute, "min") + UnitMeasurements::Time.new(second, "s") end end |
.parse_month_day(string) ⇒ UnitMeasurements::Time (private)
Parses a string
representing a time in the format of month-day
into a UnitMeasurements::Time
object.
146 147 148 149 150 151 152 |
# File 'lib/composite_unit_measurements/time.rb', line 146 def parse_month_day(string) month, day = string.match(MONTH_DAY)&.captures if month && day UnitMeasurements::Time.new(month, "mo") + UnitMeasurements::Time.new(day, "d") end end |
.parse_week_day(string) ⇒ UnitMeasurements::Time (private)
Parses a string
representing a time in the format of week-day
into a UnitMeasurements::Time
object.
126 127 128 129 130 131 132 |
# File 'lib/composite_unit_measurements/time.rb', line 126 def parse_week_day(string) week, day = string.match(WEEK_DAY)&.captures if week && day UnitMeasurements::Time.new(week, "wk") + UnitMeasurements::Time.new(day, "d") end end |