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.
494 495 496 497 498 |
# File 'lib/etna/filesystem.rb', line 494 def initialize(&new_io) @files = {} @dirs = {} @new_io = new_io end |
Instance Method Details
#exist?(src) ⇒ Boolean
559 560 561 |
# File 'lib/etna/filesystem.rb', line 559 def exist?(src) @files.include?(src) || @dirs.include?(src) end |
#mkdir_p(dest) ⇒ Object
519 520 521 522 523 524 525 |
# File 'lib/etna/filesystem.rb', line 519 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
500 501 502 503 504 505 506 |
# File 'lib/etna/filesystem.rb', line 500 def mkio(file, opts) if @new_io.nil? StringIO.new else @new_io.call(file, opts) end end |
#mv(src, dest) ⇒ Object
527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/etna/filesystem.rb', line 527 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
563 564 565 |
# File 'lib/etna/filesystem.rb', line 563 def stat(src) @files[src].respond_to?(:stat) ? @files[src].stat : MockStat.new(@files[src]) end |
#tmpdir ⇒ Object
541 542 543 544 |
# File 'lib/etna/filesystem.rb', line 541 def tmpdir require 'securerandom' "/tmp-#{SecureRandom::uuid}" end |
#with_readable(src, opts = 'r') {|| ... } ⇒ Object
546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'lib/etna/filesystem.rb', line 546 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
508 509 510 511 512 513 514 515 516 517 |
# File 'lib/etna/filesystem.rb', line 508 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 |