diff options
author | Xavier Noria <fxn@hashref.com> | 2010-03-31 07:47:58 -0700 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-03-31 07:47:58 -0700 |
commit | 824fa10f4d1306cc1e310a7d5de7e95cfb07d6f8 (patch) | |
tree | 1967c2a945d6e131d467fb4f0178a0f60e01ae8a /activesupport | |
parent | 1ed1652bef981d9402797b6cb9f0920a40eea21a (diff) | |
parent | db28d407f76a790a31e27bf51560e23425dd6944 (diff) | |
download | rails-824fa10f4d1306cc1e310a7d5de7e95cfb07d6f8.tar.gz rails-824fa10f4d1306cc1e310a7d5de7e95cfb07d6f8.tar.bz2 rails-824fa10f4d1306cc1e310a7d5de7e95cfb07d6f8.zip |
Merge commit 'rails/master'
Diffstat (limited to 'activesupport')
23 files changed, 167 insertions, 79 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 56c81cf63b..3822abcab6 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *Rails 3.0 (pending)* +* New assertions assert_blank and assert_present. #4299 [Juanjo Bazan] + * Use Object#singleton_class instead of #metaclass. Prefer Ruby's choice. [Jeremy Kemper] * JSON backend for YAJL. Preferred if available. #2666 [Brian Lopez] diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb index d584c9e254..d84a62ca2d 100644 --- a/activesupport/lib/active_support/cache/mem_cache_store.rb +++ b/activesupport/lib/active_support/cache/mem_cache_store.rb @@ -91,7 +91,7 @@ module ActiveSupport def delete(key, options = nil) # :nodoc: super do - response = @data.delete(key, expires_in(options)) + response = @data.delete(key) response == Response::DELETED end rescue MemCache::MemCacheError => e diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb index e4d429fc2b..045a6944fa 100644 --- a/activesupport/lib/active_support/core_ext/hash/keys.rb +++ b/activesupport/lib/active_support/core_ext/hash/keys.rb @@ -22,7 +22,7 @@ class Hash # to +to_sym+. def symbolize_keys! keys.each do |key| - self[(key.to_sym rescue key)] = delete(key) if key.respond_to?(:to_sym) && !key.is_a?(Fixnum) + self[(key.to_sym rescue key) || key] = delete(key) end self end diff --git a/activesupport/lib/active_support/core_ext/time/marshal.rb b/activesupport/lib/active_support/core_ext/time/marshal.rb new file mode 100644 index 0000000000..1a4d918ce7 --- /dev/null +++ b/activesupport/lib/active_support/core_ext/time/marshal.rb @@ -0,0 +1,56 @@ +# Pre-1.9 versions of Ruby have a bug with marshaling Time instances, where utc instances are +# unmarshalled in the local zone, instead of utc. We're layering behavior on the _dump and _load +# methods so that utc instances can be flagged on dump, and coerced back to utc on load. +if !Marshal.load(Marshal.dump(Time.now.utc)).utc? + class Time + class << self + alias_method :_load_without_utc_flag, :_load + def _load(marshaled_time) + time = _load_without_utc_flag(marshaled_time) + time.instance_eval do + if defined?(@marshal_with_utc_coercion) + val = remove_instance_variable("@marshal_with_utc_coercion") + end + val ? utc : self + end + end + end + + alias_method :_dump_without_utc_flag, :_dump + def _dump(*args) + obj = dup + obj.instance_variable_set('@marshal_with_utc_coercion', utc?) + obj._dump_without_utc_flag(*args) + end + end +end + +# Ruby 1.9.2 adds utc_offset and zone to Time, but marshaling only +# preserves utc_offset. Preserve zone also, even though it may not +# work in some edge cases. +if Time.local(2010).zone != Marshal.load(Marshal.dump(Time.local(2010))).zone + class Time + class << self + alias_method :_load_without_zone, :_load + def _load(marshaled_time) + time = _load_without_zone(marshaled_time) + time.instance_eval do + if zone = defined?(@_zone) && remove_instance_variable('@_zone') + ary = to_a + ary[-1] = zone + utc? ? Time.utc(*ary) : Time.local(*ary) + else + self + end + end + end + end + + alias_method :_dump_without_zone, :_dump + def _dump(*args) + obj = dup + obj.instance_variable_set('@_zone', zone) + obj._dump_without_zone(*args) + end + end +end diff --git a/activesupport/lib/active_support/core_ext/time/marshal_with_utc_flag.rb b/activesupport/lib/active_support/core_ext/time/marshal_with_utc_flag.rb deleted file mode 100644 index 8d46d80251..0000000000 --- a/activesupport/lib/active_support/core_ext/time/marshal_with_utc_flag.rb +++ /dev/null @@ -1,26 +0,0 @@ -# Pre-1.9 versions of Ruby have a bug with marshaling Time instances, where utc instances are -# unmarshalled in the local zone, instead of utc. We're layering behavior on the _dump and _load -# methods so that utc instances can be flagged on dump, and coerced back to utc on load. -if RUBY_VERSION < '1.9' - class Time - class << self - alias_method :_original_load, :_load - def _load(marshaled_time) - time = _original_load(marshaled_time) - time.instance_eval do - if defined?(@marshal_with_utc_coercion) - val = remove_instance_variable("@marshal_with_utc_coercion") - end - val ? utc : self - end - end - end - - alias_method :_original_dump, :_dump - def _dump(*args) - obj = dup - obj.instance_variable_set('@marshal_with_utc_coercion', utc?) - obj._original_dump(*args) - end - end -end diff --git a/activesupport/lib/active_support/dependencies/autoload.rb b/activesupport/lib/active_support/dependencies/autoload.rb index f669f4a77e..4c771da096 100644 --- a/activesupport/lib/active_support/dependencies/autoload.rb +++ b/activesupport/lib/active_support/dependencies/autoload.rb @@ -3,10 +3,6 @@ require "active_support/lazy_load_hooks" module ActiveSupport module Autoload - def self.extended(base) - base.extend(LazyLoadHooks) - end - @@autoloads = {} @@under_path = nil @@at_path = nil diff --git a/activesupport/lib/active_support/i18n.rb b/activesupport/lib/active_support/i18n.rb index 034d7d8ddc..11af48d67e 100644 --- a/activesupport/lib/active_support/i18n.rb +++ b/activesupport/lib/active_support/i18n.rb @@ -1,3 +1,3 @@ require 'i18n' I18n.load_path << "#{File.dirname(__FILE__)}/locale/en.yml" -ActiveSupport.run_base_hooks(:i18n)
\ No newline at end of file +ActiveSupport.run_load_hooks(:i18n)
\ No newline at end of file diff --git a/activesupport/lib/active_support/lazy_load_hooks.rb b/activesupport/lib/active_support/lazy_load_hooks.rb index 36acfda524..642a4c105c 100644 --- a/activesupport/lib/active_support/lazy_load_hooks.rb +++ b/activesupport/lib/active_support/lazy_load_hooks.rb @@ -1,25 +1,17 @@ module ActiveSupport - module LazyLoadHooks - def _setup_base_hooks - @base_hooks ||= Hash.new {|h,k| h[k] = [] } - @base ||= {} - end - - def base_hook(name = nil, &block) - _setup_base_hooks + @load_hooks = Hash.new {|h,k| h[k] = [] } + @loaded = {} - if base = @base[name] - base.instance_eval(&block) - else - @base_hooks[name] << block - end + def self.on_load(name, &block) + if base = @loaded[name] + base.instance_eval(&block) + else + @load_hooks[name] << block end + end - def run_base_hooks(base, name = nil) - _setup_base_hooks - - @base_hooks[name].each { |hook| base.instance_eval(&hook) } if @base_hooks - @base[name] = base - end + def self.run_load_hooks(name, base = Object) + @load_hooks[name].each { |hook| base.instance_eval(&hook) } + @loaded[name] = base end end
\ No newline at end of file diff --git a/activesupport/lib/active_support/railtie.rb b/activesupport/lib/active_support/railtie.rb index e45d16ee96..b8d54ff839 100644 --- a/activesupport/lib/active_support/railtie.rb +++ b/activesupport/lib/active_support/railtie.rb @@ -35,7 +35,7 @@ module I18n config.i18n.load_path = [] initializer "i18n.initialize" do - ActiveSupport.base_hook(:i18n) do + ActiveSupport.on_load(:i18n) do I18n.reload! ActionDispatch::Callbacks.to_prepare do diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb index c529b92240..33793f0688 100644 --- a/activesupport/lib/active_support/testing/assertions.rb +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/array/wrap' +require 'active_support/core_ext/object/blank' module ActiveSupport module Testing @@ -62,6 +63,20 @@ module ActiveSupport def assert_no_difference(expression, message = nil, &block) assert_difference expression, 0, message, &block end + + # Test if an expression is blank. Passes if object.blank? is true. + # + # assert_blank [] # => true + def assert_blank(object) + assert object.blank?, "#{object.inspect} is not blank" + end + + # Test if an expression is not blank. Passes if object.present? is true. + # + # assert_present {:data => 'x' } # => true + def assert_present(object) + assert object.present?, "#{object.inspect} is blank" + end end end end diff --git a/activesupport/lib/active_support/time.rb b/activesupport/lib/active_support/time.rb index 0f421421d0..784c7173a9 100644 --- a/activesupport/lib/active_support/time.rb +++ b/activesupport/lib/active_support/time.rb @@ -14,7 +14,7 @@ require 'date' require 'time' require 'active_support/core_ext/time/publicize_conversion_methods' -require 'active_support/core_ext/time/marshal_with_utc_flag' +require 'active_support/core_ext/time/marshal' require 'active_support/core_ext/time/acts_like' require 'active_support/core_ext/time/calculations' require 'active_support/core_ext/time/conversions' diff --git a/activesupport/lib/active_support/values/time_zone.rb b/activesupport/lib/active_support/values/time_zone.rb index 3cb4d89e02..945cdd5278 100644 --- a/activesupport/lib/active_support/values/time_zone.rb +++ b/activesupport/lib/active_support/values/time_zone.rb @@ -198,10 +198,12 @@ module ActiveSupport @name = name @utc_offset = utc_offset @tzinfo = tzinfo + @current_period = nil end def utc_offset - @utc_offset ||= tzinfo.current_period.utc_offset + @current_period ||= tzinfo.current_period + @current_period.utc_offset end # Returns the offset of this time zone as a formatted string, of the @@ -362,13 +364,14 @@ module ActiveSupport "Wellington" ], [ 46_800, "Nuku'alofa" ]]. each do |offset, *places| - places.sort.each do |place| + places.each do |place| place.freeze zone = new(place, offset) ZONES << zone ZONES_MAP[place] = zone end end + ZONES.sort! ZONES.freeze ZONES_MAP.freeze diff --git a/activesupport/lib/active_support/xml_mini/libxmlsax.rb b/activesupport/lib/active_support/xml_mini/libxmlsax.rb index d7b2f4c5be..fe2c1b9349 100644 --- a/activesupport/lib/active_support/xml_mini/libxmlsax.rb +++ b/activesupport/lib/active_support/xml_mini/libxmlsax.rb @@ -1,4 +1,5 @@ require 'libxml' +require 'active_support/core_ext/object/blank' # = XmlMini LibXML implementation using a SAX-based parser module ActiveSupport diff --git a/activesupport/lib/active_support/xml_mini/nokogirisax.rb b/activesupport/lib/active_support/xml_mini/nokogirisax.rb index d538a9110f..8af7b5e565 100644 --- a/activesupport/lib/active_support/xml_mini/nokogirisax.rb +++ b/activesupport/lib/active_support/xml_mini/nokogirisax.rb @@ -1,4 +1,5 @@ require 'nokogiri' +require 'active_support/core_ext/object/blank' # = XmlMini Nokogiri implementation using a SAX-based parser module ActiveSupport diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb index 3db48ce5a3..863d66a91d 100644 --- a/activesupport/lib/active_support/xml_mini/rexml.rb +++ b/activesupport/lib/active_support/xml_mini/rexml.rb @@ -27,7 +27,13 @@ module ActiveSupport data.ungetc(char) silence_warnings { require 'rexml/document' } unless defined?(REXML::Document) doc = REXML::Document.new(data) - merge_element!({}, doc.root) + + if doc.root + merge_element!({}, doc.root) + else + raise REXML::ParseException, + "The document #{doc.to_s.inspect} does not have a valid root" + end end end diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 67f652325e..ea6ebca3ce 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -10,6 +10,7 @@ $:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) require 'test/unit' require 'active_support/core_ext/kernel/reporting' +require 'empty_bool' silence_warnings { require 'mocha' } diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 00e05f76fe..d96f8e1de5 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -333,6 +333,12 @@ uses_memcached 'memcached backed store' do assert_equal 'bat', @cache.read('baz') assert_equal nil, @cache.read('foo') end + + def test_delete_should_only_pass_key_to_data + key = 'foo' + @data.expects(:delete).with(key) + @cache.delete(key) + end end class CompressedMemCacheStore < ActiveSupport::TestCase diff --git a/activesupport/test/core_ext/blank_test.rb b/activesupport/test/core_ext/blank_test.rb index ed6c625a0a..97c6b213ba 100644 --- a/activesupport/test/core_ext/blank_test.rb +++ b/activesupport/test/core_ext/blank_test.rb @@ -1,14 +1,6 @@ require 'abstract_unit' require 'active_support/core_ext/object/blank' -class EmptyTrue - def empty?() true; end -end - -class EmptyFalse - def empty?() false; end -end - class BlankTest < Test::Unit::TestCase BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", [], {} ] NOT = [ EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], { nil => 0 } ] diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 159b7d8366..c24c8619c6 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -758,33 +758,29 @@ end class TimeExtMarshalingTest < Test::Unit::TestCase def test_marshaling_with_utc_instance t = Time.utc(2000) - marshaled = Marshal.dump t - unmarshaled = Marshal.load marshaled - assert_equal t, unmarshaled + unmarshaled = Marshal.load(Marshal.dump(t)) assert_equal "UTC", unmarshaled.zone + assert_equal t, unmarshaled end def test_marshaling_with_local_instance t = Time.local(2000) - marshaled = Marshal.dump t - unmarshaled = Marshal.load marshaled - assert_equal t, unmarshaled + unmarshaled = Marshal.load(Marshal.dump(t)) assert_equal t.zone, unmarshaled.zone + assert_equal t, unmarshaled end def test_marshaling_with_frozen_utc_instance t = Time.utc(2000).freeze - marshaled = Marshal.dump t - unmarshaled = Marshal.load marshaled - assert_equal t, unmarshaled + unmarshaled = Marshal.load(Marshal.dump(t)) assert_equal "UTC", unmarshaled.zone + assert_equal t, unmarshaled end def test_marshaling_with_frozen_local_instance t = Time.local(2000).freeze - marshaled = Marshal.dump t - unmarshaled = Marshal.load marshaled - assert_equal t, unmarshaled + unmarshaled = Marshal.load(Marshal.dump(t)) assert_equal t.zone, unmarshaled.zone + assert_equal t, unmarshaled end end diff --git a/activesupport/test/empty_bool.rb b/activesupport/test/empty_bool.rb new file mode 100644 index 0000000000..005b3523ef --- /dev/null +++ b/activesupport/test/empty_bool.rb @@ -0,0 +1,7 @@ +class EmptyTrue + def empty?() true; end +end + +class EmptyFalse + def empty?() false; end +end diff --git a/activesupport/test/message_encryptor_test.rb b/activesupport/test/message_encryptor_test.rb index 5c2b44f188..2fba62bdd6 100644 --- a/activesupport/test/message_encryptor_test.rb +++ b/activesupport/test/message_encryptor_test.rb @@ -4,7 +4,7 @@ require 'active_support/time' class MessageEncryptorTest < Test::Unit::TestCase def setup @encryptor = ActiveSupport::MessageEncryptor.new(ActiveSupport::SecureRandom.hex(64)) - @data = {:some=>"data", :now=>Time.now} + @data = { :some => "data", :now => Time.local(2010) } end def test_simple_round_tripping diff --git a/activesupport/test/message_verifier_test.rb b/activesupport/test/message_verifier_test.rb index 714a3b3a39..4821311244 100644 --- a/activesupport/test/message_verifier_test.rb +++ b/activesupport/test/message_verifier_test.rb @@ -12,7 +12,7 @@ require 'active_support/time' class MessageVerifierTest < Test::Unit::TestCase def setup @verifier = ActiveSupport::MessageVerifier.new("Hey, I'm a secret!") - @data = {:some=>"data", :now=>Time.now} + @data = { :some => "data", :now => Time.local(2010) } end def test_simple_round_tripping diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 1928da51ca..3092fe01ae 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -86,6 +86,46 @@ class AssertDifferenceTest < ActiveSupport::TestCase end end +class AssertBlankTest < ActiveSupport::TestCase + BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", [], {} ] + NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ] + + def test_assert_blank_true + BLANK.each { |v| assert_blank v } + end + + def test_assert_blank_false + NOT_BLANK.each { |v| + begin + assert_blank v + fail 'should not get to here' + rescue Exception => e + assert_match(/is not blank/, e.message) + end + } + end +end + +class AssertPresentTest < ActiveSupport::TestCase + BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", [], {} ] + NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ] + + def test_assert_blank_true + NOT_BLANK.each { |v| assert_present v } + end + + def test_assert_blank_false + BLANK.each { |v| + begin + assert_present v + fail 'should not get to here' + rescue Exception => e + assert_match(/is blank/, e.message) + end + } + end +end + # These should always pass if ActiveSupport::Testing.const_defined?(:Default) class NotTestingThingsTest < Test::Unit::TestCase |