Class: RailsSettings::Base

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

Direct Known Subclasses

ScopedSettings

Defined Under Namespace

Classes: Configuration

Class Attribute Summary collapse

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 Attribute Details

.configurationObject

Returns the value of attribute configuration.



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

def configuration
  @configuration
end

Class Method Details

.[](key) ⇒ Object



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

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

.[]=(var_name, value) ⇒ Object

set a setting value by [] notation



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

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

.cache_key(var_name, scope_object) ⇒ Object



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

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



24
25
26
# File 'lib/rails-settings/base.rb', line 24

def cache_prefix(&block)
  @cache_prefix = block
end

.cache_storeObject



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

def cache_store
  @cache_store ||= RailsSettings::Base.configuration.cache_store
end

.configure {|configuration| ... } ⇒ Object

Yields:



19
20
21
22
# File 'lib/rails-settings/base.rb', line 19

def configure
  self.configuration ||= Configuration.new
  yield(configuration)
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
  self.class.configuration.cache_store.delete(cache_key)
end

#rewrite_cacheObject



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

def rewrite_cache
  self.class.configuration.cache_store.fetch(cache_key, value)
end