Class: RailsSettings::Base

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

Direct Known Subclasses

ScopedSettings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Settings

destroy, get_all, merge!, method_missing, object, rails_initialized?, source, thing_scoped, #value, #value=, where

Class Method Details

.[](key) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/rails-settings/base.rb', line 28

def [](key)
  return super(key) unless rails_initialized?
  val = Rails.cache.fetch(cache_key(key, @object)) do
    super(key)
  end
  val
end

.[]=(var_name, value) ⇒ Object

set a setting value by [] notation



37
38
39
40
41
# File 'lib/rails-settings/base.rb', line 37

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

.cache_key(var_name, scope_object) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/rails-settings/base.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.to_s
  scope.join('/')
end

.cache_prefix(&block) ⇒ Object



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

def cache_prefix(&block)
  @cache_prefix = block
end

Instance Method Details

#cache_keyObject



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

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

#expire_cacheObject



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

def expire_cache
  Rails.cache.delete(cache_key)
end

#rewrite_cacheObject



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

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