Skip to content

Commit

Permalink
Short-circuit blank? on date and time values
Browse files Browse the repository at this point in the history
The concept of a blank date or time doesn't make sense so we can short
circuit the calls for `blank?` on these classes to gain small speed boost.

Fixes rails#21657
  • Loading branch information
pixeltrix committed Sep 21, 2015
1 parent cd2c0ad commit b3eac82
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 1 deletion.
6 changes: 6 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,3 +1,9 @@
* Short-circuit `blank?` on date and time values since they are never blank.

Fixes #21657

*Andrew White*

* Replaced deprecated `ThreadSafe::Cache` with its successor `Concurrent::Map` now that
the thread_safe gem has been merged into concurrent-ruby.

Expand Down
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/core_ext/date.rb
@@ -1,5 +1,5 @@
require 'active_support/core_ext/date/acts_like'
require 'active_support/core_ext/date/blank'
require 'active_support/core_ext/date/calculations'
require 'active_support/core_ext/date/conversions'
require 'active_support/core_ext/date/zones'

12 changes: 12 additions & 0 deletions activesupport/lib/active_support/core_ext/date/blank.rb
@@ -0,0 +1,12 @@
require 'date'

class Date #:nodoc:
# No Date is blank:
#
# Date.today.blank? # => false
#
# @return [false]
def blank?
false
end
end
1 change: 1 addition & 0 deletions activesupport/lib/active_support/core_ext/date_time.rb
@@ -1,4 +1,5 @@
require 'active_support/core_ext/date_time/acts_like'
require 'active_support/core_ext/date_time/blank'
require 'active_support/core_ext/date_time/calculations'
require 'active_support/core_ext/date_time/conversions'
require 'active_support/core_ext/date_time/zones'
12 changes: 12 additions & 0 deletions activesupport/lib/active_support/core_ext/date_time/blank.rb
@@ -0,0 +1,12 @@
require 'date'

class DateTime #:nodoc:
# No DateTime is ever blank:
#
# DateTime.now.blank? # => false
#
# @return [false]
def blank?
false
end
end
11 changes: 11 additions & 0 deletions activesupport/lib/active_support/core_ext/object/blank.rb
Expand Up @@ -127,3 +127,14 @@ def blank?
false
end
end

class Time #:nodoc:
# No Time is blank:
#
# Time.now.blank? # => false
#
# @return [false]
def blank?
false
end
end
5 changes: 5 additions & 0 deletions activesupport/lib/active_support/time_with_zone.rb
Expand Up @@ -388,6 +388,11 @@ def is_a?(klass)
end
alias_method :kind_of?, :is_a?

# An instance of ActiveSupport::TimeWithZone is never blank
def blank?
false
end

def freeze
period; utc; time # preload instance variables before freezing
super
Expand Down

0 comments on commit b3eac82

Please sign in to comment.