From 0da4367c173b139bfe653df9defdfb927541da45 Mon Sep 17 00:00:00 2001 From: Ryan Angilly Date: Thu, 29 Jan 2009 00:15:37 -0500 Subject: [PATCH] adding or_if_blank.rb and test --- .../lib/active_support/core_ext/or_if_blank.rb | 19 +++++++++++++++++++ activesupport/test/core_ext/or_if_blank_test.rb | 13 +++++++++++++ 2 files changed, 32 insertions(+), 0 deletions(-) create mode 100644 activesupport/lib/active_support/core_ext/or_if_blank.rb create mode 100644 activesupport/test/core_ext/or_if_blank_test.rb diff --git a/activesupport/lib/active_support/core_ext/or_if_blank.rb b/activesupport/lib/active_support/core_ext/or_if_blank.rb new file mode 100644 index 0000000..0ebc311 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/or_if_blank.rb @@ -0,0 +1,19 @@ +class Object + # This simplifies cases where you want to do: + # + # self.email.blank? ? self.name : self.email + # + # you can now do: + # + # self.email.or_if_blank self.name + # + # It will work on any object that implements blank? + + def or_if_blank(val) + if respond_to? :blank? + self.blank? ? val : self + else + self + end + end +end diff --git a/activesupport/test/core_ext/or_if_blank_test.rb b/activesupport/test/core_ext/or_if_blank_test.rb new file mode 100644 index 0000000..240bc6b --- /dev/null +++ b/activesupport/test/core_ext/or_if_blank_test.rb @@ -0,0 +1,13 @@ +require 'abstract_unit' + +class OrIfBlankTest < Test::Unit::TestCase + BLANK = ["", [], {}, nil, false] + NOT = ["a", [1,2], {:a => 2}, true, 0] + + def test_or_if_blank + substitute = "substitute me!" + + BLANK.each { |v| assert_equal v.or_if_blank(substitute), substitute } + NOT.each { |v| assert_equal v.or_if_blank(substitute), v } + end +end -- 1.5.4.3 From 4f69395c02bffeadf978b154ca1ad9e3a57376e0 Mon Sep 17 00:00:00 2001 From: Ryan Angilly Date: Thu, 29 Jan 2009 00:25:14 -0500 Subject: [PATCH] Adding tests for case where object does not respond to blank --- activesupport/test/core_ext/or_if_blank_test.rb | 10 ++++++++++ 1 files changed, 10 insertions(+), 0 deletions(-) diff --git a/activesupport/test/core_ext/or_if_blank_test.rb b/activesupport/test/core_ext/or_if_blank_test.rb index 240bc6b..e5fb785 100644 --- a/activesupport/test/core_ext/or_if_blank_test.rb +++ b/activesupport/test/core_ext/or_if_blank_test.rb @@ -1,5 +1,9 @@ require 'abstract_unit' +class NoBlank + undef blank? +end + class OrIfBlankTest < Test::Unit::TestCase BLANK = ["", [], {}, nil, false] NOT = ["a", [1,2], {:a => 2}, true, 0] @@ -10,4 +14,10 @@ class OrIfBlankTest < Test::Unit::TestCase BLANK.each { |v| assert_equal v.or_if_blank(substitute), substitute } NOT.each { |v| assert_equal v.or_if_blank(substitute), v } end + + def test_when_blank_not_defined + a = NoBlank.new + assert !a.respond_to?(:blank?) + assert_equal a.or_if_blank("string"), a + end end -- 1.5.4.3 From 83f2a4124a21f18a5536acf64ad81f911c9487f8 Mon Sep 17 00:00:00 2001 From: Ryan Angilly Date: Thu, 29 Jan 2009 08:05:00 -0500 Subject: [PATCH] adding block option to or_if_blank --- .../lib/active_support/core_ext/or_if_blank.rb | 8 +++- activesupport/test/core_ext/or_if_blank_test.rb | 39 +++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/or_if_blank.rb b/activesupport/lib/active_support/core_ext/or_if_blank.rb index 0ebc311..2541186 100644 --- a/activesupport/lib/active_support/core_ext/or_if_blank.rb +++ b/activesupport/lib/active_support/core_ext/or_if_blank.rb @@ -7,11 +7,15 @@ class Object # # self.email.or_if_blank self.name # + # or for lazy evaluation: + # + # self.email.or_if_blank { self.name } + # # It will work on any object that implements blank? - def or_if_blank(val) + def or_if_blank(val = nil) if respond_to? :blank? - self.blank? ? val : self + self.blank? ? (val || yield) : self else self end diff --git a/activesupport/test/core_ext/or_if_blank_test.rb b/activesupport/test/core_ext/or_if_blank_test.rb index e5fb785..c2bbb37 100644 --- a/activesupport/test/core_ext/or_if_blank_test.rb +++ b/activesupport/test/core_ext/or_if_blank_test.rb @@ -4,9 +4,21 @@ class NoBlank undef blank? end +class SampleObj + attr_accessor :a + + def initialize(options) + self.a = options[:a] + end + + def blank? + self.a == "" + end +end + class OrIfBlankTest < Test::Unit::TestCase - BLANK = ["", [], {}, nil, false] - NOT = ["a", [1,2], {:a => 2}, true, 0] + BLANK = ["", [], {}, nil, false, SampleObj.new(:a => "")] + NOT = ["a", [1,2], {:a => 2}, true, 0, SampleObj.new(:a => "something")] def test_or_if_blank substitute = "substitute me!" @@ -20,4 +32,27 @@ class OrIfBlankTest < Test::Unit::TestCase assert !a.respond_to?(:blank?) assert_equal a.or_if_blank("string"), a end + + def test_takes_block + substitute = "substitute me!" + + BLANK.each { |v| assert_equal v.or_if_blank {substitute}, substitute } + NOT.each { |v| assert_equal v.or_if_blank {substitute}, v } + end + + def test_block_computation_can_modifiying_receiver + blank_obj = SampleObj.new(:a => "") + assert blank_obj.blank? + assert_equal blank_obj.or_if_blank { blank_obj.a << "1" }, "1" + assert !blank_obj.blank? + end + + def test_block_evaluates_lazily + orig_value = "a" + full_obj = SampleObj.new(:a => orig_value) + assert !full_obj.blank? + assert_equal full_obj.or_if_blank { full_obj.a << "1" }, full_obj + assert_equal full_obj.a, orig_value + end + end -- 1.5.4.3