Skip to content
Browse files

fix if_present!

  • Loading branch information...
1 parent 005fc47 commit 7d22c6ae260ba3fd8f0ed9a7f9cae9eea5aabd07 @khustochka committed Jan 12, 2010
Showing with 6 additions and 2 deletions.
  1. +1 −1 lib/core_ext/object.rb
  2. +5 −1 test/unit/core_ext/object_test.rb
View
2 lib/core_ext/object.rb
@@ -14,6 +14,6 @@ def if_present(value = nil, &block)
end
def if_present!(value = nil, &block)
- raise "Object not present (nil, false, or empty)." if if_present(value, &block).blank?
+ (result = if_present(value, &block)).blank? ? raise("Object not present (nil, false, or empty).") : result
end
end
View
6 test/unit/core_ext/object_test.rb
@@ -5,7 +5,11 @@ class ObjectTest < ActiveSupport::TestCase
# success tests
assert_equal "Цаплевые", "Цаплевые".if_present
assert_equal "<b>Цаплевые</b>", "Цаплевые".if_present { |val| "<b>#{val}</b>" }
- temp = true.if_present("true") rescue "empty"
+ temp = true.if_present("true")
+ assert_equal "true", temp
+ assert_equal "Цаплевые", "Цаплевые".if_present!
+ assert_equal "<b>Цаплевые</b>", "Цаплевые".if_present! { |val| "<b>#{val}</b>" }
+ temp = true.if_present!("true") rescue "empty"
assert_equal "true", temp
# failure tests

0 comments on commit 7d22c6a

Please sign in to comment.
Something went wrong with that request. Please try again.