Class: BackgroundCache::Config

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Config

Returns a new instance of Config.



8
9
10
11
# File 'lib/background_cache/config.rb', line 8

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

Class Attribute Details

.current_cacheObject

Returns the value of attribute current_cache.



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

def current_cache
  @current_cache
end

.groupObject

Returns the value of attribute group.



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

def group
  @group
end

Class Method Details

.build_cache(options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/background_cache/config.rb', line 54

def self.build_cache(options)
  {
    :except => options.delete(:except),
    :group => options.delete(:group),
    :layout => options.delete(:layout),
    :only => options.delete(:only),
    :path => options.delete(:path),
    :params => options
  }
end

.cachesObject



102
103
104
# File 'lib/background_cache/config.rb', line 102

def self.caches
  defined?(@@caches) ? @@caches : []
end

.load!(group = nil) ⇒ Object



106
107
108
109
110
# File 'lib/background_cache/config.rb', line 106

def self.load!(group=nil)
  self.group = group
  load RAILS_ROOT + "/lib/background_cache_config.rb"
  self.group = nil
end

.match?(fragment = {}) ⇒ Boolean

Does controller and fragment match current cache?

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/background_cache/config.rb', line 66

def self.match?(fragment={})
  cache = self.current_cache
  cache &&
  # Basic params match (action, controller, etc)
  (
    # No fragment specified
    fragment.empty? ||
    (
      (
        # :only not defined
        !cache[:only] ||
        # :only matches fragment
        cache[:only] == fragment ||
        (
          # :only is an array
          cache[:only].respond_to?(:compact) &&
          # :only includes matching fragment
          cache[:only].include?(fragment)
        )
      ) &&
      (
        # :except not defined
        !cache[:except] ||
        # :except not explicitly named
        cache[:except] != fragment ||
        (
          # :except is an array
          cache[:except].respond_to?(:compact) &&
          # :except does not include matching fragment
          !cache[:except].include?(fragment)
        )
      )
    )
  )
end

.unload!Object



112
113
114
115
# File 'lib/background_cache/config.rb', line 112

def self.unload!
  @@caches = []
  self.current_cache = nil
end

Instance Method Details

#cache(options) ⇒ Object



13
14
15
16
17
# File 'lib/background_cache/config.rb', line 13

def cache(options)
  @options ||= []
  options = @options.inject({}) { |hash, option| hash.merge(option) }.merge(options)
  @@caches << self.class.build_cache(options)
end

#except(value, &block) ⇒ Object



19
20
21
# File 'lib/background_cache/config.rb', line 19

def except(value, &block)
  set_option({ :except => value }, &block)
end

#group(value, &block) ⇒ Object



23
24
25
26
27
# File 'lib/background_cache/config.rb', line 23

def group(value, &block)
  if self.class.group.nil? || value == self.class.group
    set_option({ :group => value }, &block)
  end
end

#layout(value, &block) ⇒ Object



29
30
31
# File 'lib/background_cache/config.rb', line 29

def layout(value, &block)
  set_option({ :layout => value }, &block)
end

#only(value, &block) ⇒ Object



33
34
35
# File 'lib/background_cache/config.rb', line 33

def only(value, &block)
  set_option({ :only => value }, &block)
end

#set_option(hash, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/background_cache/config.rb', line 37

def set_option(hash, &block)
  @options ||= []
  @options << hash
  if block
    @last_option_index ||= []
    @last_option_index << (@options.length - 1)
    yield
    @last_option_index.pop
    if @last_option_index.empty?
      @options = []
    else
      @options = @options[0..@last_option_index.last]
    end
  end
  self
end