class Kinksync::File2Sync

Class that represents a duple of files with absolute path which can be synced

Public Class Methods

new(file) click to toggle source

Configures a File2Sync class

@param file [String] local or remote file to sync

# File lib/kinksync/file_2_sync.rb, line 12
def initialize(file)
  @file = file
  @twin_file = twin_file(file)
end

Public Instance Methods

sync() click to toggle source

Sync a file, copying origin over destination

@return file or nil if file is already synced

# File lib/kinksync/file_2_sync.rb, line 22
def sync
  if File.exist?(@twin_file) && FileUtils.identical?(@file, @twin_file)
    nil
  else
    origin = newer
    destination = twin_file(origin)
    FileUtils.mkdir_p(File.dirname(@twin_file))
    FileUtils.cp(origin, destination)
    @file
  end
end

Private Instance Methods

newer() click to toggle source

Return newer file

# File lib/kinksync/file_2_sync.rb, line 39
def newer
  FileUtils.uptodate?(@file, [@twin_file]) ? @file : @twin_file
end
remote?(file) click to toggle source

Decides whether a file is remote or local

@param file [String]

# File lib/kinksync/file_2_sync.rb, line 61
def remote?(file)
  File.dirname(file).start_with?(Kinksync.configuration.remote_path)
end
twin_file(file) click to toggle source

Gets twin file of file provided

@param file [String] file to get twin from

# File lib/kinksync/file_2_sync.rb, line 48
def twin_file(file)
  if remote? file
    file.sub(Kinksync.configuration.remote_path, '')
  else
    Kinksync.configuration.remote_path + file
  end
end