1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #22206 from grosser/grosser/dry

dry up increment/decrement
This commit is contained in:
Santiago Pastorino 2015-11-08 21:35:18 -03:00
commit 2c88ee604b

View file

@ -75,30 +75,12 @@ module ActiveSupport
# Increment an integer value in the cache. # Increment an integer value in the cache.
def increment(name, amount = 1, options = nil) def increment(name, amount = 1, options = nil)
synchronize do modify_value(name, amount, options)
options = merged_options(options)
if num = read(name, options)
num = num.to_i + amount
write(name, num, options)
num
else
nil
end
end
end end
# Decrement an integer value in the cache. # Decrement an integer value in the cache.
def decrement(name, amount = 1, options = nil) def decrement(name, amount = 1, options = nil)
synchronize do modify_value(name, -amount, options)
options = merged_options(options)
if num = read(name, options)
num = num.to_i - amount
write(name, num, options)
num
else
nil
end
end
end end
def delete_matched(matcher, options = nil) def delete_matched(matcher, options = nil)
@ -167,6 +149,19 @@ module ActiveSupport
!!entry !!entry
end end
end end
private
def modify_value(name, amount, options)
synchronize do
options = merged_options(options)
if num = read(name, options)
num = num.to_i + amount
write(name, num, options)
num
end
end
end
end end
end end
end end