class Savedatasync::Operator

Constants

Error

Public Class Methods

new(title, force, remote_dir_path) click to toggle source
# File lib/savedatasync.rb, line 82
def initialize(title, force, remote_dir_path)
  @title = title
  @force = force
  @remote_dir_path = remote_dir_path
  title_dir_path = File.expand_path(@remote_dir_path + '/' + @title).encode("UTF-8")
  unless File.directory?(@remote_dir_path)
    raise Error.new("remote dir #{@remote_dir_path} does not exist")
  end
  FileUtils.mkdir(title_dir_path) unless File.exist?(title_dir_path)
end

Public Instance Methods

cut(local_path, remote_filename) click to toggle source
# File lib/savedatasync.rb, line 151
def cut(local_path, remote_filename)
  remote_path = construct_remote_path(local_path, remote_filename)
  lstatus = local_status(local_path, remote_path)
  rstatus = remote_status(remote_path)
  unless lstatus == :valid_link && rstatus == :entity
    raise Error.new("cannot cut un-synced file")
  end
  FileUtils.rm_r(local_path)
  FileUtils.cp_r(remote_path, local_path)
  return true
end
get(local_path, remote_filename) click to toggle source
# File lib/savedatasync.rb, line 120
def get(local_path, remote_filename)
  remote_path = construct_remote_path(local_path, remote_filename)
  lstatus = local_status(local_path, remote_path)
  rstatus = remote_status(remote_path)
  case lstatus
  when :entity
    if rstatus == :empty
      raise Error.new("remote file '#{remote_path}' is empty")
    end
    unless @force
      raise Error.new("local file '#{local_path}' exists. use -f to force")
    end
    FileUtils.rm_rf(local_path)
    FileUtils.symlink(remote_path, local_path)
    return true
  when :empty
    if rstatus == :empty
      raise Error.new("remote file '#{remote_path}' is empty")
    end
    FileUtils.symlink(remote_path, local_path)
    return true
  when :valid_link
    if rstatus == :empty
      raise Error.new("local file '#{local_path}' is broken link. remove it to continue")
    end
    return true
  when :invalid_link
    raise Error.new("local file '#{local_path} is broken link. remove it to continue")
  end
end
put(local_path, remote_filename) click to toggle source
# File lib/savedatasync.rb, line 93
def put(local_path, remote_filename)
  remote_path = construct_remote_path(local_path, remote_filename)
  lstatus = local_status(local_path, remote_path)
  rstatus = remote_status(remote_path)
  case lstatus
  when :entity
    if rstatus == :entity
      unless @force
        raise Error.new("remote file '#{remote_path}' exists. use -f to force")
      end
      FileUtils.rm_r(remote_path)
    end
    FileUtils.mv(local_path, remote_path)
    FileUtils.symlink(remote_path, local_path)
    return true
  when :empty
    raise Error.new("local file '#{local_path}' is empty")
  when :valid_link
    if rstatus == :empty
      raise Error.new("local file '#{local_path}' is broken link. remove it to continue")
    end
    return true
  when :invalid_link
    raise Error.new("local file '#{local_path} is broken link. remove it to continue")
  end
end
status(local_path, remote_filename) click to toggle source
# File lib/savedatasync.rb, line 163
def status(local_path, remote_filename)
  remote_path = construct_remote_path(local_path, remote_filename)
  lstatus = local_status(local_path, remote_path)
  rstatus = remote_status(remote_path)
  STDERR.puts "[#{@title}] #{local_path} (#{lstatus}) <==> #{remote_path} (#{rstatus})"
  return true
end

Private Instance Methods

construct_remote_path(local_path, remote_filename) click to toggle source
# File lib/savedatasync.rb, line 185
def construct_remote_path(local_path, remote_filename)
  File.expand_path("#{@remote_dir_path}/#{@title}/#{remote_filename}").encode("UTF-8")
end
local_status(local_path, remote_path) click to toggle source
# File lib/savedatasync.rb, line 173
def local_status(local_path, remote_path)
  if File.symlink?(local_path)
    File.readlink(local_path).encode("UTF-8") == remote_path ? :valid_link : :invalid_link
  else
    File.exist?(local_path) ? :entity : :empty
  end
end
remote_status(remote_path) click to toggle source
# File lib/savedatasync.rb, line 181
def remote_status(remote_path)
  File.exist?(remote_path) ? :entity : :empty
end