Sha256: 6c57d702ce26efa327b86db05f2b53442d126b602aed77ca86b12c7405b268fd

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

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

    def initialize(datetime:, pathname:)
      @datetime = datetime
      @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 :datetime, :pathname, :reasons_to_keep
    alias __getobj__ datetime

    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)
      datetime <=> other.datetime
    end

    def add_reason_to_keep(reason)
      reasons_to_keep << reason
    end

    # def datetime
    #   backup.datetime
    # end

    def expired?
      reasons_to_keep.empty?
    end

    def keep?
      reasons_to_keep.any?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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