Class: System::CacheFile

Inherits:
Object
  • Object
show all
Includes:
Logue::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.



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

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

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

Instance Method Details

#read_fileObject



28
29
30
31
32
33
# File 'lib/system/command/cachefile.rb', line 28

def read_file
  Zlib::GzipReader.open(@pn.to_s) do |gz|
    @output = gz.readlines
  end
  @output
end

#readlinesObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/system/command/cachefile.rb', line 35

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

#save_fileObject



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

def save_file
  @pn.parent.mkpath unless @pn.parent.exist?
  @pn.unlink if @pn.exist?
  Zlib::GzipWriter.open(@pn.to_s) do |gz|
    gz.puts @output
  end
end

#to_sObject



46
47
48
# File 'lib/system/command/cachefile.rb', line 46

def to_s
  @pn.to_s
end