Module: Taro::Types::Shared::Caching::ClassMethods

Defined in:
lib/taro/types/shared/caching.rb

Instance Method Summary collapse

Instance Method Details

#cache_key=(arg) ⇒ Object



15
16
17
18
19
20
# File 'lib/taro/types/shared/caching.rb', line 15

def cache_key=(arg)
  arg.nil? || arg.is_a?(Proc) && arg.arity == 1 || arg.is_a?(Hash) ||
    raise(Taro::ArgumentError, "Type.cache_key must be a Proc with arity 1, a Hash, or nil")

  @cache_key = arg
end

#with_cache(cache_key:, expires_in: nil) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/taro/types/shared/caching.rb', line 22

def with_cache(cache_key:, expires_in: nil)
  klass = dup
  klass.cache_key = cache_key.is_a?(Proc) ? cache_key : ->(_) { cache_key }
  klass.expires_in = expires_in
  this = self
  klass.define_singleton_method(:type_class) { this }
  klass
end