Class: System::CacheFile

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/system/command/cachefile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir, args) ⇒ CacheFile

Returns a new instance of CacheFile.



13
14
15
16
17
18
19
# File 'lib/system/command/cachefile.rb', line 13

def initialize cache_dir, args
  info "cache_dir: #{cache_dir}"
  @args = args
  @pn = Pathname.new(cache_dir) + (args.join('-').gsub('/', '_slash_') + '.gz')
  info "pn: #{@pn}"
  @output = nil
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



11
12
13
# File 'lib/system/command/cachefile.rb', line 11

def output
  @output
end

Instance Method Details

#read_fileObject



30
31
32
33
34
35
36
# File 'lib/system/command/cachefile.rb', line 30

def read_file
  info "reading from cache file: #{@pn}"
  Zlib::GzipReader.open(@pn.to_s) do |gz|
    @output = gz.readlines
  end
  @output
end

#readlinesObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/system/command/cachefile.rb', line 38

def readlines
  if @pn.exist?
    read_file
  else
    cl = CommandLine.new @args
    @output = cl.execute
    save_file
    @output
  end
end

#save_fileObject



21
22
23
24
25
26
27
28
# File 'lib/system/command/cachefile.rb', line 21

def save_file
  @pn.parent.mkpath unless @pn.parent.exist?
  @pn.unlink if @pn.exist?
  info "saving output to cache file: #{@pn}"
  Zlib::GzipWriter.open(@pn.to_s) do |gz|
    gz.puts @output
  end
end

#to_sObject



49
50
51
# File 'lib/system/command/cachefile.rb', line 49

def to_s
  @pn.to_s
end