Class: Expire::BackupList

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/expire/backup_list.rb

Overview

All Backups go here

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backups = []) ⇒ BackupList

Returns a new instance of BackupList.



11
12
13
14
# File 'lib/expire/backup_list.rb', line 11

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

Instance Attribute Details

#backupsObject (readonly)

Returns the value of attribute backups.



16
17
18
# File 'lib/expire/backup_list.rb', line 16

def backups
  @backups
end

Instance Method Details

#expiredObject



53
54
55
# File 'lib/expire/backup_list.rb', line 53

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

#expired_countObject



57
58
59
# File 'lib/expire/backup_list.rb', line 57

def expired_count
  expired.length
end

#keepObject



61
62
63
# File 'lib/expire/backup_list.rb', line 61

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

#keep_countObject



65
66
67
# File 'lib/expire/backup_list.rb', line 65

def keep_count
  keep.length
end

#most_recent(amount = 1) ⇒ Object



37
38
39
# File 'lib/expire/backup_list.rb', line 37

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

#newestObject



41
42
43
# File 'lib/expire/backup_list.rb', line 41

def newest
  backups.max
end

#not_older_than(reference_time) ⇒ Object



49
50
51
# File 'lib/expire/backup_list.rb', line 49

def not_older_than(reference_time)
  sort.select { |backup| backup.time >= reference_time }
end

#oldestObject



45
46
47
# File 'lib/expire/backup_list.rb', line 45

def oldest
  backups.min
end

#one_per(noun) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/expire/backup_list.rb', line 20

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