module SleeperRb::Utilities::Cache::ClassMethods
Methods to be extended into the class when included.
Constants
- DEFAULT_TRANSLATORS
Public Instance Methods
cached_association(name, &block)
click to toggle source
Creates a memoized association that returns another resource.
# File lib/sleeper_rb/utilities/cache.rb, line 36 def cached_association(name, &block) define_method(name) do |arg = nil| if arg cached_associations[name] ||= {} cached_associations[name][arg.to_s] ||= instance_exec(arg, &block) else cached_associations[name] ||= instance_exec(&block) end end end
cached_attr(*attrs)
click to toggle source
Creates a memoized attribute reader for the named attributes.
Example¶ ↑
+cached_attr :display_name, :username, foo: ->(x) { x + 3 }+
# File lib/sleeper_rb/utilities/cache.rb, line 21 def cached_attr(*attrs) attrs.each do |attr| if attr.is_a?(Hash) attr.each do |field_name, translator| translator = DEFAULT_TRANSLATORS[translator] if translator.is_a?(Symbol) create_method(field_name, translator) end else create_method(attr, ->(x) { x }) end end end
cached_attrs()
click to toggle source
The stored mapping of cached attribute names to their translator functions.
# File lib/sleeper_rb/utilities/cache.rb, line 57 def cached_attrs @cached_attrs ||= {} end
skip_refresh(*fields)
click to toggle source
Takes in a list of fields which should be excluded from refresh. Use :all for models which do not have any endpoint but only retrieve their data from another object. These models have no API source to refresh themselves, but they still have associations.
# File lib/sleeper_rb/utilities/cache.rb, line 51 def skip_refresh(*fields) fields.any? { |field| field == :all } ? @skip_refresh_fields = :all : skip_refresh_fields.concat(fields) end
skip_refresh_fields()
click to toggle source
The stored list of fields which should be excluded from refresh.
# File lib/sleeper_rb/utilities/cache.rb, line 63 def skip_refresh_fields @skip_refresh_fields ||= [] end
Private Instance Methods
create_method(field_name, translator)
click to toggle source
# File lib/sleeper_rb/utilities/cache.rb, line 69 def create_method(field_name, translator) cached_attrs[field_name.to_sym] = translator skip_refresh_fields << field_name.to_sym if field_name.to_s =~ /\w+_id/ define_method(field_name) do ivar = :"@#{field_name}" instance_variable_get(ivar) || instance_variable_set(ivar, translator.call(values[field_name.to_sym])) end end