Sha256: 73fafe3a43810df16311ab05411498970d3fa83772983b1d45e0ae7c261e2fed

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 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 that becomes valid (through valid_from)
    # will cause inconsistency
    class ValidFrom < CmsStateGuard
      def consistent?
        if min_valid_from_known?
          no_changes_since?
        else
          current_min_valid_from.nil?
        end
      end

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

      # @return true if a timestamp can be read from cache with {#cache_key}
      def min_valid_from_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_from == cache.read(cache_key) && current_min_valid_from > Time.now
      end

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

      # @return [Time] the timestamp of the most recent valid_from, or nil if none found
      def current_min_valid_from
        str_value = scoped_relation.where("valid_from > ?", Time.now.to_iso).minimum(:valid_from)
        return str_value if str_value.kind_of? Time

        RailsConnector::DateAttribute.parse(str_value) if str_value.present?
      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/valid_from.rb
infopark_component_cache-5.0.1 lib/infopark_component_cache/guards/valid_from.rb
infopark_component_cache-4.2.0 lib/infopark_component_cache/guards/valid_from.rb
infopark_component_cache-4.1.0 lib/infopark_component_cache/guards/valid_from.rb
infopark_component_cache-4.0.1 lib/infopark_component_cache/guards/valid_from.rb
infopark_component_cache-4.0.0 lib/infopark_component_cache/guards/valid_from.rb