Class: StaticCache

Inherits:
Object
  • Object
show all
Defined in:
app/models/static_cache.rb

Constant Summary collapse

CACHE_PATH =
File.join Rails.root, "public/cache/static"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ StaticCache

Returns a new instance of StaticCache.



6
7
8
# File 'app/models/static_cache.rb', line 6

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



2
3
4
# File 'app/models/static_cache.rb', line 2

def name
  @name
end

Class Method Details

.allObject



27
28
29
30
31
# File 'app/models/static_cache.rb', line 27

def all
  cache_files.map do |name|
    StaticCache.new name
  end
end

.cache_files(options = {}) ⇒ Object



33
34
35
36
37
38
39
# File 'app/models/static_cache.rb', line 33

def cache_files(options = {})
  Dir.glob(File.join(CACHE_PATH, "**/*"), File::FNM_DOTMATCH).map do |file|
    next if file =~ /\.gz$/ unless options[:with_gz]
    next if File.directory? file
    file.sub "#{CACHE_PATH}/", ""
  end.compact.sort.uniq
end

.expire_all!Object



41
42
43
44
45
# File 'app/models/static_cache.rb', line 41

def expire_all!
  files = Dir.glob File.join(CACHE_PATH, "**/*"), File::FNM_DOTMATCH
  files.reject! { |file| File.directory? file }
  File.delete *files unless files.empty?
end

.find(name) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


20
21
22
23
24
25
# File 'app/models/static_cache.rb', line 20

def find(name)
  name = "" if name == "INDEX"
  actual = cache_files.select { |x| x == name }.first
  raise ActiveRecord::RecordNotFound.new("No records found!") unless actual
  StaticCache.new actual
end

Instance Method Details

#expire!Object



10
11
12
13
14
15
16
17
# File 'app/models/static_cache.rb', line 10

def expire!
  gz_name = "#{name}.gz"
  StaticCache.cache_files(:with_gz => true).select do |file|
    file == name || file == gz_name
  end.each do |file|
    File.delete File.join(CACHE_PATH, file)
  end
end