Class: RailsSettings::CachedSettings

Inherits:
Settings
  • Object
show all
Defined in:
lib/rails-settings/cached_settings.rb

Direct Known Subclasses

ScopedSettings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Settings

[]=, destroy, get_all, merge!, method_missing, object, thing_scoped, #value, #value=, where

Class Method Details

.[](var_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rails-settings/cached_settings.rb', line 27

def [](var_name)
  value = Rails.cache.fetch(cache_key(var_name, @object)) do
    super(var_name)
  end

  if value.nil?
    @@defaults[var_name.to_s] if value.nil?
  else
    value
  end
end

.cache_key(var_name, scope_object) ⇒ Object



20
21
22
23
24
25
# File 'lib/rails-settings/cached_settings.rb', line 20

def cache_key(var_name, scope_object)
  scope = "rails_settings_cached:"
  scope << "#{@cache_prefix.call}:" if @cache_prefix
  scope << "#{scope_object.class.name}-#{scope_object.id}:" if scope_object
  scope << "#{var_name}"
end

.cache_prefix(&block) ⇒ Object



16
17
18
# File 'lib/rails-settings/cached_settings.rb', line 16

def cache_prefix(&block)
  @cache_prefix = block
end

.save_default(key, value) ⇒ Object



39
40
41
42
# File 'lib/rails-settings/cached_settings.rb', line 39

def save_default(key, value)
  return false unless self[key].nil?
  self[key] = value
end

Instance Method Details

#cache_keyObject



11
12
13
# File 'lib/rails-settings/cached_settings.rb', line 11

def cache_key
  self.class.cache_key(var, thing)
end

#expire_cacheObject



7
8
9
# File 'lib/rails-settings/cached_settings.rb', line 7

def expire_cache
  Rails.cache.delete(cache_key)
end

#rewrite_cacheObject



3
4
5
# File 'lib/rails-settings/cached_settings.rb', line 3

def rewrite_cache
  Rails.cache.write(cache_key, value)
end