Permalink
Browse files

Merge branch 'master' of github.com:rails/rails

  • Loading branch information...
2 parents 99f7e0f + 1c47d04 commit 9abbe9f0b311418b19e9c036e9b67c84a6bf2b7c Yehuda Katz committed Dec 28, 2009
View
@@ -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]
@@ -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 @@ 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:
@@ -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
View
@@ -1,5 +1,7 @@
*Edge*
+* Added default .gitignore (this is just recognizing Git market share, don't throw a hissy if you use another SCM) [DHH]
+
* Added cookies.permanent, cookies.signed, and cookies.permanent.signed accessor for common cookie actions [DHH]. Examples:
cookies.permanent[:prefers_open_id] = true
@@ -49,6 +49,7 @@ def create_root
def create_root_files
copy_file "README"
+ copy_file "gitignore", ".gitignore"
template "Rakefile"
template "config.ru"
template "Gemfile"
@@ -0,0 +1,3 @@
+db/*.sqlite3
+log/*.log
+tmp/**/*

0 comments on commit 9abbe9f

Please sign in to comment.