Class: Backup::Storage::Object

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/storage/object.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Object

Instantiates a new Backup::Storage::Object and stores the full path to the storage file (yaml) in the @storage_file attribute



14
15
16
# File 'lib/backup/storage/object.rb', line 14

def initialize(type)
  @storage_file = File.join(DATA_PATH, TRIGGER, "#{type}.yml")
end

Instance Attribute Details

#storage_fileObject

Holds the type attribute



9
10
11
# File 'lib/backup/storage/object.rb', line 9

def storage_file
  @storage_file
end

Instance Method Details

#loadObject

Tries to load an existing YAML file and returns an array of storage objects. If no file exists, an empty array gets returned

If a file is loaded it’ll sort the array of objects by @time descending. The newest backup storage object comes in Backup::Storage::Object.load and the oldest in Backup::Storage::Object.load



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

def load
  if File.exist?(storage_file)
    YAML.load_file(storage_file).sort { |a,b| b.time <=> a.time }
  else
    []
  end
end

#write(objects) ⇒ Object

Takes the provided objects and converts it to YAML format. The YAML data gets written to the storage file



37
38
39
40
41
# File 'lib/backup/storage/object.rb', line 37

def write(objects)
  File.open(storage_file, 'w') do |file|
    file.write(objects.to_yaml)
  end
end