Class: BiblioTech::Backups::PruneList

Inherits:
Object
  • Object
show all
Defined in:
lib/bibliotech/backups/prune_list.rb

Constant Summary collapse

TIMESTAMP_REGEX =
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})_(?<hour>\d{2}):(?<minute>\d{2})/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, prefix) ⇒ PruneList

Returns a new instance of PruneList.



8
9
10
# File 'lib/bibliotech/backups/prune_list.rb', line 8

def initialize(path, prefix)
  @path, @prefix = path, prefix
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/bibliotech/backups/prune_list.rb', line 6

def path
  @path
end

#prefixObject

Returns the value of attribute prefix.



6
7
8
# File 'lib/bibliotech/backups/prune_list.rb', line 6

def prefix
  @prefix
end

Class Method Details

.filename_for(prefix, time) ⇒ Object



41
42
43
# File 'lib/bibliotech/backups/prune_list.rb', line 41

def self.filename_for(prefix, time)
  time.strftime("#{prefix}-%Y-%m-%d_%H:%M.sql")
end

Instance Method Details

#build_record(file) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bibliotech/backups/prune_list.rb', line 45

def build_record(file)
  if file =~ prefix_re(/.*/)
    if !(match = prefix_timestamp_re.match(file)).nil?
      timespec = %w{year month day hour minute}.map do |part|
        Integer(match[part], 10)
      end
      parsed_time = Time::utc(*timespec)
      return FileRecord.new(File::join(path, file), parsed_time)
    else
      raise "File prefixed #{prefix} doesn't match #{prefix_timestamp_re.inspect}: #{File::join(path, file)}"
    end
  else
    if file !~ TIMESTAMP_REGEX
      warn "Stray file in backups directory: #{File::join(path, file)}"
      return nil
    end
  end
  return nil
end

#listObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bibliotech/backups/prune_list.rb', line 12

def list
  files = []
  begin
    dir = Dir.new(path)
  rescue Errno::ENOENT
    return []
  end

  dir.each do |file|
    next if %w{. ..}.include?(file)
    file_record = build_record(file)
    if file_record.nil?
    else
      files << file_record
    end
  end
  files.sort_by do |record|
    - record.timestamp.to_i
  end
end

#prefix_re(also) ⇒ Object



37
38
39
# File 'lib/bibliotech/backups/prune_list.rb', line 37

def prefix_re(also)
  /\A#{prefix}-#{also}\..*\z/
end

#prefix_timestamp_reObject



33
34
35
# File 'lib/bibliotech/backups/prune_list.rb', line 33

def prefix_timestamp_re
  prefix_re(TIMESTAMP_REGEX)
end