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.

Instance Method Summary collapse

Constructor Details

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

Constructor



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

def initialize(options = {}, &block)
  @group = options.fetch(:group, :default)
  @spoor = "generated by cache_cache"
  @seed = options.fetch(:seed, "")

  @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
37
# File 'lib/cache_cache.rb', line 29

def cache(entry = nil)
  @cache[@group] ||= []

  unless entry.nil?
    @cache[@group] << entry
  end

   @cache[@group].flatten
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



53
54
55
56
57
58
59
60
61
# File 'lib/cache_cache.rb', line 53

def fallback(entry = nil)
  @fallback[@group] ||= []

  unless entry.nil?
    @fallback[@group] << entry
  end

  @fallback[@group].flatten
end

#generateObject

Generate the manifest



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

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

#group(group_name = nil) ⇒ Object

Get / Set the current group



87
88
89
90
# File 'lib/cache_cache.rb', line 87

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

#labelObject

Get label



110
111
112
# File 'lib/cache_cache.rb', line 110

def label
  "cache_cache_#{@group}"
end

#label_seedObject

Get label seed



116
117
118
# File 'lib/cache_cache.rb', line 116

def label_seed
  "cache_cache_seed_#{@group}"
end

#manifestObject

Get the manifest



65
66
67
# File 'lib/cache_cache.rb', line 65

def manifest
  return self.generate
end

#network(entry = nil) ⇒ Object

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



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

def network(entry = nil)
  @network[@group] ||= []

  unless entry.nil?
    @network[@group] << entry
  end

  @network[@group].flatten
end

#seed(new_seed = nil) ⇒ Object

Get the seed of the manifest



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

def seed(new_seed = nil)
  @seed = new_seed unless new_seed.nil?

  @seed
end

#spoor(spoor_content = nil) ⇒ Object

Get the spoor of the manifest



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

def spoor(spoor_content = nil)
  @spoor = spoor_content unless spoor_content.nil?

  @spoor
end

#to_sObject

Get the manifest



122
123
124
# File 'lib/cache_cache.rb', line 122

def to_s
  self.manifest
end