Class: Thinning::Cleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/thinning/cleaner.rb

Constant Summary collapse

HOUR =
3600
DAY =
24 * HOUR
TYPE_MAPPING =
{
  :hourly => 1 * HOUR,
  :daily=> 24 * HOUR,
  :weekly => 7 * DAY,
  :monthly => 30 * DAY,
  :yearly => 365 * DAY
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, pattern, now = Time.now) ⇒ Cleaner

Returns a new instance of Cleaner.



3
4
5
6
7
# File 'lib/thinning/cleaner.rb', line 3

def initialize(files, pattern, now = Time.now)
  @files = files
  @pattern = pattern
  @now = now
end

Class Method Details

.select_files_to_delete(files, timestamps) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/thinning/cleaner.rb', line 31

def select_files_to_delete(files, timestamps)
  tmp_file = files.clone
  timestamps.each do |ts|
    if file = tmp_file.sort_by { |f| (f.timestamp - ts).abs }.first
      tmp_file.delete(file)
    end
  end
  tmp_file
end

.timestamps_for_pattern(pattern, now = Time.now) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/thinning/cleaner.rb', line 41

def timestamps_for_pattern(pattern, now = Time.now)
  stamps = []
  pattern.each_pair do |type, duration|
    current = now
    step = TYPE_MAPPING.fetch(type)
    while current > now - duration
      stamps << current
      current -= step
    end
  end
  stamps.uniq
end

Instance Method Details

#files_to_deleteObject



9
10
11
# File 'lib/thinning/cleaner.rb', line 9

def files_to_delete
  self.class.select_files_to_delete(@files, timestamps)
end