Skip to content

Commit

Permalink
Added Object#presence that returns the object if it's #present? other…
Browse files Browse the repository at this point in the history
…wise returns nil [DHH/Colin Kelley]
  • Loading branch information
dhh committed Dec 28, 2009
1 parent a642edb commit 1c47d04
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions activesupport/CHANGELOG
@@ -1,5 +1,7 @@
*Edge*

* Added Object#presence that returns the object if it's #present? otherwise returns nil [DHH/Colin Kelley]

* Add Enumerable#exclude? to bring parity to Enumerable#include? and avoid if !x.include?/else calls [DHH]

* Update Edinburgh TimeZone to use "Europe/London" instead of "Europe/Dublin" #3310 [Phil Ross]
Expand Down
22 changes: 20 additions & 2 deletions activesupport/lib/active_support/core_ext/object/blank.rb
Expand Up @@ -2,11 +2,11 @@ class Object
# An object is blank if it's false, empty, or a whitespace string.
# For example, "", " ", +nil+, [], and {} are blank.
#
# This simplifies
# This simplifies:
#
# if !address.nil? && !address.empty?
#
# to
# ...to:
#
# if !address.blank?
def blank?
Expand All @@ -17,6 +17,24 @@ def blank?
def present?
!blank?
end

# Returns object if it's #present? otherwise returns nil.
# object.presence is equivalent to object.present? ? object : nil.
#
# This is handy for any representation of objects where blank is the same
# as not present at all. For example, this simplifies a common check for
# HTTP POST/query parameters:
#
# state = params[:state] if params[:state].present?
# country = params[:country] if params[:country].present?
# region = state || country || 'US'
#
# ...becomes:
#
# region = params[:state].presence || params[:country].presence || 'US'
def presence
self if present?
end
end

class NilClass #:nodoc:
Expand Down
9 changes: 7 additions & 2 deletions activesupport/test/core_ext/blank_test.rb
Expand Up @@ -14,12 +14,17 @@ class BlankTest < Test::Unit::TestCase
NOT = [ EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], { nil => 0 } ]

def test_blank
BLANK.each { |v| assert v.blank?, "#{v.inspect} should be blank" }
BLANK.each { |v| assert v.blank?, "#{v.inspect} should be blank" }
NOT.each { |v| assert !v.blank?, "#{v.inspect} should not be blank" }
end

def test_present
BLANK.each { |v| assert !v.present?, "#{v.inspect} should not be present" }
NOT.each { |v| assert v.present?, "#{v.inspect} should be present" }
NOT.each { |v| assert v.present?, "#{v.inspect} should be present" }
end

def test_presence
BLANK.each { |v| assert_equal nil, v.presence, "#{v.inspect}.presence should return nil" }
NOT.each { |v| assert_equal v, v.presence, "#{v.inspect}.presence should return self" }
end
end

3 comments on commit 1c47d04

@dmitryelastic
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool !!

@mikhailov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntactic sugar is here!

@mdominiak
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love it!

Please sign in to comment.