aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/cache/strategy/local_cache.rb14
-rw-r--r--activesupport/lib/active_support/core_ext/hash/keys.rb4
-rw-r--r--activesupport/lib/active_support/core_ext/hash/transform_values.rb4
-rw-r--r--activesupport/test/caching_test.rb11
-rw-r--r--activesupport/test/core_ext/hash/transform_keys_test.rb6
-rw-r--r--activesupport/test/core_ext/hash/transform_values_test.rb6
6 files changed, 26 insertions, 19 deletions
diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb
index 4e2d3e9875..d521061004 100644
--- a/activesupport/lib/active_support/cache/strategy/local_cache.rb
+++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb
@@ -48,10 +48,6 @@ module ActiveSupport
@data.clear
end
- def fetch(*args, &block)
- @data.fetch(*args, &block)
- end
-
def read_entry(key, options)
@data[key]
end
@@ -64,6 +60,10 @@ module ActiveSupport
def delete_entry(key, options)
!!@data.delete(key)
end
+
+ def fetch_entry(key, options = nil) # :nodoc:
+ @data.fetch(key) { @data[key] = yield }
+ end
end
# Use a local cache for the duration of block.
@@ -103,11 +103,7 @@ module ActiveSupport
protected
def read_entry(key, options) # :nodoc:
if cache = local_cache
- cache.fetch(key) do
- entry = super
- cache.write_entry(key, entry, options)
- entry
- end
+ cache.fetch_entry(key) { super }
else
super
end
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb
index 07a282e8b6..8b2366c4b3 100644
--- a/activesupport/lib/active_support/core_ext/hash/keys.rb
+++ b/activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -10,7 +10,7 @@ class Hash
#
# hash.transform_keys.with_index { |k, i| [k, i].join } # => {"name0"=>"Rob", "age1"=>"28"}
def transform_keys
- return enum_for(:transform_keys) unless block_given?
+ return enum_for(:transform_keys) { size } unless block_given?
result = self.class.new
each_key do |key|
result[yield(key)] = self[key]
@@ -21,7 +21,7 @@ class Hash
# Destructively converts all keys using the +block+ operations.
# Same as +transform_keys+ but modifies +self+.
def transform_keys!
- return enum_for(:transform_keys!) unless block_given?
+ return enum_for(:transform_keys!) { size } unless block_given?
keys.each do |key|
self[yield(key)] = delete(key)
end
diff --git a/activesupport/lib/active_support/core_ext/hash/transform_values.rb b/activesupport/lib/active_support/core_ext/hash/transform_values.rb
index 9ddb838774..7d507ac998 100644
--- a/activesupport/lib/active_support/core_ext/hash/transform_values.rb
+++ b/activesupport/lib/active_support/core_ext/hash/transform_values.rb
@@ -9,7 +9,7 @@ class Hash
#
# { a: 1, b: 2 }.transform_values.with_index { |v, i| [v, i].join.to_i } # => { a: 10, b: 21 }
def transform_values
- return enum_for(:transform_values) unless block_given?
+ return enum_for(:transform_values) { size } unless block_given?
return {} if empty?
result = self.class.new
each do |key, value|
@@ -21,7 +21,7 @@ class Hash
# Destructively converts all values using the +block+ operations.
# Same as +transform_values+ but modifies +self+.
def transform_values!
- return enum_for(:transform_values!) unless block_given?
+ return enum_for(:transform_values!) { size } unless block_given?
each do |key, value|
self[key] = yield(value)
end
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index 94e73b6df3..3629f5e64b 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -633,6 +633,13 @@ module LocalCacheBehavior
end
end
+ def test_local_cache_fetch
+ @cache.with_local_cache do
+ @cache.send(:local_cache).write 'foo', 'bar'
+ assert_equal 'bar', @cache.send(:local_cache).fetch('foo')
+ end
+ end
+
def test_local_cache_of_write_nil
@cache.with_local_cache do
assert @cache.write('foo', nil)
@@ -1102,11 +1109,11 @@ class CacheStoreLoggerTest < ActiveSupport::TestCase
def test_log_with_proc_namespace
proc = Proc.new do
"proc_namespace"
- end
+ end
@cache.fetch('foo', {:namespace => proc}) { 'bar' }
assert_match %r{proc_namespace:foo}, @buffer.string
end
-
+
def test_mute_logging
@cache.mute { @cache.fetch('foo') { 'bar' } }
assert @buffer.string.blank?
diff --git a/activesupport/test/core_ext/hash/transform_keys_test.rb b/activesupport/test/core_ext/hash/transform_keys_test.rb
index 5a0b99e22c..99af274614 100644
--- a/activesupport/test/core_ext/hash/transform_keys_test.rb
+++ b/activesupport/test/core_ext/hash/transform_keys_test.rb
@@ -18,15 +18,17 @@ class TransformKeysTest < ActiveSupport::TestCase
assert_same original, mapped
end
- test "transform_keys returns an Enumerator if no block is given" do
+ test "transform_keys returns a sized Enumerator if no block is given" do
original = { a: 'a', b: 'b' }
enumerator = original.transform_keys
+ assert_equal original.size, enumerator.size
assert_equal Enumerator, enumerator.class
end
- test "transform_keys! returns an Enumerator if no block is given" do
+ test "transform_keys! returns a sized Enumerator if no block is given" do
original = { a: 'a', b: 'b' }
enumerator = original.transform_keys!
+ assert_equal original.size, enumerator.size
assert_equal Enumerator, enumerator.class
end
diff --git a/activesupport/test/core_ext/hash/transform_values_test.rb b/activesupport/test/core_ext/hash/transform_values_test.rb
index 7c33227dc0..114022fbaf 100644
--- a/activesupport/test/core_ext/hash/transform_values_test.rb
+++ b/activesupport/test/core_ext/hash/transform_values_test.rb
@@ -47,15 +47,17 @@ class TransformValuesTest < ActiveSupport::TestCase
assert_nil mapped[:b]
end
- test "transform_values returns an Enumerator if no block is given" do
+ test "transform_values returns a sized Enumerator if no block is given" do
original = { a: 'a', b: 'b' }
enumerator = original.transform_values
+ assert_equal original.size, enumerator.size
assert_equal Enumerator, enumerator.class
end
- test "transform_values! returns an Enumerator if no block is given" do
+ test "transform_values! returns a sized Enumerator if no block is given" do
original = { a: 'a', b: 'b' }
enumerator = original.transform_values!
+ assert_equal original.size, enumerator.size
assert_equal Enumerator, enumerator.class
end