Class: InfoparkComponentCache::Guards::ValidUntil

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

Overview

This guard ensures that any object, whose valid until date has been passed will cause inconsistency.

Author:

Direct Known Subclasses

DelayedValidUntil

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_min_valid_until.

Returns:



33
34
35
# File 'lib/infopark_component_cache/guards/valid_until.rb', line 33

def cache_key
  component.cache_key("min_valid_until")
end

#consistent?Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
# File 'lib/infopark_component_cache/guards/valid_until.rb', line 10

def consistent?
  if min_valid_until_known?
    no_changes_since?
  else
    current_min_valid_until.nil?
  end
end

#current_min_valid_untilTime

Returns the timestamp of the the object that will be deactivated in nearest future.

Returns:

  • (Time)

    the timestamp of the the object that will be deactivated in nearest future



38
39
40
41
42
43
# File 'lib/infopark_component_cache/guards/valid_until.rb', line 38

def current_min_valid_until
  str_value = scoped_relation.where("valid_until > ?", Time.now.to_iso).minimum(:valid_until)
  return str_value if str_value.kind_of? Time

  RailsConnector::DateAttribute.parse(str_value) if str_value.present?
end

#guard!Object



18
19
20
# File 'lib/infopark_component_cache/guards/valid_until.rb', line 18

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

#min_valid_until_known?Boolean

Returns true if a timestamp can be read from cache with #cache_key.

Returns:

  • (Boolean)

    true if a timestamp can be read from cache with #cache_key



23
24
25
# File 'lib/infopark_component_cache/guards/valid_until.rb', line 23

def min_valid_until_known?
  cache.exist?(cache_key) && cache.read(cache_key).kind_of?(Time)
end

#no_changes_since?Boolean

Returns true if no obj has been changed since last #guard!.

Returns:

  • (Boolean)

    true if no obj has been changed since last #guard!



28
29
30
# File 'lib/infopark_component_cache/guards/valid_until.rb', line 28

def no_changes_since?
  current_min_valid_until == cache.read(cache_key) && current_min_valid_until > Time.now
end