Sha256: cfeae8a2903ac162acca72438db80b6f0765bd84c83301b2ea62c2fce10c3d37

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

require "infopark_component_cache/guards/cms_state_guard"

module InfoparkComponentCache
  module Guards
    # @author Tomasz Przedmojski <tomasz.przedmojski@infopark.de>
    #
    # This guard ensures that any object, whose valid until date has been passed
    # will cause inconsistency.
    class ValidUntil < CmsStateGuard
      def consistent?
        if min_valid_until_known?
          return no_changes_since?
        else
          return current_min_valid_until.nil?
        end
      end

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

      # @return true if a timestamp can be read from cache with {#cache_key}
      def min_valid_until_known?
        cache.exist?(cache_key) && cache.read(cache_key).kind_of?(Time)
      end

      # @return true if no obj has been changed since last {#guard!}
      def no_changes_since?
        current_min_valid_until == cache.read(cache_key) && current_min_valid_until > Time.now
      end

      # @return [String] the cache key for storing {#current_min_valid_until}
      def cache_key
        component.cache_key('min_valid_until')
      end

      # @return [Time] the timestamp of the the object that will be deactivated in nearest future
      def current_min_valid_until
        str_value = scoped_relation.where('valid_until > ?', Time.now.to_iso).minimum(:valid_until)
        if str_value.present?
          RailsConnector::DateAttribute.parse(str_value)
        else
          nil
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
infopark_component_cache-3.2.0 lib/infopark_component_cache/guards/valid_until.rb
infopark_component_cache-3.1.1 lib/infopark_component_cache/guards/valid_until.rb
infopark_component_cache-3.1.0 lib/infopark_component_cache/guards/valid_until.rb
infopark_component_cache-3.0.0 lib/infopark_component_cache/guards/valid_until.rb
infopark_component_cache-2.0.0 lib/infopark_component_cache/guards/valid_until.rb