Skip to content

Commit

Permalink
Allow passing cache: false to disable cache in getter
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Apr 6, 2017
1 parent 4fd73d8 commit b4858ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/mobility/backend/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ module Backend
module Cache
# @group Backend Accessors
# @!macro backend_reader
def read(locale, **_)
def read(locale, **options)
return super if options[:cache] == false
if write_to_cache? || cache.has_key?(locale)
cache[locale]
else
Expand Down
6 changes: 6 additions & 0 deletions spec/mobility/backend/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def backend_double
2.times { expect(backend.read(:fr, options)).to eq("foo") }
end

it "does not cache reads with cache: false option" do
backend = cached_backend_class.new("model", "attribute")
expect(backend.backend_double).to receive(:read).twice.with(:fr, options.merge(cache: false)).and_return("foo")
2.times { expect(backend.read(:fr, options.merge(cache: false))).to eq("foo") }
end

it "always returns from cache if backend defines write_to_cache? to return true" do
cache = double("cache")
backend_class.class_eval do
Expand Down

0 comments on commit b4858ac

Please sign in to comment.