Sha256: a79962f752ba0b1731d7ec95adf424acdeb11706629a16de9eb3355c5b5c1fb6

Contents?: true

Size: 1.15 KB

Versions: 6

Compression:

Stored size: 1.15 KB

Contents

require "infopark_component_cache/guards/cms_state_guard"

module InfoparkComponentCache
  module Guards
    # @author Tomasz Przedmojski <tomasz.przedmojski@infopark.de>
    #
    # 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)
    class ObjCount < CmsStateGuard
      def consistent?
        count_known? && no_changes_since?
      end

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

      # @return true if obj count can be read from cache with {#cache_key}
      def count_known?
        cache.exist?(cache_key) && cache.read(cache_key).kind_of?(Integer)
      end

      # @return true if no obj has been deleted or added since last {#guard!}
      def no_changes_since?
        current_count == cache.read(cache_key)
      end

      # @return [String] the cache key for storing {#current_count}
      def cache_key
        component.cache_key("obj_count")
      end

      # @return [Integer] the number of Objs in the database
      def current_count
        scoped_relation.count
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
infopark_component_cache-5.0.2 lib/infopark_component_cache/guards/obj_count.rb
infopark_component_cache-5.0.1 lib/infopark_component_cache/guards/obj_count.rb
infopark_component_cache-4.2.0 lib/infopark_component_cache/guards/obj_count.rb
infopark_component_cache-4.1.0 lib/infopark_component_cache/guards/obj_count.rb
infopark_component_cache-4.0.1 lib/infopark_component_cache/guards/obj_count.rb
infopark_component_cache-4.0.0 lib/infopark_component_cache/guards/obj_count.rb