From 0289b0e7dff1c114a090f905406cc212d9e3570e Mon Sep 17 00:00:00 2001 From: gbuesing Date: Sat, 12 Apr 2008 17:31:29 -0500 Subject: Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing --- activesupport/CHANGELOG | 2 ++ activesupport/lib/active_support/time_with_zone.rb | 27 ++++++++++++++-------- 2 files changed, 20 insertions(+), 9 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 0627c89c53..c28d630f5d 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing [Geoff Buesing] + * TimeWithZone respects config.active_support.use_standard_json_time_format [Geoff Buesing] * Add config.active_support.escape_html_entities_in_json to allow disabling of html entity escaping. [rick] diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 1c984f07d4..f1a2498298 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -134,7 +134,8 @@ module ActiveSupport # If wrapped #time is a DateTime, use DateTime#since instead of #+ # Otherwise, just pass on to #method_missing def +(other) - time.acts_like?(:date) ? method_missing(:since, other) : method_missing(:+, other) + result = utc.acts_like?(:date) ? utc.since(other) : utc + other + result.in_time_zone(time_zone) end # If a time-like object is passed in, compare it with #utc @@ -144,10 +145,23 @@ module ActiveSupport if other.acts_like?(:time) utc - other else - time.acts_like?(:date) ? method_missing(:ago, other) : method_missing(:-, other) + result = utc.acts_like?(:date) ? utc.ago(other) : utc - other + result.in_time_zone(time_zone) end end + def since(other) + utc.since(other).in_time_zone(time_zone) + end + + def ago(other) + utc.ago(other).in_time_zone(time_zone) + end + + def advance(options) + utc.advance(options).in_time_zone(time_zone) + end + def usec time.respond_to?(:usec) ? time.usec : 0 end @@ -208,13 +222,8 @@ module ActiveSupport # Send the missing method to time instance, and wrap result in a new TimeWithZone with the existing time_zone def method_missing(sym, *args, &block) - if %w(+ - since ago advance).include?(sym.to_s) - result = utc.__send__(sym, *args, &block) - result.acts_like?(:time) ? result.in_time_zone(time_zone) : result - else - result = time.__send__(sym, *args, &block) - result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result - end + result = time.__send__(sym, *args, &block) + result.acts_like?(:time) ? self.class.new(nil, time_zone, result) : result end private -- cgit v1.2.3 From f285b6119b9ca7f598e31c0c8518dce3e1b13386 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Sat, 12 Apr 2008 17:37:50 -0500 Subject: Add #getutc alias for DateTime#utc --- activesupport/CHANGELOG | 2 ++ activesupport/lib/active_support/core_ext/date_time/calculations.rb | 1 + activesupport/test/core_ext/date_time_ext_test.rb | 1 + 3 files changed, 4 insertions(+) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index c28d630f5d..2c5042c9ef 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Add #getutc alias for DateTime#utc [Geoff Buesing] + * Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing [Geoff Buesing] * TimeWithZone respects config.active_support.use_standard_json_time_format [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/date_time/calculations.rb b/activesupport/lib/active_support/core_ext/date_time/calculations.rb index 5c351c21c6..fa444f71b1 100644 --- a/activesupport/lib/active_support/core_ext/date_time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/date_time/calculations.rb @@ -86,6 +86,7 @@ module ActiveSupport #:nodoc: def utc new_offset(0) end + alias_method :getutc, :utc # Returns true if offset == 0 def utc? diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index 25a8e878f9..ed45daf403 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -240,6 +240,7 @@ class DateTimeExtCalculationsTest < Test::Unit::TestCase assert_equal DateTime.civil(2005, 2, 21, 15, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(-5, 24)).utc assert_equal DateTime.civil(2005, 2, 21, 10, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, 0).utc assert_equal DateTime.civil(2005, 2, 21, 9, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(1, 24)).utc + assert_equal DateTime.civil(2005, 2, 21, 9, 11, 12, 0), DateTime.civil(2005, 2, 21, 10, 11, 12, Rational(1, 24)).getutc end def test_formatted_offset_with_utc -- cgit v1.2.3 From 9620372a6dc7eeebdb04f1fdb7f150d29e60fc00 Mon Sep 17 00:00:00 2001 From: gbuesing Date: Sat, 12 Apr 2008 19:35:47 -0500 Subject: Time#since behaves correctly when passed a Duration. Closes #11527 [kemiller] --- activesupport/CHANGELOG | 2 + .../active_support/core_ext/time/calculations.rb | 13 +++-- activesupport/test/core_ext/time_ext_test.rb | 55 +++++++++++++++++++++- 3 files changed, 63 insertions(+), 7 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 2c5042c9ef..cfdefed91e 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Time#since behaves correctly when passed a Duration. Closes #11527 [kemiller] + * Add #getutc alias for DateTime#utc [Geoff Buesing] * Refactor TimeWithZone: don't send #since, #ago, #+, #-, #advance through method_missing [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 0bc83af709..ffbdf37789 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -88,18 +88,21 @@ module ActiveSupport #:nodoc: end # Returns a new Time representing the time a number of seconds ago, this is basically a wrapper around the Numeric extension - # Do not use this method in combination with x.months, use months_ago instead! def ago(seconds) self.since(-seconds) end # Returns a new Time representing the time a number of seconds since the instance time, this is basically a wrapper around - # the Numeric extension. Do not use this method in combination with x.months, use months_since instead! + # the Numeric extension. def since(seconds) - initial_dst = self.dst? ? 1 : 0 f = seconds.since(self) - final_dst = f.dst? ? 1 : 0 - (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f + if ActiveSupport::Duration === seconds + f + else + initial_dst = self.dst? ? 1 : 0 + final_dst = f.dst? ? 1 : 0 + (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f + end rescue self.to_datetime.since(seconds) end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index d1d92964c3..e53b7193ea 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -205,6 +205,31 @@ class TimeExtCalculationsTest < Test::Unit::TestCase end end + def test_daylight_savings_time_crossings_backward_start_1day + with_env_tz 'US/Eastern' do + # dt: US: 2005 April 3rd 4:18am + assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(1.day), 'dt-1.day=>st' + assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(1.day), 'st-1.day=>st' + end + with_env_tz 'NZ' do + # dt: New Zealand: 2006 October 1st 4:18am + assert_equal Time.local(2006,9,30,4,18,0), Time.local(2006,10,1,4,18,0).ago(1.day), 'dt-1.day=>st' + assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(1.day), 'st-1.day=>st' + end + end + + def test_daylight_savings_time_crossings_backward_end_1day + with_env_tz 'US/Eastern' do + # st: US: 2005 October 30th 4:03am + assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(1.day), 'st-1.day=>dt' + assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(1.day), 'dt-1.day=>dt' + end + with_env_tz 'NZ' do + # st: New Zealand: 2006 March 19th 4:03am + assert_equal Time.local(2006,3,18,4,3), Time.local(2006,3,19,4,3,0).ago(1.day), 'st-1.day=>dt' + assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(1.day), 'dt-1.day=>dt' + end + end def test_since assert_equal Time.local(2005,2,22,10,10,11), Time.local(2005,2,22,10,10,10).since(1) assert_equal Time.local(2005,2,22,11,10,10), Time.local(2005,2,22,10,10,10).since(3600) @@ -227,6 +252,19 @@ class TimeExtCalculationsTest < Test::Unit::TestCase end end + def test_daylight_savings_time_crossings_forward_start_1day + with_env_tz 'US/Eastern' do + # st: US: 2005 April 2nd 7:27pm + assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(1.day), 'st+1.day=>dt' + assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(1.day), 'dt+1.day=>dt' + end + with_env_tz 'NZ' do + # st: New Zealand: 2006 September 30th 7:27pm + assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,9,30,19,27,0).since(1.day), 'st+1.day=>dt' + assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(1.day), 'dt+1.day=>dt' + end + end + def test_daylight_savings_time_crossings_forward_start_tomorrow with_env_tz 'US/Eastern' do # st: US: 2005 April 2nd 7:27pm @@ -240,7 +278,7 @@ class TimeExtCalculationsTest < Test::Unit::TestCase end end - def test_daylight_savings_time_crossings_forward_start_yesterday + def test_daylight_savings_time_crossings_backward_start_yesterday with_env_tz 'US/Eastern' do # st: US: 2005 April 2nd 7:27pm assert_equal Time.local(2005,4,2,19,27,0), Time.local(2005,4,3,19,27,0).yesterday, 'dt-1.day=>st' @@ -266,6 +304,19 @@ class TimeExtCalculationsTest < Test::Unit::TestCase end end + def test_daylight_savings_time_crossings_forward_end_1day + with_env_tz 'US/Eastern' do + # dt: US: 2005 October 30th 12:45am + assert_equal Time.local(2005,10,31,0,45,0), Time.local(2005,10,30,0,45,0).since(1.day), 'dt+1.day=>st' + assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(1.day), 'st+1.day=>st' + end + with_env_tz 'NZ' do + # dt: New Zealand: 2006 March 19th 1:45am + assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,19,1,45,0).since(1.day), 'dt+1.day=>st' + assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(1.day), 'st+1.day=>st' + end + end + def test_daylight_savings_time_crossings_forward_end_tomorrow with_env_tz 'US/Eastern' do # dt: US: 2005 October 30th 12:45am @@ -279,7 +330,7 @@ class TimeExtCalculationsTest < Test::Unit::TestCase end end - def test_daylight_savings_time_crossings_forward_end_yesterday + def test_daylight_savings_time_crossings_backward_end_yesterday with_env_tz 'US/Eastern' do # dt: US: 2005 October 30th 12:45am assert_equal Time.local(2005,10,30,0,45,0), Time.local(2005,10,31,0,45,0).yesterday, 'st-1.day=>dt' -- cgit v1.2.3 From 9e1d506a8cfedef2fdd605e4cbf4bf53651ad214 Mon Sep 17 00:00:00 2001 From: jweiss Date: Thu, 17 Apr 2008 12:58:31 -0500 Subject: Support options passed to ActiveSupport::Cache :mem_cache_store [#14 state:resolved] Signed-off-by: Joshua Peek --- activesupport/lib/active_support/cache/mem_cache_store.rb | 3 ++- activesupport/test/caching_test.rb | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb index 88e324aa3a..9b787702b2 100644 --- a/activesupport/lib/active_support/cache/mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/mem_cache_store.rb @@ -15,9 +15,10 @@ module ActiveSupport def initialize(*addresses) addresses = addresses.flatten + options = addresses.extract_options! addresses = ["localhost"] if addresses.empty? @addresses = addresses - @data = MemCache.new(addresses) + @data = MemCache.new(addresses, options) end def read(key, options = nil) diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index a038f29f53..09b56525e0 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -18,6 +18,19 @@ class CacheStoreSettingTest < Test::Unit::TestCase assert_kind_of(ActiveSupport::Cache::MemCacheStore, store) assert_equal %w(localhost), store.addresses end + + def test_mem_cache_fragment_cache_store_with_multiple_servers + store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1' + assert_kind_of(ActiveSupport::Cache::MemCacheStore, store) + assert_equal %w(localhost 192.168.1.1), store.addresses + end + + def test_mem_cache_fragment_cache_store_with_options + store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo' + assert_kind_of(ActiveSupport::Cache::MemCacheStore, store) + assert_equal %w(localhost 192.168.1.1), store.addresses + assert_equal 'foo', store.instance_variable_get('@data').instance_variable_get('@namespace') + end def test_object_assigned_fragment_cache_store store = ActiveSupport::Cache.lookup_store ActiveSupport::Cache::FileStore.new("/path/to/cache/directory") -- cgit v1.2.3 From cf04e621270bb2e5e9e7971d2c59e73d6797482d Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Thu, 17 Apr 2008 23:30:01 -0500 Subject: Tidy up ActiveSupport::Callbacks::CallbackChain instance API. --- activesupport/lib/active_support/callbacks.rb | 25 ++++++++++++++------- activesupport/test/callbacks_test.rb | 31 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 329cc2fdc7..282b6cbcbc 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -96,17 +96,26 @@ module ActiveSupport end end - def find_callback(callback, &block) + def |(chain) + if chain.is_a?(Callback) + if found_callback = find(chain) + index = index(found_callback) + self[index] = chain + else + self << chain + end + else + chain.each { |callback| self | callback } + end + self + end + + def find(callback, &block) select { |c| c == callback && (!block_given? || yield(c)) }.first end - def replace_or_append_callback(callback) - if found_callback = find_callback(callback) - index = index(found_callback) - self[index] = callback - else - self << callback - end + def delete(callback) + super(find(callback)) end private diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb index 3f8cb7f01a..7f71ca2262 100644 --- a/activesupport/test/callbacks_test.rb +++ b/activesupport/test/callbacks_test.rb @@ -96,6 +96,8 @@ class ConditionalCallbackTest < Test::Unit::TestCase end class CallbackTest < Test::Unit::TestCase + include ActiveSupport::Callbacks + def test_eql callback = Callback.new(:before, :save, :identifier => :lifesaver) assert callback.eql?(Callback.new(:before, :save, :identifier => :lifesaver)) @@ -115,3 +117,32 @@ class CallbackTest < Test::Unit::TestCase assert_equal({}, a.options) end end + +class CallbackChainTest < Test::Unit::TestCase + include ActiveSupport::Callbacks + + def setup + @chain = CallbackChain.build(:make, :bacon, :lettuce, :tomato) + end + + def test_build + assert_equal 3, @chain.size + assert_equal [:bacon, :lettuce, :tomato], @chain.map(&:method) + end + + def test_find + assert_equal :bacon, @chain.find(:bacon).method + end + + def test_union + assert_equal [:bacon, :lettuce, :tomato], (@chain | Callback.new(:make, :bacon)).map(&:method) + assert_equal [:bacon, :lettuce, :tomato, :turkey], (@chain | CallbackChain.build(:make, :bacon, :lettuce, :tomato, :turkey)).map(&:method) + assert_equal [:bacon, :lettuce, :tomato, :turkey, :mayo], (@chain | Callback.new(:make, :mayo)).map(&:method) + end + + def test_delete + assert_equal [:bacon, :lettuce, :tomato], @chain.map(&:method) + @chain.delete(:bacon) + assert_equal [:lettuce, :tomato], @chain.map(&:method) + end +end -- cgit v1.2.3 From a4c15303cbc96fffd66c68abdeebe73d8883e5ab Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Fri, 18 Apr 2008 14:44:55 -0500 Subject: Slight optimization to CallbackChain#union and delete. --- activesupport/lib/active_support/callbacks.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 282b6cbcbc..5c51237049 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -97,15 +97,14 @@ module ActiveSupport end def |(chain) - if chain.is_a?(Callback) - if found_callback = find(chain) - index = index(found_callback) + if chain.is_a?(CallbackChain) + chain.each { |callback| self | callback } + elsif chain.is_a?(Callback) + if index = index(chain) self[index] = chain else self << chain end - else - chain.each { |callback| self | callback } end self end @@ -115,7 +114,7 @@ module ActiveSupport end def delete(callback) - super(find(callback)) + super(callback.is_a?(Callback) ? callback : find(callback)) end private -- cgit v1.2.3 From 46ab7422d9ebac0d529f71a3a7c2feaf0b9d5dbd Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 20 Apr 2008 11:45:44 -0500 Subject: Use define_callbacks helper for ActiveRecord validations. --- activesupport/lib/active_support/callbacks.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 5c51237049..9c59b7ac76 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -99,8 +99,8 @@ module ActiveSupport def |(chain) if chain.is_a?(CallbackChain) chain.each { |callback| self | callback } - elsif chain.is_a?(Callback) - if index = index(chain) + else + if (found_callback = find(chain)) && (index = index(chain)) self[index] = chain else self << chain @@ -224,8 +224,8 @@ module ActiveSupport end end - # Runs all the callbacks defined for the given options. - # + # Runs all the callbacks defined for the given options. + # # If a block is given it will be called after each callback receiving as arguments: # # * the result from the callback @@ -236,31 +236,31 @@ module ActiveSupport # Example: # class Storage # include ActiveSupport::Callbacks - # + # # define_callbacks :before_save, :after_save # end - # + # # class ConfigStorage < Storage # before_save :pass # before_save :pass # before_save :stop # before_save :pass - # + # # def pass # puts "pass" # end - # + # # def stop # puts "stop" # return false # end - # + # # def save # result = run_callbacks(:before_save) { |result, object| result == false } # puts "- save" if result # end # end - # + # # config = ConfigStorage.new # config.save # -- cgit v1.2.3