Class: Etna::Filesystem::Mock
- Inherits:
-
Filesystem
- Object
- 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
Constructor Details
#initialize(&new_io) ⇒ Mock
Returns a new instance of Mock.
349 350 351 352 353 |
# File 'lib/etna/filesystem.rb', line 349 def initialize(&new_io) @files = {} @dirs = {} @new_io = new_io end |
Instance Method Details
#exist?(src) ⇒ Boolean
414 415 416 |
# File 'lib/etna/filesystem.rb', line 414 def exist?(src) @files.include?(src) || @dirs.include?(src) end |
#mkdir_p(dest) ⇒ Object
374 375 376 377 378 379 380 |
# File 'lib/etna/filesystem.rb', line 374 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
355 356 357 358 359 360 361 |
# File 'lib/etna/filesystem.rb', line 355 def mkio(file, opts) if @new_io.nil? StringIO.new else @new_io.call(file, opts) end end |
#mv(src, dest) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/etna/filesystem.rb', line 382 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
418 419 420 |
# File 'lib/etna/filesystem.rb', line 418 def stat(src) @files[src].respond_to?(:stat) ? @files[src].stat : MockStat.new(@files[src]) end |
#tmpdir ⇒ Object
396 397 398 399 |
# File 'lib/etna/filesystem.rb', line 396 def tmpdir require 'securerandom' "/tmp-#{SecureRandom::uuid}" end |
#with_readable(src, opts = 'r') {|| ... } ⇒ Object
401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/etna/filesystem.rb', line 401 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
363 364 365 366 367 368 369 370 371 372 |
# File 'lib/etna/filesystem.rb', line 363 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 |