Class: Savedatasync::Operator

Inherits:
Object
  • Object
show all
Defined in:
lib/savedatasync.rb

Constant Summary collapse

Error =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(title, force, remote_dir_path) ⇒ Operator

Returns a new instance of Operator.



82
83
84
85
86
87
88
89
90
91
# 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

Instance Method Details

#cut(local_path, remote_filename) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# 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) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object



163
164
165
166
167
168
169
# 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