class Dutiful::Storage

Attributes

name[R]
storage_path[R]

Public Class Methods

new(name: nil, path: nil) click to toggle source
# File lib/dutiful/storage.rb, line 7
def initialize(name: nil, path: nil)
  name ||= 'Custom folder'
  path ||= Tomlrb.load_file("#{Dutiful.dir}/config/#{name.downcase}.toml", symbolize_keys: true)[:storage][:path]

  @name         = name
  @storage_path = File.expand_path path
  @path         = "#{@storage_path}/dutiful"
end

Private Class Methods

all() click to toggle source
# File lib/dutiful/storage.rb, line 76
def self.all
  Dir["#{Dutiful.dir}/config/*.toml"].map do |filename|
    data = Tomlrb.load_file(filename, symbolize_keys: true)[:storage]
    Dutiful::Storage.new name: data[:name], path: data[:path]
  end.compact
end
each() { |storage| ... } click to toggle source
# File lib/dutiful/storage.rb, line 83
def self.each
  return enum_for(:each) unless block_given?

  all.each { |storage| yield storage }
end
find(if_none = nil, &block) click to toggle source
# File lib/dutiful/storage.rb, line 89
def self.find(if_none = nil, &block)
  result = nil
  found  = false

  each do |element|
    if block.call(element)
      result = element
      found = true
      break
    end
  end

  found ? result : if_none && if_none.call
end

Public Instance Methods

available?() click to toggle source
# File lib/dutiful/storage.rb, line 16
def available?
  File.exist? storage_path
end
backup(file) click to toggle source
# File lib/dutiful/storage.rb, line 37
def backup(file)
  create_dir file
  Rsync.run file.full_path.shellescape, file.backup_path.shellescape, '--recursive'
end
create_dir(file) click to toggle source
# File lib/dutiful/storage.rb, line 20
def create_dir(file)
  FileUtils.mkdir_p File.dirname "#{path}/#{file.path}"
  FileUtils.mkdir_p File.dirname "#{file.full_path}"
end
exist?(file) click to toggle source
# File lib/dutiful/storage.rb, line 25
def exist?(file)
  File.exist? "#{path}/#{file.path}"
end
in_sync?(file) click to toggle source
# File lib/dutiful/storage.rb, line 59
def in_sync?(file)
  if file.directory?
    Dir.glob("#{file.full_path}*").all? do |file_path|
      filename = File.basename(file_path)
      file_backup_path = path "#{file.path}#{filename}"

      File.exist?(file_path) &&
        File.exist?(file_backup_path) &&
        FileUtils.identical?(file_path, file_backup_path)
    end
  else
    FileUtils.identical? file.full_path, "#{path}/#{file.path}"
  end
end
path(path = nil) click to toggle source
# File lib/dutiful/storage.rb, line 29
def path(path = nil)
  if path
    "#{@path}/#{path}"
  else
    @path
  end
end
restore(file) click to toggle source
# File lib/dutiful/storage.rb, line 42
def restore(file)
  create_dir file
  Rsync.run file.backup_path.shellescape, file.full_path.shellescape, '--recursive'
end
sync(file) click to toggle source
# File lib/dutiful/storage.rb, line 47
def sync(file)
  if file.exist?
    if file.has_backup? && file.backup_timestamp > file.timestamp
      restore file
    else
      backup file
    end
  else
    restore file
  end
end