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

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



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

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



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

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

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



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

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