diff options
author | Nick Holden <nick.r.holden@gmail.com> | 2018-03-06 20:42:49 -0800 |
---|---|---|
committer | Nick Holden <nick.r.holden@gmail.com> | 2018-03-06 20:42:49 -0800 |
commit | 20fa0d92c36681f6cffc715e08c299ff942352f4 (patch) | |
tree | 16c284564bb661a58694bed695b41e9d312bf2cb /activesupport/lib | |
parent | b5aa0266d0a4d5f7a02eb31df730fa172d1a845a (diff) | |
download | rails-20fa0d92c36681f6cffc715e08c299ff942352f4.tar.gz rails-20fa0d92c36681f6cffc715e08c299ff942352f4.tar.bz2 rails-20fa0d92c36681f6cffc715e08c299ff942352f4.zip |
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.
Diffstat (limited to 'activesupport/lib')
4 files changed, 8 insertions, 0 deletions
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. |