module Surrender

Constants

BACKUP_RE
DEFAULT_ARGUMENTS
VERSION

Public Class Methods

reject(filenames, options={}) click to toggle source

@return [Array<String>, Array<String>] The first argument returns filenames which did not match the regular expression.

The second argument returns filenames which matched the regular expression and
are candidates to be deleted.
# File lib/surrender.rb, line 20
def self.reject(filenames, options={})
  options = DEFAULT_ARGUMENTS.merge(options)
  extra_keys = options.keys - DEFAULT_ARGUMENTS.keys
  raise ArgumentError, "Unknown keys: #{extra_keys.inspect} -- won't proceed" unless extra_keys.empty?

  verbose = options[:verbose]

  policies = [
    Surrender::MostRecentPolicy.new(options.fetch(:most_recent)),
    Surrender::WeeklyPolicy.new(options.fetch(:weekly)),
    Surrender::MonthlyPolicy.new(options.fetch(:monthly)),
    Surrender::YearlyPolicy.new(options.fetch(:yearly)),
  ]

  all_files = filenames.map(&:to_s).map(&:chomp)
  valid_filenames = all_files.select{|fn| fn =~ BACKUP_RE}
  unprocessable = all_files - valid_filenames
  valid_filenames.each do |filename|
    filename =~ BACKUP_RE
    date = Date.new($1.to_i, $3.to_i, $4.to_i)

    policies.each do |policy|
      policy.add(filename, date)
    end
  end

  deleteable = valid_filenames.select do |filename|
    if verbose then
      message = policies.map do |policy|
        "#{policy.name}=#{policy.deleteable?(filename) ? "delete" : "keep"}"
      end.join(", ")
      STDERR.puts "#{filename.inspect}: #{message}"
    end

    policies.all?{|policy| policy.deleteable?(filename)}
  end

  [unprocessable, deleteable]
end