Class: RubySettings::CachedSettings

Inherits:
Settings
  • Object
show all
Extended by:
ConfigurationHelpers
Includes:
ConfigurationHelpers
Defined in:
lib/ruby-settings/cached_settings.rb

Direct Known Subclasses

ScopedSettings

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ConfigurationHelpers

cache_store

Methods inherited from Settings

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

Class Method Details

.[](var_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby-settings/cached_settings.rb', line 36

def [](var_name)
  value = cache_store.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



29
30
31
32
33
34
# File 'lib/ruby-settings/cached_settings.rb', line 29

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



25
26
27
# File 'lib/ruby-settings/cached_settings.rb', line 25

def cache_prefix(&block)
  @cache_prefix = block
end

.save_default(key, value) ⇒ Object



48
49
50
51
# File 'lib/ruby-settings/cached_settings.rb', line 48

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

Instance Method Details

#cache_keyObject



17
18
19
# File 'lib/ruby-settings/cached_settings.rb', line 17

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

#expire_cacheObject



13
14
15
# File 'lib/ruby-settings/cached_settings.rb', line 13

def expire_cache
  cache_store.delete(cache_key)
end

#rewrite_cacheObject



9
10
11
# File 'lib/ruby-settings/cached_settings.rb', line 9

def rewrite_cache
  cache_store.write(cache_key, value)
end