Class: Etna::Filesystem::Mock
- Inherits:
-
Etna::Filesystem
- Object
- Etna::Filesystem
- Etna::Filesystem::Mock
- Defined in:
- lib/etna/filesystem.rb
Defined Under Namespace
Classes: MockStat
Instance Method Summary collapse
- #exist?(src) ⇒ Boolean
-
#initialize(&new_io) ⇒ Mock
constructor
A new instance of Mock.
- #mkdir_p(dest) ⇒ Object
- #mkio(file, opts) ⇒ Object
- #mv(src, dest) ⇒ Object
- #stat(src) ⇒ Object
- #tmpdir ⇒ Object
- #with_readable(src, opts = 'r') {|| ... } ⇒ Object
- #with_writeable(dest, opts = 'w', size_hint: nil) {|(@files[dest] = mkio(dest, opts))| ... } ⇒ Object
Methods inherited from Etna::Filesystem
Constructor Details
#initialize(&new_io) ⇒ Mock
Returns a new instance of Mock.
432 433 434 435 436 |
# File 'lib/etna/filesystem.rb', line 432 def initialize(&new_io) @files = {} @dirs = {} @new_io = new_io end |
Instance Method Details
#exist?(src) ⇒ Boolean
497 498 499 |
# File 'lib/etna/filesystem.rb', line 497 def exist?(src) @files.include?(src) || @dirs.include?(src) end |
#mkdir_p(dest) ⇒ Object
457 458 459 460 461 462 463 |
# File 'lib/etna/filesystem.rb', line 457 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
438 439 440 441 442 443 444 |
# File 'lib/etna/filesystem.rb', line 438 def mkio(file, opts) if @new_io.nil? StringIO.new else @new_io.call(file, opts) end end |
#mv(src, dest) ⇒ Object
465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/etna/filesystem.rb', line 465 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 |
#stat(src) ⇒ Object
501 502 503 |
# File 'lib/etna/filesystem.rb', line 501 def stat(src) @files[src].respond_to?(:stat) ? @files[src].stat : MockStat.new end |
#tmpdir ⇒ Object
479 480 481 482 |
# File 'lib/etna/filesystem.rb', line 479 def tmpdir require 'securerandom' "/tmp-#{SecureRandom::uuid}" end |
#with_readable(src, opts = 'r') {|| ... } ⇒ Object
484 485 486 487 488 489 490 491 492 493 494 495 |
# File 'lib/etna/filesystem.rb', line 484 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
446 447 448 449 450 451 452 453 454 455 |
# File 'lib/etna/filesystem.rb', line 446 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 |