Skip to content

Commit

Permalink
Coerce delete return value to a boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
sorentwo committed Dec 16, 2014
1 parent 9fe86d9 commit 897fd06
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
26 changes: 25 additions & 1 deletion lib/readthis/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ def read(key, options = {})
end
end

# Writes data to the cache using the given key. Will overwrite whatever
# value is already stored at that key.
#
# @param [String] Key for lookup
# @param [Hash] Optional overrides
#
# @example
#
# cache.write('some-key', 'a bunch of text') # => 'OK'
# cache.write('some-key', 'short lived', expires_in: 60) # => 'OK'
# cache.write('some-key', 'lives elsehwere', namespace: 'cache') # => 'OK'
#
def write(key, value, options = {})
options = merged_options(options)
namespaced = namespaced_key(key, options)
Expand All @@ -88,9 +100,21 @@ def write(key, value, options = {})
end
end

# Delete the value stored at the specified key. Returns `true` if
# anything was deleted, `false` otherwise.
#
# @params [String] The key for lookup
# @params [Hash] Optional overrides
#
# @example
#
# cache.delete('existing-key') # => true
# cache.delete('random-key') # => false
def delete(key, options = {})
namespaced = namespaced_key(key, merged_options(options))

invoke(:delete, key) do |store|
store.del(namespaced_key(key, merged_options(options)))
store.del(namespaced) > 0
end
end

Expand Down
6 changes: 5 additions & 1 deletion spec/readthis/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,14 @@
describe '#delete' do
it 'deletes an existing key' do
cache.write('not-long', 'for this world')
cache.delete('not-long')

expect(cache.delete('not-long')).to be_truthy
expect(cache.read('not-long')).to be_nil
end

it 'safely returns false if nothing is deleted' do
expect(cache.delete('no-such-key')).to be_falsy
end
end

describe '#increment' do
Expand Down

0 comments on commit 897fd06

Please sign in to comment.