Class: Rails::CacheCache

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

Overview

CacheCache allow you to manage dynamically multiple groups of HTML5 manifests. CacheCache use rails cache to store generated manifests.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ CacheCache

Constructor



10
11
12
13
14
15
16
17
18
19
# File 'lib/cache_cache.rb', line 10

def initialize(options = {}, &block)
  @memory_store = options.fetch(:memory_store, ActiveSupport::Cache::MemoryStore.new) 
  @group = options.fetch(:group, :default)

  @cache = { }
  @network = { }
  @fallback = { }

  self.configure(&block) if block_given?
end

Instance Method Details

#cache(entry = nil) ⇒ Object

Get / Set an entry into the cache section of the manifest



29
30
31
32
33
34
35
36
# File 'lib/cache_cache.rb', line 29

def cache(entry = nil)
  unless entry.nil?
    @cache[@group] ||= []
    @cache[@group] << entry
  end

  @cache[@group]
end

#configure(&block) ⇒ Object

Configure



23
24
25
# File 'lib/cache_cache.rb', line 23

def configure(&block)
  instance_eval(&block)
end

#fallback(entry = nil) ⇒ Object

Get / Set an entry into the fallback section of the manifest



51
52
53
54
55
56
57
58
# File 'lib/cache_cache.rb', line 51

def fallback(entry = nil)
  unless entry.nil?
    @fallback[@group] ||= []
    @fallback[@group] << entry
  end

  @fallback[@group]
end

#generateObject

Generate the manifest



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cache_cache.rb', line 74

def generate
  [
    'CACHE MANIFEST'  ,
    self.spoor        ,
    'CACHE:'          ,
    self.cache        ,
    'NETWORK:'        ,
    self.network      ,
    'FALLBACK:'       ,
    self.fallback     ,
  ].join("\n")
end

#group(group_name = nil) ⇒ Object

Get / Set the current group



89
90
91
92
# File 'lib/cache_cache.rb', line 89

def group(group_name = nil)
  @group = group_name unless group_name.nil?
  @group
end

#labelObject

Get label



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

def label
  "cache_cache_#{@group}"
end

#manifestObject

Get the manifest



68
69
70
# File 'lib/cache_cache.rb', line 68

def manifest
  return (@memory_store.read(self.label) or self.generate)
end

#network(entry = nil) ⇒ Object

Get / Set an entry into the network section of the manifest



40
41
42
43
44
45
46
47
# File 'lib/cache_cache.rb', line 40

def network(entry = nil)
  unless entry.nil?
    @network[@group] ||= []
    @network[@group] << entry
  end

  @network[@group]
end

#saveObject

Save the current manifest



62
63
64
# File 'lib/cache_cache.rb', line 62

def save
  @memory_store.write(self.label, self.manifest)
end

#spoorObject

Get the spoor of the manifest



96
97
98
# File 'lib/cache_cache.rb', line 96

def spoor
  "# generated by cache_cache"
end

#to_sObject

Get the manifest



108
109
110
# File 'lib/cache_cache.rb', line 108

def to_s
  self.manifest
end