Class: Etna::Filesystem::Mock

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

Instance Method Summary collapse

Methods inherited from Etna::Filesystem

#ls, #rm_rf

Constructor Details

#initialize(&new_io) ⇒ Mock

Returns a new instance of Mock.



373
374
375
376
377
# File 'lib/etna/filesystem.rb', line 373

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

Instance Method Details

#exist?(src) ⇒ Boolean

Returns:

  • (Boolean)


438
439
440
# File 'lib/etna/filesystem.rb', line 438

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

#mkdir_p(dest) ⇒ Object



398
399
400
401
402
403
404
# File 'lib/etna/filesystem.rb', line 398

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



379
380
381
382
383
384
385
# File 'lib/etna/filesystem.rb', line 379

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

#mv(src, dest) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/etna/filesystem.rb', line 406

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



420
421
422
423
# File 'lib/etna/filesystem.rb', line 420

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

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

Yields:

  • ()


425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/etna/filesystem.rb', line 425

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)))


387
388
389
390
391
392
393
394
395
396
# File 'lib/etna/filesystem.rb', line 387

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