class Eye::Rotator

Attributes

count[R]

options

:gzip => [true, false]
:min_size => [10 * 1024 * 1024, nil]
:count => 5
filename[R]

options

:gzip => [true, false]
:min_size => [10 * 1024 * 1024, nil]
:count => 5
gzip[R]

options

:gzip => [true, false]
:min_size => [10 * 1024 * 1024, nil]
:count => 5
min_size[R]

options

:gzip => [true, false]
:min_size => [10 * 1024 * 1024, nil]
:count => 5
options[R]

options

:gzip => [true, false]
:min_size => [10 * 1024 * 1024, nil]
:count => 5

Public Class Methods

new(filename, options = {}) click to toggle source
# File lib/eye-rotate/rotator.rb, line 36
def initialize(filename, options = {})
  @filename = File.expand_path(filename)
  @options = options

  @gzip = @options[:gzip]
  @min_size = @options[:min_size]
  @count = @options[:count] || 7
  @count = 1 if @count.to_i < 1
end

Public Instance Methods

rotate_if_needed() click to toggle source
# File lib/eye-rotate/rotator.rb, line 46
def rotate_if_needed
  if min_size
    rotate if file_size > min_size
  else
    rotate
  end
end

Private Instance Methods

file_size() click to toggle source
# File lib/eye-rotate/rotator.rb, line 56
def file_size
  File.size(filename) rescue -1
end
move(from_num, to_num) click to toggle source
# File lib/eye-rotate/rotator.rb, line 89
def move(from_num, to_num)
  FileUtils.mv(name(from_num), name(to_num))
rescue
end
name(num) click to toggle source
# File lib/eye-rotate/rotator.rb, line 60
def name(num)
  s = "#{filename}.#{num}"
  s += ".gz" if gzip
  s
end
rotate() click to toggle source
# File lib/eye-rotate/rotator.rb, line 66
def rotate
  return unless File.exists?(filename)

  @tmp_name = "#{filename}.tmp#{Time.now.to_f}"

  if gzip
    `#{gzip_cmd} -c '#{filename}' > '#{@tmp_name}'`
  else
    `#{cp_cmd} '#{filename}' '#{@tmp_name}'`
  end

  # truncate filename
  File.truncate(filename, 0)

  # delete last
  FileUtils.rm(name(count)) rescue nil

  # move files
  count.downto(1) { |i| move(i - 1, i) }

  FileUtils.mv(@tmp_name, name(1))
end