Skip to content

Commit

Permalink
Add method Cell#cached?
Browse files Browse the repository at this point in the history
  • Loading branch information
PikachuEXE committed Dec 5, 2018
1 parent 2d33ad0 commit 68bb0ad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
14 changes: 14 additions & 0 deletions lib/cell/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ def cache?(state, *args)
perform_caching? and state_cached?(state) and self.class.conditional_procs[state].(self, *args)
end

# Method for getting whether the cell is cached
# If caching is disabled for the cell or the whole app
# It returns `false` without hitting cache store
#
# Same arguments as `#call`
def cached?(state=:show, *args)
return false unless cache?(state, *args)

key = self.class.state_cache_key(state, self.class.version_procs[state].(self, *args))
options = self.class.cache_options[state].(self, *args)

cache_store.exist?(key, options)
end

private

def perform_caching?
Expand Down
24 changes: 21 additions & 3 deletions test/cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class CacheTest < Minitest::Spec
def fetch(key, options, &block)
self[key] || self[key] = yield
end

def exist?(key, _options = nil)
!!self[key]
end
end.new

module Cache
Expand All @@ -24,9 +28,23 @@ class Index < Cell::ViewModel
include Cache
end

it do
Index.new(1).().must_equal("1")
Index.new(2).().must_equal("1")
describe "#call" do
it "returns cached value after value cached" do
Index.new(1).().must_equal("1")
Index.new(2).().must_equal("1")
end
end

describe "#cached?" do
it "returns cached value after value cached" do
cell_1 = Index.new(1)
cell_2 = Index.new(2)

cell_1.cached?.must_equal(false)
cell_1.()
cell_1.cached?.must_equal(true)
cell_2.cached?.must_equal(true)
end
end
end

0 comments on commit 68bb0ad

Please sign in to comment.