Class: Ruby::Reports::CacheFile
- Inherits:
-
Object
- Object
- Ruby::Reports::CacheFile
- Defined in:
- lib/ruby/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 =
'utf-8'.freeze
Instance Attribute Summary collapse
-
#coding ⇒ Object
readonly
Returns the value of attribute coding.
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#expiration_time ⇒ Object
readonly
Returns the value of attribute expiration_time.
-
#ext ⇒ Object
readonly
Returns the value of attribute ext.
Instance Method Summary collapse
- #clear ⇒ Object
- #exists? ⇒ Boolean (also: #ready?)
- #filename ⇒ Object
-
#initialize(dir, filename, options = {}) ⇒ CacheFile
constructor
A new instance of CacheFile.
- #open(force = false) ⇒ Object
Constructor Details
#initialize(dir, filename, options = {}) ⇒ CacheFile
Returns a new instance of CacheFile.
13 14 15 16 17 18 19 20 21 |
# File 'lib/ruby/reports/cache_file.rb', line 13 def initialize(dir, filename, = {}) @dir = dir @filename = File.join(dir, filename) @ext = File.extname(@filename) # options @coding = [:coding] || DEFAULT_CODING @expiration_time = [:expire_in] || DEFAULT_EXPIRE_TIME end |
Instance Attribute Details
#coding ⇒ Object (readonly)
Returns the value of attribute coding.
12 13 14 |
# File 'lib/ruby/reports/cache_file.rb', line 12 def coding @coding end |
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
12 13 14 |
# File 'lib/ruby/reports/cache_file.rb', line 12 def dir @dir end |
#expiration_time ⇒ Object (readonly)
Returns the value of attribute expiration_time.
12 13 14 |
# File 'lib/ruby/reports/cache_file.rb', line 12 def expiration_time @expiration_time end |
#ext ⇒ Object (readonly)
Returns the value of attribute ext.
12 13 14 |
# File 'lib/ruby/reports/cache_file.rb', line 12 def ext @ext end |
Instance Method Details
#clear ⇒ Object
47 48 49 |
# File 'lib/ruby/reports/cache_file.rb', line 47 def clear FileUtils.rm_f(@filename) end |
#exists? ⇒ Boolean Also known as: ready?
23 24 25 |
# File 'lib/ruby/reports/cache_file.rb', line 23 def exists? !expired?(@filename) end |
#filename ⇒ Object
28 29 30 31 |
# File 'lib/ruby/reports/cache_file.rb', line 28 def filename fail 'File doesn\'t exist, check exists? before' unless exists? @filename end |
#open(force = false) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ruby/reports/cache_file.rb', line 33 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 |