diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 8 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/enumerable.rb | 9 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/misc.rb | 13 | ||||
-rw-r--r-- | activesupport/lib/active_support/version.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/enumerable_test.rb | 11 | ||||
-rw-r--r-- | activesupport/test/core_ext/object_and_class_ext_test.rb | 25 | ||||
-rw-r--r-- | activesupport/test/time_zone_test.rb | 2 |
7 files changed, 68 insertions, 4 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 721706c1f7..0717d344d7 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,4 +1,10 @@ -*2.3.0/3.0* +*2.3.0 [Edge]* + +* Added Object#try. ( Taken from http://ozmm.org/posts/try.html ) [Chris Wanstrath] + +* Added Enumerable#none? to check that none of the elements match the block #1408 [Damian Janowski] + +* TimeZone offset tests: use current_period, to ensure TimeZone#utc_offset is up-to-date [Geoff Buesing] * Update bundled TZInfo to 0.3.12 [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb index 788f3a7e9e..a7eaccfed7 100644 --- a/activesupport/lib/active_support/core_ext/enumerable.rb +++ b/activesupport/lib/active_support/core_ext/enumerable.rb @@ -104,4 +104,13 @@ module Enumerable size = block_given? ? select(&block).size : self.size size > 1 end + + # Returns true if none of the elements match the given block. + # + # success = responses.none? {|r| r.status / 100 == 5 } + # + # This is a builtin method in Ruby 1.8.7 and later. + def none?(&block) + !any?(&block) + end unless [].respond_to?(:none?) end diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb index cd0a04d32a..50f824c358 100644 --- a/activesupport/lib/active_support/core_ext/object/misc.rb +++ b/activesupport/lib/active_support/core_ext/object/misc.rb @@ -71,4 +71,17 @@ class Object def acts_like?(duck) respond_to? "acts_like_#{duck}?" end + + # Tries to send the method only if object responds to it. Return +nil+ otherwise. + # + # ==== Example : + # + # # Without try + # @person ? @person.name : nil + # + # With try + # @person.try(:name) + def try(method) + send(method) if respond_to?(method, true) + end end diff --git a/activesupport/lib/active_support/version.rb b/activesupport/lib/active_support/version.rb index 6631f233f1..3e2b29b327 100644 --- a/activesupport/lib/active_support/version.rb +++ b/activesupport/lib/active_support/version.rb @@ -1,8 +1,8 @@ module ActiveSupport module VERSION #:nodoc: MAJOR = 2 - MINOR = 2 - TINY = 1 + MINOR = 3 + TINY = 0 STRING = [MAJOR, MINOR, TINY].join('.') end diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index deb9b7544d..92db977a77 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -79,4 +79,15 @@ class EnumerableTests < Test::Unit::TestCase assert ![ 1, 2 ].many? {|x| x > 1 } assert [ 1, 2, 2 ].many? {|x| x > 1 } end + + def test_none + assert [].none? + assert [nil, false].none? + assert ![1].none? + + assert [].none? {|x| x > 1 } + assert ![ 2 ].none? {|x| x > 1 } + assert ![ 1, 2 ].none? {|x| x > 1 } + assert [ 1, 1 ].none? {|x| x > 1 } + end end diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index e88dcb52d5..fbdce56ac2 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -247,3 +247,28 @@ class ObjectInstanceVariableTest < Test::Unit::TestCase [arg] + instance_exec('bar') { |v| [reverse, v] } } end end + +class ObjectTryTest < Test::Unit::TestCase + def setup + @string = "Hello" + end + + def test_nonexisting_method + method = :undefined_method + assert !@string.respond_to?(method) + assert_nil @string.try(method) + end + + def test_valid_method + assert_equal 5, @string.try(:size) + end + + def test_valid_private_method + class << @string + private :size + end + + assert_equal 5, @string.try(:size) + end + +end diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index d999b9f2a8..60313dc2f7 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -51,7 +51,7 @@ class TimeZoneTest < Test::Unit::TestCase define_method("test_utc_offset_for_#{name}") do silence_warnings do # silence warnings raised by tzinfo gem - period = zone.tzinfo.period_for_utc(Time.utc(2009,1,1,0,0,0)) + period = zone.tzinfo.current_period assert_equal period.utc_offset, zone.utc_offset end end |