From 20fa0d92c36681f6cffc715e08c299ff942352f4 Mon Sep 17 00:00:00 2001 From: Nick Holden Date: Tue, 6 Mar 2018 20:42:49 -0800 Subject: Add `before?` and `after?` methods to date and time classes Equality comparisons between dates and times can take some extra time to comprehend. I tend to think of a date or time as "before" or "after" another date or time, but I naturally read `<` and `>` as "less than" and "greater than." This change seeks to make date/time comparisons more human readable. --- activesupport/lib/active_support/core_ext/date/calculations.rb | 2 ++ activesupport/lib/active_support/core_ext/date_time/calculations.rb | 2 ++ activesupport/lib/active_support/core_ext/time/calculations.rb | 2 ++ activesupport/lib/active_support/time_with_zone.rb | 2 ++ 4 files changed, 8 insertions(+) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 1cd7acb05d..e7be5c1376 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -142,4 +142,6 @@ class Date end alias_method :compare_without_coercion, :<=> alias_method :<=>, :compare_with_coercion + alias_method :before?, :< + alias_method :after?, :> end diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index e61b23f842..eace125883 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -208,4 +208,6 @@ class DateTime super end end + alias_method :before?, :< + alias_method :after?, :> end diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 120768dec5..0507f9e652 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -302,6 +302,8 @@ class Time end alias_method :compare_without_coercion, :<=> alias_method :<=>, :compare_with_coercion + alias_method :before?, :< + alias_method :after?, :> # Layers additional behavior on Time#eql? so that ActiveSupport::TimeWithZone instances # can be eql? to an equivalent Time diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 20650ce714..7e71318404 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -225,6 +225,8 @@ module ActiveSupport def <=>(other) utc <=> other end + alias_method :before?, :< + alias_method :after?, :> # Returns true if the current object's time is within the specified # +min+ and +max+ time. -- cgit v1.2.3