Class: Expire::Backup

Inherits:
Delegator
  • Object
show all
Includes:
Comparable
Defined in:
lib/expire/backup.rb

Overview

Representation of a single backup

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time:, pathname:) ⇒ Backup

Returns a new instance of Backup.



8
9
10
11
12
13
14
15
# File 'lib/expire/backup.rb', line 8

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

Instance Attribute Details

#pathnameObject (readonly)

Returns the value of attribute pathname.



17
18
19
# File 'lib/expire/backup.rb', line 17

def pathname
  @pathname
end

#reasons_to_keepObject (readonly)

Returns the value of attribute reasons_to_keep.



17
18
19
# File 'lib/expire/backup.rb', line 17

def reasons_to_keep
  @reasons_to_keep
end

#timeObject (readonly) Also known as: __getobj__

Returns the value of attribute time.



17
18
19
# File 'lib/expire/backup.rb', line 17

def time
  @time
end

Instance Method Details

#<=>(other) ⇒ Object

The <=> method seems not to be delegated so we need to implement it Note that this Class includes the Comparable module



54
55
56
# File 'lib/expire/backup.rb', line 54

def <=>(other)
  time <=> other.time
end

#add_reason_to_keep(reason) ⇒ Object



58
59
60
# File 'lib/expire/backup.rb', line 58

def add_reason_to_keep(reason)
  reasons_to_keep << reason
end

#cweekObject

def time

backup.time

end



66
67
68
# File 'lib/expire/backup.rb', line 66

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

#expired?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/expire/backup.rb', line 70

def expired?
  reasons_to_keep.empty?
end

#keep?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/expire/backup.rb', line 74

def keep?
  reasons_to_keep.any?
end

#same_day?(other) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/expire/backup.rb', line 27

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

  false
end

#same_hour?(other) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/expire/backup.rb', line 20

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

  false
end

#same_month?(other) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/expire/backup.rb', line 41

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

  false
end

#same_week?(other) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/expire/backup.rb', line 34

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

  false
end

#same_year?(other) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/expire/backup.rb', line 48

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

#to_sObject



78
79
80
# File 'lib/expire/backup.rb', line 78

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