Class: RailsSettings::Base

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

Direct Known Subclasses

CachedSettings, 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



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

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



43
44
45
46
47
48
49
# File 'lib/rails-settings/base.rb', line 43

def []=(var_name, value)
  super

  Rails.cache.write(cache_key(var_name, @object),value)

  value
end

.cache_key(var_name, scope_object) ⇒ Object



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

def cache_key(var_name, scope_object)
  scope = ['rails_settings_cached', cache_prefix_by_startup]
  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



22
23
24
# File 'lib/rails-settings/base.rb', line 22

def cache_prefix(&block)
  @cache_prefix = block
end

.cache_prefix_by_startupObject



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

def cache_prefix_by_startup
  return @cache_prefix_by_startup if defined? @cache_prefix_by_startup
  return '' unless Default.enabled?
  @cache_prefix_by_startup = Digest::MD5.hexdigest(Default.instance.to_s)
end

.save_default(key, value) ⇒ Object



51
52
53
54
55
56
# File 'lib/rails-settings/base.rb', line 51

def save_default(key, value)
  Kernel.warn 'DEPRECATION WARNING: RailsSettings save_default is deprecated and it will removed in 0.7.0. ' <<
              'Please use YAML file for default setting.'
  return false unless self[key].nil?
  self[key] = value
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