Class: BackgroundCache::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/background_cache/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Config

Returns a new instance of Config.

Yields:

  • (_self)

Yield Parameters:



5
6
7
8
# File 'lib/background_cache/config.rb', line 5

def initialize(&block)
  @@caches = []
  yield self
end

Class Method Details

.cachesObject



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

.keyObject



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(options)
  # Convert keys to strings for param matching
  options.dup.each do |key, value|
    options[key.to_s] = value
    options.delete(key)
  end
  # Method-style config
  options = @options.merge(options)
  # Store the cache options
  @@caches.push({
    :except => options.delete('except'),
    :every => options.delete('every') || 1.hour,
    :layout => options.delete('layout'),
    :only => options.delete('only'),
    :params => options
  })
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