Class: Etna::Filesystem::Mock

Inherits:
Etna::Filesystem show all
Defined in:
lib/etna/filesystem.rb

Instance Method Summary collapse

Methods inherited from Etna::Filesystem

#rm_rf

Constructor Details

#initialize(&new_io) ⇒ Mock

Returns a new instance of Mock.



245
246
247
248
249
# File 'lib/etna/filesystem.rb', line 245

def initialize(&new_io)
  @files = {}
  @dirs = {}
  @new_io = new_io
end

Instance Method Details

#exist?(src) ⇒ Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/etna/filesystem.rb', line 310

def exist?(src)
  @files.include?(src) || @dirs.include?(src)
end

#mkdir_p(dest) ⇒ Object



270
271
272
273
274
275
276
# File 'lib/etna/filesystem.rb', line 270

def mkdir_p(dest)
  while !@dirs.include?(dest)
    @dirs[dest] = Set.new
    break if dest == "." || dest == "/"
    dest, _ = File.split(dest)
  end
end

#mkio(file, opts) ⇒ Object



251
252
253
254
255
256
257
# File 'lib/etna/filesystem.rb', line 251

def mkio(file, opts)
  if @new_io.nil?
    StringIO.new
  else
    @new_io.call(file, opts)
  end
end

#mv(src, dest) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/etna/filesystem.rb', line 278

def mv(src, dest)
  if exist?(dest)
    raise "#{dest} already exists, cannot move"
  end

  if @dirs.include?(src)
    @dirs[dest] = @dirs.delete(src)
  elsif @files.include?(src)
    @files[dest] = @files.delete(src)
  else
    raise "#{src} does not exist, cannot move"
  end
end

#tmpdirObject



292
293
294
295
# File 'lib/etna/filesystem.rb', line 292

def tmpdir
  require 'securerandom'
  "/tmp-#{SecureRandom::uuid}"
end

#with_readable(src, opts = 'r') {|| ... } ⇒ Object

Yields:

  • ()


297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/etna/filesystem.rb', line 297

def with_readable(src, opts = 'r', &block)
  if @dirs.include?(src)
    raise IOError.new("Path #{src} is a directory")
  end

  if !@files.include?(src)
    raise IOError.new("Path #{src} does not exist")
  end

  @files[src].rewind
  yield @files[src]
end

#with_writeable(dest, opts = 'w', size_hint: nil) {|(@files[dest] = mkio(dest, opts))| ... } ⇒ Object

Yields:

  • ((@files[dest] = mkio(dest, opts)))


259
260
261
262
263
264
265
266
267
268
# File 'lib/etna/filesystem.rb', line 259

def with_writeable(dest, opts = 'w', size_hint: nil, &block)
  if @dirs.include?(dest)
    raise IOError.new("Path #{dest} is a directory")
  end

  dir, file = File.split(dest)
  @dirs[dir] ||= Set.new
  @dirs[dir].add(file)
  yield (@files[dest] = mkio(dest, opts))
end