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.
514 515 516 517 518 |
# File 'lib/etna/filesystem.rb', line 514 def initialize(&new_io) @files = {} @dirs = {} @new_io = new_io end |
Instance Method Details
#exist?(src) ⇒ Boolean
579 580 581 |
# File 'lib/etna/filesystem.rb', line 579 def exist?(src) @files.include?(src) || @dirs.include?(src) end |
#mkdir_p(dest) ⇒ Object
539 540 541 542 543 544 545 |
# File 'lib/etna/filesystem.rb', line 539 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
520 521 522 523 524 525 526 |
# File 'lib/etna/filesystem.rb', line 520 def mkio(file, opts) if @new_io.nil? StringIO.new else @new_io.call(file, opts) end end |
#mv(src, dest) ⇒ Object
547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'lib/etna/filesystem.rb', line 547 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
583 584 585 |
# File 'lib/etna/filesystem.rb', line 583 def stat(src) @files[src].respond_to?(:stat) ? @files[src].stat : MockStat.new(@files[src]) end |
#tmpdir ⇒ Object
561 562 563 564 |
# File 'lib/etna/filesystem.rb', line 561 def tmpdir require 'securerandom' "/tmp-#{SecureRandom::uuid}" end |
#with_readable(src, opts = 'r') {|| ... } ⇒ Object
566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/etna/filesystem.rb', line 566 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
528 529 530 531 532 533 534 535 536 537 |
# File 'lib/etna/filesystem.rb', line 528 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 |