Sha256: c2c49e2e09d1d179564b9c39eb83adce2d4cca8ac1b23546429cba9dff9bee4f

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module Expire
  # Representation of a single backup
  class Backup < Delegator
    include Comparable

    def initialize(time:, pathname:)
      @time     = time
      @pathname = pathname

      # @reasons_to_keep is a Set so a reason can added multiple times
      # but appears only once
      @reasons_to_keep = Set.new
    end

    attr_reader :time, :pathname, :reasons_to_keep
    alias __getobj__ time

    def same_hour?(other)
      return false unless same_day?(other)
      return true if hour == other.hour

      false
    end

    def same_day?(other)
      return false unless same_week?(other)
      return true if day == other.day

      false
    end

    def same_week?(other)
      return false unless same_year?(other)
      return true if cweek == other.cweek

      false
    end

    def same_month?(other)
      return false unless same_year?(other)
      return true if month == other.month

      false
    end

    def same_year?(other)
      year == other.year
    end

    # The <=> method seems not to be delegated so we need to implement it
    # Note that this Class includes the Comparable module
    def <=>(other)
      time <=> other.time
    end

    def add_reason_to_keep(reason)
      reasons_to_keep << reason
    end

    # def time
    #   backup.time
    # end

    def cweek
      time&.strftime('%V').to_i
    end

    def expired?
      reasons_to_keep.empty?
    end

    def keep?
      reasons_to_keep.any?
    end

    def to_s
      time.strftime('%Y-%m-%dT%H:%M')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
expire-0.2.2 lib/expire/backup.rb
expire-0.2.1 lib/expire/backup.rb