Sha256: 6f4af0343dba25d25116861bfecce823d43fd15eb5d7bea05e5404d6b484af38

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'forwardable'

module Expire
  # All Backups go here
  class BackupList
    include Enumerable
    extend Forwardable

    def initialize(backups = [])
      # @backups = backups.sort.reverse
      @backups = backups
    end

    attr_reader :backups

    def_delegators :backups, :each, :empty?, :last, :length, :<<

    def one_per(noun)
      backups_per_noun = self.class.new
      return backups_per_noun unless any?

      reversed = sort.reverse

      backups_per_noun << reversed.first

      message = "same_#{noun}?"

      reversed.each do |backup|
        backups_per_noun << backup unless backup.send(message, backups_per_noun.last)
      end

      backups_per_noun
    end

    def most_recent(amount = 1)
      self.class.new(sort.reverse.first(amount))
    end

    def newest
      backups.max
    end

    def oldest
      backups.min
    end

    def not_older_than(reference_datetime)
      sort.select { |backup| backup.datetime >= reference_datetime }
    end

    def expired
      self.class.new(backups.select(&:expired?))
    end

    def expired_count
      expired.length
    end

    def keep
      self.class.new(backups.select(&:keep?))
    end

    def keep_count
      keep.length
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
expire-0.2.0 lib/expire/backup_list.rb