Sha256: 137c7907c5dbac63f00d049c883c4bbcfb058dabdd6e9c577ea5a147a2d0f575

Contents?: true

Size: 639 Bytes

Versions: 4

Compression:

Stored size: 639 Bytes

Contents

module RailsSettings
  class CachedSettings < Settings
    after_update :rewrite_cache    
    after_create :rewrite_cache
    def rewrite_cache
      Rails.cache.write("settings:#{self.var}", self.value)
    end
    
    after_destroy { |record| Rails.cache.delete("settings:#{record.var}") }
    
    def self.[](var_name)
      cache_key = "settings:#{var_name}"
      obj = Rails.cache.fetch(cache_key) {
        super(var_name)
      }
      obj == nil ? @@defaults[var_name.to_s] : obj
    end    
    
    def self.save_default(key,value)
      if self.send(key) == nil
        self.send("#{key}=",value)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails-settings-cached-0.4.1 lib/rails-settings/cached_settings.rb
rails-settings-cached-0.4.0 lib/rails-settings/cached_settings.rb
rails-settings-cached-0.3.2 lib/rails-settings/cached_settings.rb
rails-settings-cached-0.3.1 lib/rails-settings/cached_settings.rb