Class: InfoparkComponentCache::Guards::ObjCount

Inherits:
CmsStateGuard show all
Defined in:
lib/infopark_component_cache/guards/obj_count.rb

Overview

This Guard class ensures that the total count of Objs in the database does not change inbetween. (It caches the number of Objs and compares it to the current count)

Author:

Direct Known Subclasses

DelayedObjCount

Instance Attribute Summary

Attributes inherited from ConsistencyGuard

#component, #options

Instance Method Summary collapse

Methods inherited from ConsistencyGuard

#cache, #initialize

Constructor Details

This class inherits a constructor from InfoparkComponentCache::ConsistencyGuard

Instance Method Details

#cache_keyString

Returns the cache key for storing #current_count.

Returns:



30
31
32
# File 'lib/infopark_component_cache/guards/obj_count.rb', line 30

def cache_key
  component.cache_key("obj_count")
end

#consistent?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/infopark_component_cache/guards/obj_count.rb', line 11

def consistent?
  count_known? && no_changes_since?
end

#count_known?Boolean

Returns true if obj count can be read from cache with #cache_key.

Returns:

  • (Boolean)

    true if obj count can be read from cache with #cache_key



20
21
22
# File 'lib/infopark_component_cache/guards/obj_count.rb', line 20

def count_known?
  cache.exist?(cache_key) && cache.read(cache_key).kind_of?(Integer)
end

#current_countInteger

Returns the number of Objs in the database.

Returns:

  • (Integer)

    the number of Objs in the database



35
36
37
# File 'lib/infopark_component_cache/guards/obj_count.rb', line 35

def current_count
  scoped_relation.count
end

#guard!Object



15
16
17
# File 'lib/infopark_component_cache/guards/obj_count.rb', line 15

def guard!
  cache.write(cache_key, current_count)
end

#no_changes_since?Boolean

Returns true if no obj has been deleted or added since last #guard!.

Returns:

  • (Boolean)

    true if no obj has been deleted or added since last #guard!



25
26
27
# File 'lib/infopark_component_cache/guards/obj_count.rb', line 25

def no_changes_since?
  current_count == cache.read(cache_key)
end