-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19878 from pabloh/replace_alias_chains_with_prepend
Replace use of alias chains with prepend at core_ext/date and core_ext/time
- Loading branch information
Showing
5 changed files
with
79 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
activesupport/lib/active_support/core_ext/date/operators.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'active_support/core_ext/date_and_time/with_duration' | ||
|
||
module ActiveSupport | ||
module DateOperators # :nodoc: | ||
include DateAndTime::WithDuration | ||
|
||
# Allow Date to be compared with Time by converting to DateTime and relying on the <=> from there. | ||
def <=>(other) | ||
if other.is_a?(Time) | ||
self.to_datetime <=> other | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
activesupport/lib/active_support/core_ext/date_and_time/with_duration.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'active_support/duration' | ||
|
||
module ActiveSupport | ||
module DateAndTime | ||
module WithDuration #:nodoc: | ||
def +(other) #:nodoc: | ||
if ActiveSupport::Duration === other | ||
other.since(self) | ||
else | ||
super | ||
end | ||
end | ||
|
||
def -(other) #:nodoc: | ||
if ActiveSupport::Duration === other | ||
self + (-other) | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
activesupport/lib/active_support/core_ext/time/operators.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'active_support/core_ext/date_and_time/with_duration' | ||
|
||
module ActiveSupport | ||
module TimeOperators # :nodoc: | ||
include DateAndTime::WithDuration | ||
|
||
# Layers additional behavior on Time#<=> so that DateTime and ActiveSupport::TimeWithZone instances | ||
# can be chronologically compared with a Time | ||
def <=>(other) | ||
# we're avoiding Time#to_datetime and Time#to_time because they're expensive | ||
if other.class == Time | ||
super | ||
elsif other.is_a?(Time) | ||
super(other.to_time) | ||
else | ||
to_datetime <=> other | ||
end | ||
end | ||
|
||
# Layers additional behavior on Time#eql? so that ActiveSupport::TimeWithZone instances | ||
# can be eql? to an equivalent Time | ||
def eql?(other) | ||
# if other is an ActiveSupport::TimeWithZone, coerce a Time instance from it so we can do eql? comparison | ||
other = other.comparable_time if other.respond_to?(:comparable_time) | ||
super | ||
end | ||
|
||
# Time#- can also be used to determine the number of seconds between two Time instances. | ||
# We're layering on additional behavior so that ActiveSupport::TimeWithZone instances | ||
# are coerced into values that Time#- will recognize | ||
def -(other) | ||
other = other.comparable_time if other.respond_to?(:comparable_time) | ||
other.is_a?(DateTime) ? to_f - other.to_f : super | ||
end | ||
end | ||
end |