class StaleOptions::AbstractOptions
Public Class Methods
new(record, options = {})
click to toggle source
Params:
record
Object
-
An
Object
,Array
orActiveRecord::Relation
.
options
Hash
:cache_by
String
orSymbol
-
A name of method which returns unique identifier of object for caching.
For arrays and relations if value is
itself
, then it will be cached as it is, otherwise this method will be called on each element. Relations will be converted to arrays by calling#to_a
.Hint: To cache an array of “simple” objects (e.g.
String
orNumeric
) set it toitself
.
Default:
:updated_at
.:last_modified
String
orSymbol
-
If
record
is a relation, then an attribute name. Ifrecord
is anArray
orObject
, then a method name. Expected an instance ofActiveSupport::TimeWithZone
,DateTime
,Time
.
ActiveSupport::TimeWithZone
,DateTime
,Time
ornil
-
To set
last_modified
.
Default:
:updated_at
.
# File lib/stale_options/abstract_options.rb, line 24 def initialize(record, options = {}) @record = record @options = { cache_by: :updated_at, last_modified: :updated_at }.merge!(options) end
Public Instance Methods
to_h()
click to toggle source
Returns options for ActionController::ConditionalGet#stale?
# File lib/stale_options/abstract_options.rb, line 33 def to_h { etag: etag, last_modified: nil }.tap do |h| unless last_modified_opt.nil? h[:last_modified] = StaleOptions.time?(last_modified_opt) ? last_modified_opt : last_modified h[:last_modified] = h[:last_modified]&.utc end end end
Protected Instance Methods
etag()
click to toggle source
# File lib/stale_options/abstract_options.rb, line 76 def etag raise NotImplementedError end
last_modified()
click to toggle source
# File lib/stale_options/abstract_options.rb, line 80 def last_modified raise NotImplementedError end
Private Instance Methods
cache_by_itself?()
click to toggle source
# File lib/stale_options/abstract_options.rb, line 48 def cache_by_itself? cache_by_opt.to_s == 'itself' end
cache_by_opt()
click to toggle source
# File lib/stale_options/abstract_options.rb, line 44 def cache_by_opt @options[:cache_by] end
collection_hash(collection)
click to toggle source
# File lib/stale_options/abstract_options.rb, line 70 def collection_hash(collection) object_hash(collection.map { |obj| read_cache_by(obj) }) end
last_modified_opt()
click to toggle source
# File lib/stale_options/abstract_options.rb, line 58 def last_modified_opt @options[:last_modified] end
object_hash(obj)
click to toggle source
# File lib/stale_options/abstract_options.rb, line 66 def object_hash(obj) Digest::MD5.hexdigest(Marshal.dump(obj)) end
read_cache_by(obj)
click to toggle source
# File lib/stale_options/abstract_options.rb, line 52 def read_cache_by(obj) value = obj.public_send(cache_by_opt) StaleOptions.time?(value) ? value.to_f : value end
read_last_modified(obj)
click to toggle source
# File lib/stale_options/abstract_options.rb, line 62 def read_last_modified(obj) obj.public_send(last_modified_opt) end