Module: InertiaRails::PropCacheable

Included in:
CachedProp, DeferProp, OptionalProp
Defined in:
lib/inertia_rails/prop_cacheable.rb

Instance Method Summary collapse

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/inertia_rails/prop_cacheable.rb', line 21

def cached?
  !@cache_key.nil?
end

#call(controller, **context) ⇒ Object



25
26
27
28
29
30
# File 'lib/inertia_rails/prop_cacheable.rb', line 25

def call(controller, **context)
  return super unless cached?

  json = InertiaRails.cache_store.fetch(@cache_key, **(@cache_options || {})) { super.to_json }
  RawJson.new(json)
end

#initialize(**props, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/inertia_rails/prop_cacheable.rb', line 5

def initialize(**props, &block)
  cache_arg = props.delete(:cache)

  if cache_arg.is_a?(Hash)
    raise ArgumentError, 'cache: hash requires a :key' unless cache_arg.key?(:key)

    @cache_key = derive_cache_key(cache_arg.delete(:key))
    @cache_options = cache_arg.freeze
  elsif cache_arg
    @cache_key = derive_cache_key(cache_arg)
    @cache_options = nil
  end

  super
end