diff options
author | rick <rick@spacemonkey.local> | 2008-05-10 17:46:55 -0700 |
---|---|---|
committer | rick <rick@spacemonkey.local> | 2008-05-10 17:46:55 -0700 |
commit | d09a8446d5606a5a0b5c024224b09a1318e9cf4d (patch) | |
tree | 199ef3554f731c980ea5726e67e34af4ea057c2e /activesupport | |
parent | c8451aeeea200043d8a3e6eae9c49def3a154ddb (diff) | |
parent | a7ea06b4ebe252e258f83e7de945b4baa30ec3bc (diff) | |
download | rails-d09a8446d5606a5a0b5c024224b09a1318e9cf4d.tar.gz rails-d09a8446d5606a5a0b5c024224b09a1318e9cf4d.tar.bz2 rails-d09a8446d5606a5a0b5c024224b09a1318e9cf4d.zip |
fix merge conflict with actionpack changelog
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 8 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/date/calculations.rb | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/indifferent_access.rb | 43 | ||||
-rw-r--r-- | activesupport/lib/active_support/deprecation.rb | 14 | ||||
-rw-r--r-- | activesupport/lib/active_support/inflector.rb | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/time_with_zone.rb | 8 | ||||
-rw-r--r-- | activesupport/lib/active_support/values/time_zone.rb | 8 | ||||
-rw-r--r-- | activesupport/test/core_ext/date_ext_test.rb | 23 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_with_zone_test.rb | 10 | ||||
-rw-r--r-- | activesupport/test/deprecation_test.rb | 10 | ||||
-rw-r--r-- | activesupport/test/time_zone_test.rb | 25 |
11 files changed, 135 insertions, 24 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 3ee9ed925c..f72825731e 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,13 @@ *SVN* +* Adding Date.current, which returns Time.zone.today if config.time_zone is set; otherwise returns Date.today [Geoff Buesing] + +* TimeWithZone: date part getter methods (#year #mon #day etc) are defined on class; no longer relying on method_missing [Geoff Buesing] + +* Time.zone.parse return nil for strings with no date information [Geoff Buesing] + +* Time.zone.parse respects offset information in string. Resolves #105. [Scott Fleckenstein, Geoff Buesing] + * Added Ruby 1.8 implementation of Process.daemon * Duration #since and #ago with no argument (e.g., 5.days.ago) return TimeWithZone when config.time_zone is set. Introducing Time.current, which returns Time.zone.now if config.time_zone is set, otherwise just returns Time.now [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/date/calculations.rb b/activesupport/lib/active_support/core_ext/date/calculations.rb index 183471706b..1e2dbf118e 100644 --- a/activesupport/lib/active_support/core_ext/date/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date/calculations.rb @@ -25,6 +25,11 @@ module ActiveSupport #:nodoc: def tomorrow ::Date.today.tomorrow end + + # Returns Time.zone.today when config.time_zone is set, otherwise just returns Date.today. + def current + ::Time.zone_default ? ::Time.zone.today : ::Date.today + end end # Converts Date to a Time (or DateTime if necessary) with the time portion set to the beginning of the day (0:00) diff --git a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb index 2213b09144..c96c5160b3 100644 --- a/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb +++ b/activesupport/lib/active_support/core_ext/hash/indifferent_access.rb @@ -23,10 +23,7 @@ class HashWithIndifferentAccess < Hash alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) alias_method :regular_update, :update unless method_defined?(:regular_update) - # - # Assigns a new value to the hash. - # - # Example: + # Assigns a new value to the hash: # # hash = HashWithIndifferentAccess.new # hash[:key] = "value" @@ -35,25 +32,15 @@ class HashWithIndifferentAccess < Hash regular_writer(convert_key(key), convert_value(value)) end + # Updates the instantized hash with values from the second: # - # Updates the instantized hash with values from the second. - # - # Example: - # - # >> hash_1 = HashWithIndifferentAccess.new - # => {} - # - # >> hash_1[:key] = "value" - # => "value" - # - # >> hash_2 = HashWithIndifferentAccess.new - # => {} + # hash_1 = HashWithIndifferentAccess.new + # hash_1[:key] = "value" # - # >> hash_2[:key] = "New Value!" - # => "New Value!" + # hash_2 = HashWithIndifferentAccess.new + # hash_2[:key] = "New Value!" # - # >> hash_1.update(hash_2) - # => {"key"=>"New Value!"} + # hash_1.update(hash_2) # => {"key"=>"New Value!"} # def update(other_hash) other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } @@ -62,7 +49,13 @@ class HashWithIndifferentAccess < Hash alias_method :merge!, :update - # Checks the hash for a key matching the argument passed in + # Checks the hash for a key matching the argument passed in: + # + # hash = HashWithIndifferentAccess.new + # hash["key"] = "value" + # hash.key? :key # => true + # hash.key? "key" # => true + # def key?(key) super(convert_key(key)) end @@ -76,7 +69,13 @@ class HashWithIndifferentAccess < Hash super(convert_key(key), *extras) end - # Returns an array of the values at the specified indicies. + # Returns an array of the values at the specified indices: + # + # hash = HashWithIndifferentAccess.new + # hash[:a] = "x" + # hash[:b] = "y" + # hash.values_at("a", "b") # => ["x", "y"] + # def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb index c9f1884da3..7613652c71 100644 --- a/activesupport/lib/active_support/deprecation.rb +++ b/activesupport/lib/active_support/deprecation.rb @@ -175,6 +175,20 @@ module ActiveSupport ActiveSupport::Deprecation.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack) end end + + class DeprecatedInstanceVariable < Delegator #:nodoc: + def initialize(value, method) + super(value) + @method = method + @value = value + end + + def __getobj__ + ActiveSupport::Deprecation.warn("Instance variable @#{@method} is deprecated! Call instance method #{@method} instead.") + @value + end + end + end end diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index c8736549f4..68fbf3da35 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -3,6 +3,11 @@ require 'singleton' # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without, # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept # in inflections.rb. +# +# The Rails core team has stated patches for the inflections library will not be accepted +# in order to avoid breaking legacy applications which may be relying on errant inflections. +# If you discover an incorrect inflection and require it for your application, you'll need +# to correct it yourself (explained below). module Inflector # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional # inflection rules. Examples: diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 461d52e40e..21ddcaad48 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -163,6 +163,14 @@ module ActiveSupport utc.advance(options).in_time_zone(time_zone) end + %w(year mon month day mday hour min sec).each do |method_name| + class_eval <<-EOV + def #{method_name} + time.#{method_name} + end + EOV + end + def usec time.respond_to?(:usec) ? time.usec : 0 end diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 9cdc2a74ed..0fa99135e2 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -213,8 +213,14 @@ class TimeZone # Time.zone.now # => Fri, 31 Dec 1999 14:00:00 HST -10:00 # Time.zone.parse('22:30:00') # => Fri, 31 Dec 1999 22:30:00 HST -10:00 def parse(str, now=now) + date_parts = Date._parse(str) + return if date_parts.blank? time = Time.parse(str, now) rescue DateTime.parse(str) - ActiveSupport::TimeWithZone.new(nil, self, time) + if date_parts[:offset].nil? + ActiveSupport::TimeWithZone.new(nil, self, time) + else + time.in_time_zone(self) + end end # Returns an ActiveSupport::TimeWithZone instance representing the current time diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 2e363c439a..5925ae3a61 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -208,6 +208,29 @@ class DateExtCalculationsTest < Test::Unit::TestCase end end end + + uses_mocha 'TestDateCurrent' do + def test_current_returns_date_today_when_zone_default_not_set + with_env_tz 'US/Central' do + Time.stubs(:now).returns Time.local(1999, 12, 31, 23) + assert_equal Date.new(1999, 12, 31), Date.today + assert_equal Date.new(1999, 12, 31), Date.current + end + end + + def test_current_returns_time_zone_today_when_zone_default_set + silence_warnings do # silence warnings raised by tzinfo gem + Time.zone_default = TimeZone['Eastern Time (US & Canada)'] + with_env_tz 'US/Central' do + Time.stubs(:now).returns Time.local(1999, 12, 31, 23) + assert_equal Date.new(1999, 12, 31), Date.today + assert_equal Date.new(2000, 1, 1), Date.current + end + end + ensure + Time.zone_default = nil + end + end protected def with_env_tz(new_tz = 'US/Eastern') diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index df70e82c1d..64fcb4af09 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -328,10 +328,18 @@ class TimeWithZoneTest < Test::Unit::TestCase assert_equal Time.utc(1999, 12, 31, 19), mtime.time end end - + def test_method_missing_with_non_time_return_value silence_warnings do # silence warnings raised by tzinfo gem + @twz.time.expects(:foo).returns('bar') + assert_equal 'bar', @twz.foo + end + end + + def test_date_part_value_methods + silence_warnings do # silence warnings raised by tzinfo gem twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) + twz.stubs(:method_missing).returns(nil) #ensure these methods are defined directly on class assert_equal 1999, twz.year assert_equal 12, twz.month assert_equal 31, twz.day diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index ebfa405947..11357e250f 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -149,3 +149,13 @@ class DeprecationTest < Test::Unit::TestCase assert_nil @last_message end end + +class DeprecatedIvarTest < Test::Unit::TestCase + + def test_deprecated_ivar + @action = ActiveSupport::Deprecation::DeprecatedInstanceVariable.new("fubar", :foobar) + + assert_deprecated(/Instance variable @foobar is deprecated! Call instance method foobar instead/) { assert_equal "fubar", @action } + end + +end diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index 2f06c347a1..3757ddcedb 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -173,6 +173,14 @@ class TimeZoneTest < Test::Unit::TestCase assert_equal zone, twz.time_zone end + def test_parse_string_with_timezone + (-11..13).each do |timezone_offset| + zone = TimeZone[timezone_offset] + twz = zone.parse('1999-12-31 19:00:00') + assert_equal twz, zone.parse(twz.to_s) + end + end + def test_parse_with_old_date silence_warnings do # silence warnings raised by tzinfo gem zone = TimeZone['Eastern Time (US & Canada)'] @@ -181,6 +189,23 @@ class TimeZoneTest < Test::Unit::TestCase assert_equal zone, twz.time_zone end end + + def test_parse_far_future_date_with_time_zone_offset_in_string + silence_warnings do # silence warnings raised by tzinfo gem + zone = TimeZone['Eastern Time (US & Canada)'] + twz = zone.parse('2050-12-31 19:00:00 -10:00') # i.e., 2050-01-01 05:00:00 UTC + assert_equal [0,0,0,1,1,2051], twz.to_a[0,6] + assert_equal zone, twz.time_zone + end + end + + def test_parse_returns_nil_when_string_without_date_information_is_passed_in + silence_warnings do # silence warnings raised by tzinfo gem + zone = TimeZone['Eastern Time (US & Canada)'] + assert_nil zone.parse('foobar') + assert_nil zone.parse(' ') + end + end uses_mocha 'TestParseWithIncompleteDate' do def test_parse_with_incomplete_date |