module SleeperRb::Utilities::Cache

This module encapsulates the logic for caching and refreshing values retrieved from the Sleeper API.

Public Class Methods

included(base) click to toggle source

Ensures that ClassMethods are extended into the base class when including.

# File lib/sleeper_rb/utilities/cache.rb, line 81
def self.included(base)
  base.extend ClassMethods
end
new(opts = {}) click to toggle source

Sets up an object with values for any cached_attrs pre-initialized if provided.

@param opts [Hash] Key/value pairs that can match any cached_attr.

# File lib/sleeper_rb/utilities/cache.rb, line 89
def initialize(opts = {})
  opts.slice(*cached_attrs.keys).each do |key, val|
    instance_variable_set(:"@#{key}", cached_attrs[key].call(val))
  end
end

Public Instance Methods

refresh() click to toggle source

Refreshes all associations and memoized values set by cached_attr.

@return [self]

# File lib/sleeper_rb/utilities/cache.rb, line 99
def refresh
  cached_attrs.keys.reject { |k| skip_refresh_fields == :all || skip_refresh_fields.include?(k) }.each do |attr|
    ivar = :"@#{attr}"
    instance_variable_set(ivar, nil)
  end
  @values = nil
  @cached_associations = {}
  self
end