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.



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

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

#exists?Boolean Also known as: ready?

Returns:

  • (Boolean)


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

def exists?
  File.exists?(@filename)
end

#filenameObject



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

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

#open(force = false) ⇒ Object



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

def open(force = false)
  prepare_cache_dir

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

  remove_unfinished_on_error do
    File.open(@filename, "w:#{@coding}") do |file|
      yield file
    end
  end
end