Class: BackgroundCache::Config
- Inherits:
-
Object
- Object
- BackgroundCache::Config
- Defined in:
- lib/background_cache/config.rb
Class Method Summary collapse
- .caches ⇒ Object
-
.from_params(params) ⇒ Object
Find cache config from params.
- .from_params_and_fragment(params, fragment = nil) ⇒ Object
- .key ⇒ Object
- .load! ⇒ Object
-
.unique_cache_id(cache) ⇒ Object
Unique cache id for storing last expired time.
- .unload! ⇒ Object
Instance Method Summary collapse
- #cache(options) ⇒ Object
- #every(value, &block) ⇒ Object
- #except(value, &block) ⇒ Object
-
#initialize {|_self| ... } ⇒ Config
constructor
A new instance of Config.
- #layout(value, &block) ⇒ Object
- #only(value, &block) ⇒ Object
- #set_option(key, value, &block) ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Config
Returns a new instance of Config.
5 6 7 8 |
# File 'lib/background_cache/config.rb', line 5 def initialize(&block) @@caches = [] yield self end |
Class Method Details
.caches ⇒ Object
89 90 91 |
# File 'lib/background_cache/config.rb', line 89 def self.caches @@caches end |
.from_params(params) ⇒ Object
Find cache config from params
48 49 50 |
# File 'lib/background_cache/config.rb', line 48 def self.from_params(params) from_params_and_fragment(params) end |
.from_params_and_fragment(params, fragment = nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/background_cache/config.rb', line 51 def self.from_params_and_fragment(params, fragment=nil) unless @@caches.empty? @@caches.select { |item| # Basic params match (action, controller, etc) item[:params] == params && ( # No fragment specified fragment.nil? || ( ( # :only not defined !item[:only] || # :only matches fragment item[:only] == fragment || ( # :only is an array items[:only].respond_to?(:index) && # :only includes matching fragment items[:only].include?(fragment) ) ) && ( # :except not defined !item[:except] || # :except not explicitly named item[:except] != fragment || ( # :except is an array items[:except].respond_to?(:index) && # :except does not include matching fragment !items[:except].include?(fragment) ) ) ) ) }[0] end end |
.key ⇒ Object
92 93 94 |
# File 'lib/background_cache/config.rb', line 92 def self.key Digest::SHA256.hexdigest("--#{Time.now}--#{rand}--") end |
.load! ⇒ Object
95 96 97 |
# File 'lib/background_cache/config.rb', line 95 def self.load! load RAILS_ROOT + "/lib/background_cache_config.rb" end |
.unique_cache_id(cache) ⇒ Object
Unique cache id for storing last expired time
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/background_cache/config.rb', line 102 def self.unique_cache_id(cache) id = [] join = lambda do |k, v| id << (k.nil? || v.nil? ? nil : [ k, v ].collect { |kv| kv.to_s.gsub(/\W/, '_') }.join('-') ) end cache[:params].each do |key, value| join.call(key, value) end cache.each do |key, value| join.call(key, value) unless key == :params end 'background_cache/' + id.compact.join('/') end |
.unload! ⇒ Object
98 99 100 |
# File 'lib/background_cache/config.rb', line 98 def self.unload! @@caches = [] end |
Instance Method Details
#cache(options) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/background_cache/config.rb', line 9 def cache() # Convert keys to strings for param matching .dup.each do |key, value| [key.to_s] = value .delete(key) end # Method-style config = @options.merge() # Store the cache options @@caches.push({ :except => .delete('except'), :every => .delete('every') || 1.hour, :layout => .delete('layout'), :only => .delete('only'), :params => }) end |
#every(value, &block) ⇒ Object
29 30 31 |
# File 'lib/background_cache/config.rb', line 29 def every(value, &block) set_option(:every, value, &block) end |
#except(value, &block) ⇒ Object
26 27 28 |
# File 'lib/background_cache/config.rb', line 26 def except(value, &block) set_option(:except, value, &block) end |
#layout(value, &block) ⇒ Object
32 33 34 |
# File 'lib/background_cache/config.rb', line 32 def layout(value, &block) set_option(:layout, value, &block) end |
#only(value, &block) ⇒ Object
35 36 37 |
# File 'lib/background_cache/config.rb', line 35 def only(value, &block) set_option(:only, value, &block) end |
#set_option(key, value, &block) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/background_cache/config.rb', line 38 def set_option(key, value, &block) @options ||= {} @options[key.to_s] = value if block yield @options = {} end self end |