Class: DeployLog::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/deploy_log/cache.rb

Defined Under Namespace

Classes: FileNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fmt, options = {}) ⇒ Cache

Returns a new instance of Cache.



11
12
13
14
15
16
17
# File 'lib/deploy_log/cache.rb', line 11

def initialize(fmt, options = {})
  fmt ||= 'deploy_%s.log'
  dir = options[:dir] || '/tmp'

  @repo = options[:repo]
  @file_name_template = "#{dir}/#{fmt}"
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



7
8
9
# File 'lib/deploy_log/cache.rb', line 7

def filename
  @filename
end

Instance Method Details

#contentsObject

Raises:



30
31
32
33
34
# File 'lib/deploy_log/cache.rb', line 30

def contents
  raise FileNotFound unless exists?

  File.read(@filename)
end

#create(*args) ⇒ Object



19
20
21
22
23
24
# File 'lib/deploy_log/cache.rb', line 19

def create(*args)
  hash = Digest::MD5.hexdigest(@repo + args.join('|'))
  path = FileUtils.touch format(@file_name_template, hash)

  @filename = path.first
end

#exists?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/deploy_log/cache.rb', line 26

def exists?
  File.exist?(@filename) && !File.size(@filename).zero?
end

#write_object(pool, message) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/deploy_log/cache.rb', line 36

def write_object(pool, message)
  File.open(@filename, 'w+') do |file|
    pool.each do |pr|
      line = yield(pr)

      file.write(line)
    end

    file.write "============================================================\n"
    file.write "#{message}\n"
    file.write "============================================================\n"
  end
end