Module: CacheJSON::Base

Defined in:
lib/cache_json/base.rb

Defined Under Namespace

Modules: ClassMethods Classes: Cache

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
# File 'lib/cache_json/base.rb', line 7

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#cache_expiring_soon?(args:, cache: nil) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/cache_json/base.rb', line 20

def cache_expiring_soon?(args:, cache: nil)
  cache ||= Cache.new(args: args, options: self.class.cache_json_full_options)
  cache.cache_expiring_soon?
end

#check_cache(args:, cache: nil) ⇒ Object



36
37
38
39
# File 'lib/cache_json/base.rb', line 36

def check_cache(args:, cache: nil)
  cache ||= Cache.new(args: args, options: self.class.cache_json_full_options)
  cache.cached_results
end

#clear_cache!Object



25
26
27
# File 'lib/cache_json/base.rb', line 25

def clear_cache!
  Cache.new(options: self.class.cache_json_full_options).clear_cache!
end

#refresh_cache!(args:, cache: nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/cache_json/base.rb', line 29

def refresh_cache!(args:, cache: nil)
  cache ||= Cache.new(args: args, options: self.class.cache_json_full_options)
  results = compute_results(**args)
  cache.cached_results = results
  results
end

#results(args = {}) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
# File 'lib/cache_json/base.rb', line 11

def results(args = {})
  raise ArgumentError, 'Must use keyword arguments' unless args.is_a?(Hash)

  options = self.class.cache_json_full_options
  cache = Cache.new(args: args, options: options)
  check_cache(args: args, cache: cache) ||
    JSON.parse(refresh_cache!(args: args, cache: cache).to_json) # stringify keys
end