diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 3 | ||||
-rw-r--r-- | activesupport/activesupport.gemspec | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/cache.rb | 4 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/float/rounding.rb | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/deprecation/behaviors.rb | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/duration.rb | 6 | ||||
-rw-r--r-- | activesupport/lib/active_support/json/encoding.rb | 3 | ||||
-rw-r--r-- | activesupport/lib/active_support/notifications.rb | 14 | ||||
-rw-r--r-- | activesupport/lib/active_support/notifications/instrumenter.rb | 8 | ||||
-rw-r--r-- | activesupport/lib/active_support/time_with_zone.rb | 7 | ||||
-rw-r--r-- | activesupport/test/abstract_unit.rb | 1 | ||||
-rw-r--r-- | activesupport/test/core_ext/duration_test.rb | 25 | ||||
-rw-r--r-- | activesupport/test/notifications_test.rb | 40 |
14 files changed, 85 insertions, 37 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 9b0a84678a..87ec6f2a2c 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,8 @@ *Edge* +* Changed the default ActiveSupport.use_standard_json_time_format from false to true and +ActiveSupport.escape_html_entities_in_json from true to false to match previously announced Rails 3 defaults [DHH] + * Added Object#presence that returns the object if it's #present? otherwise returns nil [DHH/Colin Kelley] * Add Enumerable#exclude? to bring parity to Enumerable#include? and avoid if !x.include?/else calls [DHH] diff --git a/activesupport/activesupport.gemspec b/activesupport/activesupport.gemspec index 2d2cbf6448..8b9dd55b53 100644 --- a/activesupport/activesupport.gemspec +++ b/activesupport/activesupport.gemspec @@ -6,7 +6,7 @@ Gem::Specification.new do |s| s.summary = "Support and utility classes used by the Rails framework." s.description = %q{Utility library which carries commonly used classes and goodies from the Rails framework} - s.add_dependency('i18n', '>= 0.1.3') + s.add_dependency('i18n', '~> 0.3.0') s.files = Dir['CHANGELOG', 'README', 'lib/**/*'] s.require_path = 'lib' diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 3463000529..51ec87f329 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -66,6 +66,8 @@ module ActiveSupport autoload :StringInquirer autoload :XmlMini end + + autoload :TestCase end require 'active_support/vendor' diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb index ad238c1d96..6360a4614e 100644 --- a/activesupport/lib/active_support/cache.rb +++ b/activesupport/lib/active_support/cache.rb @@ -247,13 +247,13 @@ module ActiveSupport expires_in || 0 end - def instrument(operation, key, options, &block) + def instrument(operation, key, options) log(operation, key, options) if self.class.instrument payload = { :key => key } payload.merge!(options) if options.is_a?(Hash) - ActiveSupport::Notifications.instrument(:"cache_#{operation}", payload, &block) + ActiveSupport::Notifications.instrument("active_support.cache_#{operation}", payload){ yield } else yield end diff --git a/activesupport/lib/active_support/core_ext/float/rounding.rb b/activesupport/lib/active_support/core_ext/float/rounding.rb index 0b1ae4be7e..9bdf5bba7b 100644 --- a/activesupport/lib/active_support/core_ext/float/rounding.rb +++ b/activesupport/lib/active_support/core_ext/float/rounding.rb @@ -1,5 +1,6 @@ class Float - remove_method :round + alias precisionless_round round + private :precisionless_round # Rounds the float with the specified precision. # @@ -12,7 +13,7 @@ class Float magnitude = 10.0 ** precision (self * magnitude).round / magnitude else - super() + precisionless_round end end end diff --git a/activesupport/lib/active_support/deprecation/behaviors.rb b/activesupport/lib/active_support/deprecation/behaviors.rb index ca23d45666..578c025fcf 100644 --- a/activesupport/lib/active_support/deprecation/behaviors.rb +++ b/activesupport/lib/active_support/deprecation/behaviors.rb @@ -12,7 +12,7 @@ module ActiveSupport end def default_behavior - Deprecation::DEFAULT_BEHAVIORS[defined?(Rails) ? Rails.env.to_s : 'test'] + Deprecation::DEFAULT_BEHAVIORS[defined?(Rails.env) ? Rails.env.to_s : 'test'] end end diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index c1f0e4bf81..db5afb5324 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -36,7 +36,7 @@ module ActiveSupport end def is_a?(klass) #:nodoc: - klass == Duration || super + Duration == klass || value.is_a?(klass) end # Returns true if <tt>other</tt> is also a Duration instance with the @@ -50,7 +50,9 @@ module ActiveSupport end def self.===(other) #:nodoc: - other.is_a?(Duration) rescue super + other.is_a?(Duration) + rescue ::NoMethodError + false end # Calculates a new Time or Date that is as far in the future diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index c8415d5449..8ba45f7ea2 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -114,7 +114,8 @@ module ActiveSupport end end - self.escape_html_entities_in_json = true + self.use_standard_json_time_format = true + self.escape_html_entities_in_json = false end CircularReferenceError = Deprecation::DeprecatedConstantProxy.new('ActiveSupport::JSON::CircularReferenceError', Encoding::CircularReferenceError) diff --git a/activesupport/lib/active_support/notifications.rb b/activesupport/lib/active_support/notifications.rb index d9bfcbfcab..3e96decb8c 100644 --- a/activesupport/lib/active_support/notifications.rb +++ b/activesupport/lib/active_support/notifications.rb @@ -44,11 +44,16 @@ module ActiveSupport class << self attr_writer :notifier - delegate :publish, :subscribe, :instrument, :to => :notifier + delegate :publish, :subscribe, :to => :notifier + delegate :instrument, :to => :instrumenter def notifier @notifier ||= Notifier.new end + + def instrumenter + Thread.current[:"instrumentation_#{notifier.object_id}"] ||= Instrumenter.new(notifier) + end end class Notifier @@ -67,13 +72,6 @@ module ActiveSupport def wait @queue.wait end - - delegate :instrument, :to => :current_instrumenter - - private - def current_instrumenter - Thread.current[:"instrumentation_#{object_id}"] ||= Notifications::Instrumenter.new(self) - end end end end diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb index 0655dd0cb6..f3d877efe7 100644 --- a/activesupport/lib/active_support/notifications/instrumenter.rb +++ b/activesupport/lib/active_support/notifications/instrumenter.rb @@ -4,16 +4,20 @@ require 'active_support/core_ext/module/delegation' module ActiveSupport module Notifications class Instrumenter + attr_reader :id + def initialize(notifier) @id = unique_id @notifier = notifier end + # Instrument the given block by measuring the time taken to execute it + # and publish it. def instrument(name, payload={}) time = Time.now - yield if block_given? - ensure + result = yield(payload) if block_given? @notifier.publish(name, time, Time.now, @id, payload) + result end private diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 6b554e7158..710dce78de 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -32,7 +32,6 @@ module ActiveSupport # t.is_a?(Time) # => true # t.is_a?(ActiveSupport::TimeWithZone) # => true class TimeWithZone - def self.name 'Time' # Report class name as 'Time' to thwart type checking end @@ -114,9 +113,9 @@ module ActiveSupport end alias_method :iso8601, :xmlschema - # Coerces the date to a string for JSON encoding. - # - # ISO 8601 format is used if ActiveSupport::JSON::Encoding.use_standard_json_time_format is set. + # Coerces the date to a string for JSON encoding. The default format is ISO 8601. You can get + # %Y/%m/%d %H:%M:%S +offset style by setting ActiveSupport::JSON::Encoding.use_standard_json_time_format + # to false. # # ==== Examples # diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index dda139372e..d91e0415c4 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -13,7 +13,6 @@ require 'mocha' ENV['NO_RELOAD'] = '1' require 'active_support' -require 'active_support/test_case' # Include shims until we get off 1.8.6 require 'active_support/ruby/shim' diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 42b4f10172..6530de1ef4 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -2,6 +2,31 @@ require 'abstract_unit' require 'active_support/time' class DurationTest < ActiveSupport::TestCase + def test_is_a + d = 1.day + assert d.is_a?(ActiveSupport::Duration) + assert d.is_a?(Numeric) + assert d.is_a?(Fixnum) + assert !d.is_a?(Hash) + + k = Class.new + class << k; undef_method :== end + assert !d.is_a?(k) + end + + def test_threequals + assert ActiveSupport::Duration === 1.day + assert !(ActiveSupport::Duration === 1.day.to_i) + assert !(ActiveSupport::Duration === 'foo') + assert !(ActiveSupport::Duration === ActiveSupport::BasicObject.new) + end + + def test_equals + assert 1.day == 1.day + assert 1.day == 1.day.to_i + assert !(1.day == 'foo') + end + def test_inspect assert_equal '0 seconds', 0.seconds.inspect assert_equal '1 month', 1.month.inspect diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 3ba77ae135..c3eb1a4eb5 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -5,7 +5,8 @@ module Notifications def setup Thread.abort_on_exception = true - @notifier = ActiveSupport::Notifications::Notifier.new + ActiveSupport::Notifications.notifier = nil + @notifier = ActiveSupport::Notifications.notifier @events = [] @notifier.subscribe { |*args| @events << event(*args) } end @@ -82,13 +83,29 @@ module Notifications end class InstrumentationTest < TestCase + delegate :instrument, :instrument!, :to => ActiveSupport::Notifications + def test_instrument_returns_block_result - assert_equal 2, @notifier.instrument(:awesome) { 1 + 1 } + assert_equal 2, instrument(:awesome) { 1 + 1 } + drain + end + + def test_instrument_yields_the_paylod_for_further_modification + assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 } + drain + + assert_equal 1, @events.size + assert_equal :awesome, @events.first.name + assert_equal Hash[:result => 2], @events.first.payload + end + + def test_instrumenter_exposes_its_id + assert_equal 20, ActiveSupport::Notifications.instrumenter.id.size end def test_nested_events_can_be_instrumented - @notifier.instrument(:awesome, :payload => "notifications") do - @notifier.instrument(:wot, :payload => "child") do + instrument(:awesome, :payload => "notifications") do + instrument(:wot, :payload => "child") do 1 + 1 end @@ -106,24 +123,21 @@ module Notifications assert_equal Hash[:payload => "notifications"], @events.last.payload end - def test_instrument_publishes_when_exception_is_raised + def test_instrument_does_not_publish_when_exception_is_raised begin - @notifier.instrument(:awesome, :payload => "notifications") do + instrument(:awesome, :payload => "notifications") do raise "OMG" end - flunk - rescue + rescue RuntimeError => e + assert_equal "OMG", e.message end drain - - assert_equal 1, @events.size - assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "notifications"], @events.last.payload + assert_equal 0, @events.size end def test_event_is_pushed_even_without_block - @notifier.instrument(:awesome, :payload => "notifications") + instrument(:awesome, :payload => "notifications") drain assert_equal 1, @events.size |