diff options
author | Rizwan Reza <rizwanreza@gmail.com> | 2010-05-17 02:40:15 +0430 |
---|---|---|
committer | Rizwan Reza <rizwanreza@gmail.com> | 2010-05-17 02:40:15 +0430 |
commit | d148a6f6ba5f8ee65905f12cd2601fcc377d4852 (patch) | |
tree | 4bcb5e7ad47cfb9a9bb14ffe7c9e003d4646d64c /activesupport | |
parent | e1c773006969abea3c0619fbdc7e32c121b6085f (diff) | |
parent | 6b4e0cc526f55b5532cf99292c94f0a4db53b16f (diff) | |
download | rails-d148a6f6ba5f8ee65905f12cd2601fcc377d4852.tar.gz rails-d148a6f6ba5f8ee65905f12cd2601fcc377d4852.tar.bz2 rails-d148a6f6ba5f8ee65905f12cd2601fcc377d4852.zip |
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/random_access.rb | 12 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/date/calculations.rb | 21 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/logger.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/object/with_options.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/calculations.rb | 14 | ||||
-rw-r--r-- | activesupport/lib/active_support/lazy_load_hooks.rb | 18 | ||||
-rw-r--r-- | activesupport/test/buffered_logger_test.rb | 4 | ||||
-rw-r--r-- | activesupport/test/caching_test.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/array_ext_test.rb | 12 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_ext_test.rb | 26 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_time_ext_test.rb | 8 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_ext_test.rb | 18 | ||||
-rw-r--r-- | activesupport/test/lazy_load_hooks_test.rb | 67 |
14 files changed, 171 insertions, 39 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 6146cc6a97..7afd9926b5 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.0.0 [beta 4/release candidate] (unreleased)* +* Defines prev_(month|year) in Date and Time, and deprecates last_(month|year). [fxn] + * Aliases Date#sunday to Date#end_of_week. [fxn] * Backports Date#>> from 1.9 so that calculations do the right thing around the calendar reform. [fxn] diff --git a/activesupport/lib/active_support/core_ext/array/random_access.rb b/activesupport/lib/active_support/core_ext/array/random_access.rb index 343003f6f7..5338836b29 100644 --- a/activesupport/lib/active_support/core_ext/array/random_access.rb +++ b/activesupport/lib/active_support/core_ext/array/random_access.rb @@ -1,6 +1,16 @@ class Array + # This method is deprecated because it masks Kernel#rand within the Array class itself, + # which may be used by a 3rd party library extending Array in turn. See + # + # https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4555 + # + def rand # :nodoc: + ActiveSupport::Deprecation.warn "Array#rand is deprecated, use random_element instead", caller + random_element + end + # Returns a random element from the array. - def rand + def random_element self[Kernel.rand(length)] end end diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 3038729d34..755d96ce91 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -2,6 +2,7 @@ require 'date' require 'active_support/duration' require 'active_support/core_ext/time/zones' require 'active_support/core_ext/object/acts_like' +require 'active_support/deprecation' class Date if RUBY_VERSION < '1.9' @@ -146,20 +147,30 @@ class Date advance(:years => years) end - # Short-hand for years_ago(1) - def last_year - years_ago(1) + def last_year # :nodoc: + ActiveSupport::Deprecation.warn("Date#last_year has been deprecated, please use Date#prev_year instead", caller) + prev_year end + # Shorthand for years_ago(1) + def prev_year + years_ago(1) + end unless method_defined?(:prev_year) + # Short-hand for years_since(1) def next_year years_since(1) end unless method_defined?(:next_year) + def last_month # :nodoc: + ActiveSupport::Deprecation.warn("Date#last_month has been deprecated, please use Date#prev_month instead", caller) + prev_month + end + # Short-hand for months_ago(1) - def last_month + def prev_month months_ago(1) - end + end unless method_defined?(:prev_month) # Short-hand for months_since(1) def next_month diff --git a/activesupport/lib/active_support/core_ext/logger.rb b/activesupport/lib/active_support/core_ext/logger.rb index 22749229a3..c4994ca2ee 100644 --- a/activesupport/lib/active_support/core_ext/logger.rb +++ b/activesupport/lib/active_support/core_ext/logger.rb @@ -3,7 +3,7 @@ require 'active_support/core_ext/class/attribute_accessors' # Adds the 'around_level' method to Logger. class Logger #:nodoc: def self.define_around_helper(level) - module_eval <<-end_eval + module_eval <<-end_eval, __FILE__, __LINE__ + 1 def around_#{level}(before_message, after_message, &block) # def around_debug(before_message, after_message, &block) self.#{level}(before_message) # self.debug(before_message) return_value = block.call(self) # return_value = block.call(self) diff --git a/activesupport/lib/active_support/core_ext/object/with_options.rb b/activesupport/lib/active_support/core_ext/object/with_options.rb index dd38b7d261..3209cf7f11 100644 --- a/activesupport/lib/active_support/core_ext/object/with_options.rb +++ b/activesupport/lib/active_support/core_ext/object/with_options.rb @@ -1,3 +1,5 @@ +require 'active_support/option_merger' + class Object # An elegant way to factor duplication out of options passed to a series of # method calls. Each method called in the block, with the block variable as diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 2b47ecd543..e27b08ec2e 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -2,6 +2,7 @@ require 'active_support/duration' require 'active_support/core_ext/date/acts_like' require 'active_support/core_ext/date/calculations' require 'active_support/core_ext/date_time/conversions' +require 'active_support/deprecation' class Time COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] @@ -132,8 +133,13 @@ class Time advance(:years => years) end + def last_year # :nodoc: + ActiveSupport::Deprecation.warn("Time#last_year has been deprecated, please use Time#prev_year instead", caller) + prev_year + end + # Short-hand for years_ago(1) - def last_year + def prev_year years_ago(1) end @@ -142,9 +148,13 @@ class Time years_since(1) end + def last_month # :nodoc: + ActiveSupport::Deprecation.warn("Time#last_month has been deprecated, please use Time#prev_month instead", caller) + prev_month + end # Short-hand for months_ago(1) - def last_month + def prev_month months_ago(1) end diff --git a/activesupport/lib/active_support/lazy_load_hooks.rb b/activesupport/lib/active_support/lazy_load_hooks.rb index 642a4c105c..3664431a28 100644 --- a/activesupport/lib/active_support/lazy_load_hooks.rb +++ b/activesupport/lib/active_support/lazy_load_hooks.rb @@ -2,16 +2,26 @@ module ActiveSupport @load_hooks = Hash.new {|h,k| h[k] = [] } @loaded = {} - def self.on_load(name, &block) + def self.on_load(name, options = {}, &block) if base = @loaded[name] - base.instance_eval(&block) + execute_hook(base, options, block) else - @load_hooks[name] << block + @load_hooks[name] << [block, options] + end + end + + def self.execute_hook(base, options, block) + if options[:yield] + block.call(base) + else + base.instance_eval(&block) end end def self.run_load_hooks(name, base = Object) - @load_hooks[name].each { |hook| base.instance_eval(&hook) } @loaded[name] = base + @load_hooks[name].each do |hook, options| + execute_hook(base, options, hook) + end end end
\ No newline at end of file diff --git a/activesupport/test/buffered_logger_test.rb b/activesupport/test/buffered_logger_test.rb index 7a0ec2e910..5b072d4102 100644 --- a/activesupport/test/buffered_logger_test.rb +++ b/activesupport/test/buffered_logger_test.rb @@ -73,7 +73,7 @@ class BufferedLoggerTest < Test::Unit::TestCase end @logger.flush - assert !@output.string.empty?, @logger.send(:buffer).size + assert !@output.string.empty?, @logger.send(:buffer).size.to_s end define_method "test_disabling_auto_flush_with_#{disable.inspect}_should_flush_at_max_buffer_size_as_failsafe" do @@ -86,7 +86,7 @@ class BufferedLoggerTest < Test::Unit::TestCase end @logger.info 'there it is.' - assert !@output.string.empty?, @logger.send(:buffer).size + assert !@output.string.empty?, @logger.send(:buffer).size.to_s end end diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index d9ff1207e7..3e14c754b7 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -375,7 +375,7 @@ module LocalCacheBehavior def test_local_cache_of_write_nil @cache.with_local_cache do - assert true, @cache.write('foo', nil) + assert @cache.write('foo', nil) assert_nil @cache.read('foo') @peek.write('foo', 'bar') assert_nil @cache.read('foo') @@ -394,7 +394,7 @@ module LocalCacheBehavior @cache.with_local_cache do @cache.write('foo', 'bar') @peek.delete('foo') - assert true, @cache.exist?('foo') + assert @cache.exist?('foo') end end diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index e7617466c2..ebd6806416 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -358,15 +358,19 @@ class ArrayUniqByTests < Test::Unit::TestCase end end -class ArrayExtRandomTests < Test::Unit::TestCase +class ArrayExtRandomTests < ActiveSupport::TestCase def test_random_element_from_array - assert_nil [].rand + assert_nil [].random_element Kernel.expects(:rand).with(1).returns(0) - assert_equal 'x', ['x'].rand + assert_equal 'x', ['x'].random_element Kernel.expects(:rand).with(3).returns(1) - assert_equal 2, [1, 2, 3].rand + assert_equal 2, [1, 2, 3].random_element + end + + def test_deprecated_rand_on_array + assert_deprecated { [].rand } end end diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 2b66fd03d0..1bf118e3b7 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -1,7 +1,7 @@ require 'abstract_unit' require 'active_support/time' -class DateExtCalculationsTest < Test::Unit::TestCase +class DateExtCalculationsTest < ActiveSupport::TestCase def test_to_s date = Date.new(2005, 2, 21) assert_equal "2005-02-21", date.to_s @@ -145,16 +145,20 @@ class DateExtCalculationsTest < Test::Unit::TestCase assert_equal Date.new(2005,2,28), Date.new(2004,2,29).years_since(1) # 1 year since leap day end - def test_last_year - assert_equal Date.new(2004,6,5), Date.new(2005,6,5).last_year + def test_last_year_is_deprecated + assert_deprecated { Date.today.last_year } end - def test_last_year_in_leap_years - assert_equal Date.new(1999,2,28), Date.new(2000,2,29).last_year + def test_prev_year + assert_equal Date.new(2004,6,5), Date.new(2005,6,5).prev_year end - def test_last_year_in_calendar_reform - assert_equal Date.new(1582,10,4), Date.new(1583,10,14).last_year + def test_prev_year_in_leap_years + assert_equal Date.new(1999,2,28), Date.new(2000,2,29).prev_year + end + + def test_prev_year_in_calendar_reform + assert_equal Date.new(1582,10,4), Date.new(1583,10,14).prev_year end def test_next_year @@ -225,8 +229,12 @@ class DateExtCalculationsTest < Test::Unit::TestCase assert_equal Date.new(2005, 9, 30), Date.new(2005, 8, 31).next_month end - def test_last_month_on_31st - assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).last_month + def test_last_month_is_deprecated + assert_deprecated { Date.today.last_month } + end + + def test_prev_month_on_31st + assert_equal Date.new(2004, 2, 29), Date.new(2004, 3, 31).prev_month end def test_yesterday_constructor diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index f9af059acd..4780760a19 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -127,8 +127,8 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal DateTime.civil(2005,2,28,10), DateTime.civil(2004,2,29,10,0,0).years_since(1) # 1 year since leap day end - def test_last_year - assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).last_year + def test_prev_year + assert_equal DateTime.civil(2004,6,5,10), DateTime.civil(2005,6,5,10,0,0).prev_year end def test_next_year @@ -200,8 +200,8 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal DateTime.civil(2005, 9, 30), DateTime.civil(2005, 8, 31).next_month end - def test_last_month_on_31st - assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).last_month + def test_prev_month_on_31st + assert_equal DateTime.civil(2004, 2, 29), DateTime.civil(2004, 3, 31).prev_month end def test_xmlschema diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 342d6ab577..30ee1d1652 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -1,7 +1,7 @@ require 'abstract_unit' require 'active_support/time' -class TimeExtCalculationsTest < Test::Unit::TestCase +class TimeExtCalculationsTest < ActiveSupport::TestCase def test_seconds_since_midnight assert_equal 1,Time.local(2005,1,1,0,0,1).seconds_since_midnight assert_equal 60,Time.local(2005,1,1,0,1,0).seconds_since_midnight @@ -166,8 +166,12 @@ class TimeExtCalculationsTest < Test::Unit::TestCase # assert_equal Time.local(2182,6,5,10), Time.local(2005,6,5,10,0,0).years_since(177) end - def test_last_year - assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).last_year + def test_last_year_is_deprecated + assert_deprecated { Time.now.last_year } + end + + def test_prev_year + assert_equal Time.local(2004,6,5,10), Time.local(2005,6,5,10,0,0).prev_year end def test_next_year @@ -615,8 +619,12 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal Time.local(2005, 9, 30), Time.local(2005, 8, 31).next_month end - def test_last_month_on_31st - assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).last_month + def test_last_month_is_deprecated + assert_deprecated { Time.now.last_month } + end + + def test_prev_month_on_31st + assert_equal Time.local(2004, 2, 29), Time.local(2004, 3, 31).prev_month end def test_xmlschema_is_available diff --git a/activesupport/test/lazy_load_hooks_test.rb b/activesupport/test/lazy_load_hooks_test.rb new file mode 100644 index 0000000000..58ccc14324 --- /dev/null +++ b/activesupport/test/lazy_load_hooks_test.rb @@ -0,0 +1,67 @@ +require 'abstract_unit' + +class LazyLoadHooksTest < ActiveSupport::TestCase + def test_basic_hook + i = 0 + ActiveSupport.on_load(:basic_hook) { i += 1 } + ActiveSupport.run_load_hooks(:basic_hook) + assert_equal 1, i + end + + def test_hook_registered_after_run + i = 0 + ActiveSupport.run_load_hooks(:registered_after) + assert_equal 0, i + ActiveSupport.on_load(:registered_after) { i += 1 } + assert_equal 1, i + end + + def test_hook_receives_a_context + i = 0 + ActiveSupport.on_load(:contextual) { i += incr } + assert_equal 0, i + ActiveSupport.run_load_hooks(:contextual, FakeContext.new(2)) + assert_equal 2, i + end + + def test_hook_receives_a_context_afterward + i = 0 + ActiveSupport.run_load_hooks(:contextual_after, FakeContext.new(2)) + assert_equal 0, i + ActiveSupport.on_load(:contextual_after) { i += incr } + assert_equal 2, i + end + + def test_hook_with_yield_true + i = 0 + ActiveSupport.on_load(:contextual_yield, :yield => true) do |obj| + i += obj.incr + incr_amt + end + assert_equal 0, i + ActiveSupport.run_load_hooks(:contextual_yield, FakeContext.new(2)) + assert_equal 7, i + end + + def test_hook_with_yield_true_afterward + i = 0 + ActiveSupport.run_load_hooks(:contextual_yield_after, FakeContext.new(2)) + assert_equal 0, i + ActiveSupport.on_load(:contextual_yield_after, :yield => true) do |obj| + i += obj.incr + incr_amt + end + assert_equal 7, i + end + +private + + def incr_amt + 5 + end + + class FakeContext + attr_reader :incr + def initialize(incr) + @incr = incr + end + end +end
\ No newline at end of file |