Class: DatedBackup::Core::BackupSet

Inherits:
ReverseSortedUniqueArray show all
Defined in:
lib/dated_backup/core/backup_set.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ReverseSortedUniqueArray

#initialize

Methods inherited from Array

#car, #cdr, #to_backup_set

Constructor Details

This class inherits a constructor from ReverseSortedUniqueArray

Class Method Details

.create_dynamic_time_methods(time_array = []) ⇒ Object

Creates the many similar time methods:

  • include_year?

  • include_month?

  • include_day?

  • include_week

  • one_per_year

  • one_per_month

  • one_per_day

  • one_per_week



56
57
58
59
# File 'lib/dated_backup/core/backup_set.rb', line 56

def create_dynamic_time_methods(time_array=[])
  create_include_time_boolean_methods *time_array
  create_one_per_time_methods *time_array
end

.create_include_time_boolean_methods(*methods) ⇒ Object

Creates the boolean include_*? methods (include_month?, include_year? and so on). See the notes on the create_per_time_methods, and TimeSymbol.valid_symbols



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dated_backup/core/backup_set.rb', line 17

def create_include_time_boolean_methods(*methods)
  methods.each do |method|
    define_method "include_#{method}?" do |time_value|
      truth_value = false
      self.each_as_time do |t|
        truth_value = true if t.send(method) == time_value
      end
      truth_value
    end
  end
end

.create_one_per_time_methods(*methods) ⇒ Object

Creates the one_per_* (one_per_month, one_per_year, one_per_week, and one_per_day) methods. Each of those methods will call the appropriate include_* methods, which is also dynamically defined



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dated_backup/core/backup_set.rb', line 32

def create_one_per_time_methods(*methods)
  methods.each do |method|
    define_method "one_per_#{method}" do 
      set = BackupSet.new
      reject_with_string_and_timestamp do |string, timestamp|
        set.push string unless set.send("include_#{method}?", timestamp.send("#{method}"))
      end
      set
    end      
  end
end

.find_files_in_directory(dir) ⇒ Object

Given the base of the backup directories as a string, this method should find all of the Backup Directories, and return these Directories as a BackupSet



10
11
12
13
# File 'lib/dated_backup/core/backup_set.rb', line 10

def find_files_in_directory(dir)
  raise InvalidDirectoryError, "A valid directory must be given." unless File.directory?(dir)
  new(Dir.glob "#{dir}/*")
end

Instance Method Details

#each_as_time(&blk) ⇒ Object



102
103
104
105
106
# File 'lib/dated_backup/core/backup_set.rb', line 102

def each_as_time &blk
  self.each do |obj|
    yield obj.to_time
  end
end

#filter_by_range(time_range) ⇒ Object



84
85
86
87
88
# File 'lib/dated_backup/core/backup_set.rb', line 84

def filter_by_range(time_range)
  reject_with_timestamp do |timestamp| 
    !(time_range.include? timestamp)
  end
end

#filter_by_rule(rule) ⇒ Object



64
65
66
67
68
69
# File 'lib/dated_backup/core/backup_set.rb', line 64

def filter_by_rule(rule)
  obj = self.dup
  obj = obj.filter_by_range(rule[:constraint]) if rule[:constraint]
  obj = obj.filter_by_scope(rule[:scope]) if rule[:scope]
  return obj
end

#filter_by_scope(scope) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/dated_backup/core/backup_set.rb', line 71

def filter_by_scope(scope)
  case scope
  when :yearly
    one_per_year
  when :monthly
    one_per_month
  when :weekly
    one_per_week
  when :daily
    one_per_day
  end    
end

#reject_with_string_and_timestamp(&blk) ⇒ Object



96
97
98
99
100
# File 'lib/dated_backup/core/backup_set.rb', line 96

def reject_with_string_and_timestamp &blk
  reject do |element|
    yield element, element.to_time
  end
end

#reject_with_timestamp(&blk) ⇒ Object



90
91
92
93
94
# File 'lib/dated_backup/core/backup_set.rb', line 90

def reject_with_timestamp &blk
  reject do |element|
    yield element.to_time
  end
end