From 1c47d04ea5ac19601b316daf8fdc6f38c50eec73 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 27 Dec 2009 17:54:43 -0800 Subject: Added Object#presence that returns the object if it's #present? otherwise returns nil [DHH/Colin Kelley] --- activesupport/CHANGELOG | 2 ++ .../lib/active_support/core_ext/object/blank.rb | 22 ++++++++++++++++++++-- activesupport/test/core_ext/blank_test.rb | 9 +++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index cc4a2ff90e..9b0a84678a 100644 --- a/activesupport/CHANGELOG +++ b/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] diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb index 9a1f663bf3..eb99bb1a36 100644 --- a/activesupport/lib/active_support/core_ext/object/blank.rb +++ b/activesupport/lib/active_support/core_ext/object/blank.rb @@ -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? @@ -17,6 +17,24 @@ class Object 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: diff --git a/activesupport/test/core_ext/blank_test.rb b/activesupport/test/core_ext/blank_test.rb index 1dbbf3ff30..ed6c625a0a 100644 --- a/activesupport/test/core_ext/blank_test.rb +++ b/activesupport/test/core_ext/blank_test.rb @@ -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 -- cgit v1.2.3