Class: Resque::Reports::CacheFile

Inherits:
Object
  • Object
show all
Includes:
Extensions::Encodings
Defined in:
lib/resque/reports/cache_file.rb

Overview

Class describes how to storage and access cache file NOTE: Every time any cache file is opening,

cache is cleared from old files.

Constant Summary collapse

DEFAULT_EXPIRE_TIME =
86_400
DEFAULT_CODING =
UTF8

Constants included from Extensions::Encodings

Extensions::Encodings::CP1251, Extensions::Encodings::UTF8

Instance Method Summary collapse

Constructor Details

#initialize(dir, filename, options = {}) ⇒ CacheFile

Returns a new instance of CacheFile.



14
15
16
17
18
19
20
21
22
# File 'lib/resque/reports/cache_file.rb', line 14

def initialize(dir, filename, options = {})
  @dir = dir
  @filename = File.join(dir, filename)
  @ext = File.extname(filename)

  # options
  @coding = options[:coding] || DEFAULT_CODING
  @expiration_time = options[:expire_in] || DEFAULT_EXPIRE_TIME
end

Instance Method Details

#clearObject



48
49
50
# File 'lib/resque/reports/cache_file.rb', line 48

def clear
  FileUtils.rm_f(@filename)
end

#exists?Boolean Also known as: ready?

Returns:

  • (Boolean)


24
25
26
# File 'lib/resque/reports/cache_file.rb', line 24

def exists?
  !expired?(@filename)
end

#filenameObject



29
30
31
32
# File 'lib/resque/reports/cache_file.rb', line 29

def filename
  fail 'File doesn\'t exists, check exists? before' unless exists?
  @filename
end

#open(force = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/resque/reports/cache_file.rb', line 34

def open(force = false)
  prepare_cache_dir

  (force ? clear : return) if File.exists?(@filename)

  with_tempfile do |tempfile|
    yield tempfile

    tempfile.close
    FileUtils.cp(tempfile.path, @filename)
    FileUtils.chmod(0644, @filename)
  end
end