From 19fa6b96ea1a015968d19bfafa5bc2b558b495bf Mon Sep 17 00:00:00 2001 From: Robert Qualls Date: Sun, 21 Feb 2016 12:46:15 -0600 Subject: [PATCH] Reduce complexity of VoltTime#compare --- lib/volt/helpers/time/calculations.rb | 68 +++++++++++++-------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/lib/volt/helpers/time/calculations.rb b/lib/volt/helpers/time/calculations.rb index e0c7ced9..3478b215 100644 --- a/lib/volt/helpers/time/calculations.rb +++ b/lib/volt/helpers/time/calculations.rb @@ -2,9 +2,9 @@ # https://github.com/rails/rails/blob/ca9736e78ca9348e785a5c78c8cc085c0c2d4731/activesupport/lib/active_support/core_ext/time/calculations.rb class VoltTime - + COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] - + class << self # Returns the number of days in a month. If no year is provided assumes # the current year @@ -15,7 +15,7 @@ def days_in_month(month, year = now.year) COMMON_YEAR_DAYS_IN_MONTH[month] end end - + # Returns true if the year is a leap year, otherwise false def leap?(year) if year%400 == 0 || (year%4 == 0 && year%100 != 0) @@ -25,7 +25,7 @@ def leap?(year) end end end - + # Returns a new Time where one or more of the elements have been changed according # to the +options+ parameter. The time options (:hour, :min, # :sec, :usec) reset cascadingly, so if only the hour is passed, @@ -61,7 +61,7 @@ def advance(options) t = advance_seconds(options[:secs]) if options[:secs] t end - + # Compares two VoltTime object to the given accuracy # The +accuracy+ parameter can be :year, :month # :day, :hour, :min, :sec @@ -69,21 +69,19 @@ def advance(options) # (e.g. the same year or the same day), 1 if the date called on is later # than the parameter, or -1 if the date called on is earlier than the parameter def compare(other, accuracy) - if accuracy == :year - year <=> other.year - elsif accuracy == :month - compare_date_components(other, :month, :year) - elsif accuracy == :day - compare_date_components(other, :day, :month) - elsif [:hour, :min, :sec].include? accuracy - change(accuracy => send(accuracy)) <=> other.change(accuracy => other.send(accuracy)) + case accuracy + when :year then year <=> other.year + when :month then compare_date_components(other, :month, :year) + when :day then compare_date_components(other, :day, :month) + when :hour, :min, :sec + change(accuracy => send(accuracy)) <=> other.change(accuracy => other.send(accuracy)) end end def compare?(other, accuracy) compare(other, accuracy) == 0 ? true : false - end - + end + # Advances time by a number of years def advance_years(years) advance_to_date(to_date >> (years*12)) @@ -93,43 +91,43 @@ def advance_years(years) def advance_months(months) advance_to_date(to_date >> months) end - + # Advances time by a number of days def advance_days(days) advance_to_date(to_date + days) end - + # Advances time by a number of seconds def advance_seconds(secs) self + secs end - + # Advances time by a number of hours def advance_hours(hours) self + (hours * 60 * 60) end - + # Advances time by a number of minutes def advance_minutes(mins) self + (mins * 60) end - + # Converts the time to a date def to_date Date.new(year, month, day) end - + # Advances the time to the given date def advance_to_date(date) VoltTime.new(:utc, date.year, date.month, date.day, hour, min, sec + (usec/1.0e6)) end - + # Adds a duration to the time def plus_with_duration(other) if other.is_a?(Volt::Duration) end end - + # Returns a new VoltTime representing the beginning of the day, 00:00:00 def beginning_of_day change(hour: 0, min: 0, sec: 0) @@ -140,7 +138,7 @@ def beginning_of_day def end_of_day VoltTime.new(:utc, year, month, day, 23, 59, 59.999) end - + # Returns a new Time for the middle of the day i.e. 12:00:00 def middle_of_day change(hour: 12) @@ -150,22 +148,22 @@ def middle_of_day def seconds_since_midnight to_f - change(hour: 0).to_f end - + # Returns the number of seconds to the 23:59:59 of the current day def seconds_until_end_of_day end_of_day.to_f - to_f end - + # Returns a new VoltTime for the number of seconds ago def ago(seconds) since(-seconds) end - + # Returns a new VoltTime for the number of seconds since the current time def since(seconds) VoltTime.new.set_time(@time + seconds) end - + # Returns a new Time for the beginning of the current hour def beginning_of_hour change(min: 0) @@ -176,29 +174,29 @@ def beginning_of_hour def end_of_hour change(min: 59, sec: 59.999) end - + # Returns a new Time for beginning of the current minute def beginning_of_minute change(sec: 0) end - + # Returns a new Time for the end of the current minute def end_of_minute change(sec: 59.999) end - + # Returns a Range for the start to end of day def all_day beginning_of_day..end_of_day end - + private - def compare_date_components(other, component, higher_component) + def compare_date_components(other, component, higher_component) if compare(other, higher_component) == 0 send(component) <=> other.send(component) else compare(other, higher_component) end - end - + end + end