aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/cache/file_store.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute.rb8
-rw-r--r--activesupport/lib/active_support/core_ext/enumerable.rb22
-rw-r--r--activesupport/lib/active_support/core_ext/string/conversions.rb1
-rw-r--r--activesupport/lib/active_support/multibyte/unicode.rb1
-rw-r--r--activesupport/lib/active_support/ordered_hash.rb2
-rw-r--r--activesupport/test/caching_test.rb7
-rw-r--r--activesupport/test/core_ext/duplicable_test.rb19
-rw-r--r--activesupport/test/core_ext/enumerable_test.rb84
-rw-r--r--activesupport/test/json/decoding_test.rb2
10 files changed, 99 insertions, 49 deletions
diff --git a/activesupport/lib/active_support/cache/file_store.rb b/activesupport/lib/active_support/cache/file_store.rb
index c4da04e532..f7c01948b4 100644
--- a/activesupport/lib/active_support/cache/file_store.rb
+++ b/activesupport/lib/active_support/cache/file_store.rb
@@ -16,7 +16,7 @@ module ActiveSupport
def initialize(cache_path, options = nil)
super(options)
- @cache_path = cache_path
+ @cache_path = cache_path.to_s
extend Strategy::LocalCache
end
diff --git a/activesupport/lib/active_support/core_ext/class/attribute.rb b/activesupport/lib/active_support/core_ext/class/attribute.rb
index ca9b2c1b60..45bec264ff 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute.rb
@@ -110,12 +110,6 @@ class Class
private
def singleton_class?
- # in case somebody is crazy enough to overwrite allocate
- allocate = Class.instance_method(:allocate)
- # object.class always points to a real (non-singleton) class
- allocate.bind(self).call.class != self
- rescue TypeError
- # MRI/YARV/JRuby all disallow creating new instances of a singleton class
- true
+ !name || '' == name
end
end
diff --git a/activesupport/lib/active_support/core_ext/enumerable.rb b/activesupport/lib/active_support/core_ext/enumerable.rb
index 3e05c6eaf2..ddb4f3012f 100644
--- a/activesupport/lib/active_support/core_ext/enumerable.rb
+++ b/activesupport/lib/active_support/core_ext/enumerable.rb
@@ -20,6 +20,7 @@ module Enumerable
# "2006-02-24 -> Transcript, Transcript"
# "2006-02-23 -> Transcript"
def group_by
+ return to_enum :group_by unless block_given?
assoc = ActiveSupport::OrderedHash.new
each do |element|
@@ -75,9 +76,10 @@ module Enumerable
#
# (1..5).each_with_object(1) { |value, memo| memo *= value } # => 1
#
- def each_with_object(memo, &block)
+ def each_with_object(memo)
+ return to_enum :each_with_object, memo unless block_given?
each do |element|
- block.call(element, memo)
+ yield element, memo
end
memo
end unless [].respond_to?(:each_with_object)
@@ -90,14 +92,22 @@ module Enumerable
# => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
#
def index_by
+ return to_enum :index_by unless block_given?
Hash[map { |elem| [yield(elem), elem] }]
end
- # Returns true if the collection has more than 1 element. Functionally equivalent to collection.size > 1.
+ # Returns true if the enumerable has more than 1 element. Functionally equivalent to enum.to_a.size > 1.
# Can be called with a block too, much like any?, so people.many? { |p| p.age > 26 } returns true if more than 1 person is over 26.
- def many?(&block)
- size = block_given? ? count(&block) : self.size
- size > 1
+ def many?
+ cnt = 0
+ if block_given?
+ any? do |element|
+ cnt += 1 if yield element
+ cnt > 1
+ end
+ else
+ any?{ (cnt += 1) > 1 }
+ end
end
# The negative of the Enumerable#include?. Returns true if the collection does not include the object.
diff --git a/activesupport/lib/active_support/core_ext/string/conversions.rb b/activesupport/lib/active_support/core_ext/string/conversions.rb
index 664537eea4..0f8933b658 100644
--- a/activesupport/lib/active_support/core_ext/string/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/string/conversions.rb
@@ -1,3 +1,4 @@
+# encoding: utf-8
require 'date'
require 'active_support/core_ext/time/publicize_conversion_methods'
require 'active_support/core_ext/time/calculations'
diff --git a/activesupport/lib/active_support/multibyte/unicode.rb b/activesupport/lib/active_support/multibyte/unicode.rb
index 513f83e445..754ca9290b 100644
--- a/activesupport/lib/active_support/multibyte/unicode.rb
+++ b/activesupport/lib/active_support/multibyte/unicode.rb
@@ -1,3 +1,4 @@
+# encoding: utf-8
module ActiveSupport
module Multibyte
module Unicode
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb
index 68f4bd66da..7f70628933 100644
--- a/activesupport/lib/active_support/ordered_hash.rb
+++ b/activesupport/lib/active_support/ordered_hash.rb
@@ -6,7 +6,7 @@ end
require 'yaml'
YAML.add_builtin_type("omap") do |type, val|
- ActiveSupport::OrderedHash[val.map(&:to_a).map(&:first)]
+ ActiveSupport::OrderedHash[val.map{ |v| v.to_a.first }]
end
module ActiveSupport
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index 8b3e4800c3..402c6695aa 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -521,6 +521,7 @@ class FileStoreTest < ActiveSupport::TestCase
Dir.mkdir(cache_dir) unless File.exist?(cache_dir)
@cache = ActiveSupport::Cache.lookup_store(:file_store, cache_dir, :expires_in => 60)
@peek = ActiveSupport::Cache.lookup_store(:file_store, cache_dir, :expires_in => 60)
+ @cache_with_pathname = ActiveSupport::Cache.lookup_store(:file_store, Pathname.new(cache_dir), :expires_in => 60)
end
def teardown
@@ -540,6 +541,12 @@ class FileStoreTest < ActiveSupport::TestCase
key = @cache.send(:key_file_path, "views/index?id=1")
assert_equal "views/index?id=1", @cache.send(:file_path_key, key)
end
+
+ def test_key_transformation_with_pathname
+ FileUtils.touch(File.join(cache_dir, "foo"))
+ key = @cache_with_pathname.send(:key_file_path, "views/index?id=1")
+ assert_equal "views/index?id=1", @cache_with_pathname.send(:file_path_key, key)
+ end
end
class MemoryStoreTest < ActiveSupport::TestCase
diff --git a/activesupport/test/core_ext/duplicable_test.rb b/activesupport/test/core_ext/duplicable_test.rb
index 24e0ccd9b3..e48e6a7c45 100644
--- a/activesupport/test/core_ext/duplicable_test.rb
+++ b/activesupport/test/core_ext/duplicable_test.rb
@@ -4,22 +4,27 @@ require 'active_support/core_ext/object/duplicable'
require 'active_support/core_ext/numeric/time'
class DuplicableTest < Test::Unit::TestCase
- NO = [nil, false, true, :symbol, 1, 2.3, BigDecimal.new('4.56'), Class.new, Module.new, 5.seconds]
+ RAISE_DUP = [nil, false, true, :symbol, 1, 2.3, BigDecimal.new('4.56'), 5.seconds]
YES = ['1', Object.new, /foo/, [], {}, Time.now]
+ NO = [Class.new, Module.new]
def test_duplicable
- NO.each do |v|
+ (RAISE_DUP + NO).each do |v|
assert !v.duplicable?
- begin
- v.dup
- fail
- rescue Exception
- end
end
YES.each do |v|
assert v.duplicable?
+ end
+
+ (YES + NO).each do |v|
assert_nothing_raised { v.dup }
end
+
+ RAISE_DUP.each do |v|
+ assert_raises(TypeError) do
+ v.dup
+ end
+ end
end
end
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index 4655bfe519..cdfa991a34 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -8,6 +8,19 @@ class SummablePayment < Payment
end
class EnumerableTests < Test::Unit::TestCase
+ Enumerator = [].each.class
+
+ class GenericEnumerable
+ include Enumerable
+ def initialize(values = [1, 2, 3])
+ @values = values
+ end
+
+ def each
+ @values.each{|v| yield v}
+ end
+ end
+
def test_group_by
names = %w(marcel sam david jeremy)
klass = Struct.new(:name)
@@ -17,7 +30,8 @@ class EnumerableTests < Test::Unit::TestCase
people << p
end
- grouped = objects.group_by { |object| object.name }
+ enum = GenericEnumerable.new(objects)
+ grouped = enum.group_by { |object| object.name }
grouped.each do |name, group|
assert group.all? { |person| person.name == name }
@@ -25,20 +39,24 @@ class EnumerableTests < Test::Unit::TestCase
assert_equal objects.uniq.map(&:name), grouped.keys
assert({}.merge(grouped), "Could not convert ActiveSupport::OrderedHash into Hash")
+ assert_equal Enumerator, enum.group_by.class
+ assert_equal grouped, enum.group_by.each(&:name)
end
def test_sums
- assert_equal 30, [5, 15, 10].sum
- assert_equal 30, [5, 15, 10].sum { |i| i }
+ enum = GenericEnumerable.new([5, 15, 10])
+ assert_equal 30, enum.sum
+ assert_equal 60, enum.sum { |i| i * 2}
- assert_equal 'abc', %w(a b c).sum
- assert_equal 'abc', %w(a b c).sum { |i| i }
+ enum = GenericEnumerable.new(%w(a b c))
+ assert_equal 'abc', enum.sum
+ assert_equal 'aabbcc', enum.sum { |i| i * 2 }
- payments = [ Payment.new(5), Payment.new(15), Payment.new(10) ]
+ payments = GenericEnumerable.new([ Payment.new(5), Payment.new(15), Payment.new(10) ])
assert_equal 30, payments.sum(&:price)
assert_equal 60, payments.sum { |p| p.price * 2 }
- payments = [ SummablePayment.new(5), SummablePayment.new(15) ]
+ payments = GenericEnumerable.new([ SummablePayment.new(5), SummablePayment.new(15) ])
assert_equal SummablePayment.new(20), payments.sum
assert_equal SummablePayment.new(20), payments.sum { |p| p }
end
@@ -46,21 +64,21 @@ class EnumerableTests < Test::Unit::TestCase
def test_nil_sums
expected_raise = TypeError
- assert_raise(expected_raise) { [5, 15, nil].sum }
+ assert_raise(expected_raise) { GenericEnumerable.new([5, 15, nil]).sum }
- payments = [ Payment.new(5), Payment.new(15), Payment.new(10), Payment.new(nil) ]
+ payments = GenericEnumerable.new([ Payment.new(5), Payment.new(15), Payment.new(10), Payment.new(nil) ])
assert_raise(expected_raise) { payments.sum(&:price) }
assert_equal 60, payments.sum { |p| p.price.to_i * 2 }
end
def test_empty_sums
- assert_equal 0, [].sum
- assert_equal 0, [].sum { |i| i }
- assert_equal Payment.new(0), [].sum(Payment.new(0))
+ assert_equal 0, GenericEnumerable.new([]).sum
+ assert_equal 0, GenericEnumerable.new([]).sum { |i| i + 10 }
+ assert_equal Payment.new(0), GenericEnumerable.new([]).sum(Payment.new(0))
end
- def test_enumerable_sums
+ def test_range_sums
assert_equal 20, (1..4).sum { |i| i * 2 }
assert_equal 10, (1..4).sum
assert_equal 10, (1..4.5).sum
@@ -69,29 +87,43 @@ class EnumerableTests < Test::Unit::TestCase
end
def test_each_with_object
- result = %w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
+ enum = GenericEnumerable.new(%w(foo bar))
+ result = enum.each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
assert_equal({'foo' => 'FOO', 'bar' => 'BAR'}, result)
+ assert_equal Enumerator, enum.each_with_object({}).class
+ result2 = enum.each_with_object({}).each{|str, hsh| hsh[str] = str.upcase}
+ assert_equal result, result2
end
def test_index_by
- payments = [ Payment.new(5), Payment.new(15), Payment.new(10) ]
- assert_equal({ 5 => payments[0], 15 => payments[1], 10 => payments[2] },
+ payments = GenericEnumerable.new([ Payment.new(5), Payment.new(15), Payment.new(10) ])
+ assert_equal({ 5 => Payment.new(5), 15 => Payment.new(15), 10 => Payment.new(10) },
payments.index_by { |p| p.price })
+ assert_equal Enumerator, payments.index_by.class
+ assert_equal({ 5 => Payment.new(5), 15 => Payment.new(15), 10 => Payment.new(10) },
+ payments.index_by.each { |p| p.price })
end
def test_many
- assert ![].many?
- assert ![ 1 ].many?
- assert [ 1, 2 ].many?
-
- assert ![].many? {|x| x > 1 }
- assert ![ 2 ].many? {|x| x > 1 }
- assert ![ 1, 2 ].many? {|x| x > 1 }
- assert [ 1, 2, 2 ].many? {|x| x > 1 }
+ assert_equal false, GenericEnumerable.new([] ).many?
+ assert_equal false, GenericEnumerable.new([ 1 ] ).many?
+ assert_equal true, GenericEnumerable.new([ 1, 2 ] ).many?
+
+ assert_equal false, GenericEnumerable.new([] ).many? {|x| x > 1 }
+ assert_equal false, GenericEnumerable.new([ 2 ] ).many? {|x| x > 1 }
+ assert_equal false, GenericEnumerable.new([ 1, 2 ] ).many? {|x| x > 1 }
+ assert_equal true, GenericEnumerable.new([ 1, 2, 2 ]).many? {|x| x > 1 }
+ end
+
+ def test_many_iterates_only_on_what_is_needed
+ infinity = 1.0/0.0
+ very_long_enum = 0..infinity
+ assert_equal true, very_long_enum.many?
+ assert_equal true, very_long_enum.many?{|x| x > 100}
end
def test_exclude?
- assert [ 1 ].exclude?(2)
- assert ![ 1 ].exclude?(1)
+ assert_equal true, GenericEnumerable.new([ 1 ]).exclude?(2)
+ assert_equal false, GenericEnumerable.new([ 1 ]).exclude?(1)
end
end
diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb
index 6ccffa59b1..201729a6c2 100644
--- a/activesupport/test/json/decoding_test.rb
+++ b/activesupport/test/json/decoding_test.rb
@@ -1,4 +1,4 @@
-# encoding: UTF-8
+# encoding: utf-8
require 'abstract_unit'
require 'active_support/json'
require 'active_support/time'