class MemFs::Dir

Constants

GLOB_FLAGS

Attributes

entry[RW]
max_seek[RW]
pos[R]
state[RW]

Public Class Methods

[](*patterns) click to toggle source
# File lib/memfs/dir.rb, line 11
def self.[](*patterns)
  glob(patterns)
end
chdir(path, &block) click to toggle source
# File lib/memfs/dir.rb, line 15
def self.chdir(path, &block)
  fs.chdir path, &block
  0
end
chroot(path) click to toggle source
# File lib/memfs/dir.rb, line 20
def self.chroot(path)
  fail Errno::EPERM, path unless Process.uid.zero?

  dir = fs.find_directory!(path)
  dir.name = '/'
  fs.root = dir
  0
end
delete(path)
Alias for: rmdir
entries(dirname, _opts = {}) click to toggle source
# File lib/memfs/dir.rb, line 29
def self.entries(dirname, _opts = {})
  fs.entries(dirname)
end
exist?(path)
Alias for: exists?
exists?(path) click to toggle source
# File lib/memfs/dir.rb, line 33
def self.exists?(path)
  File.directory?(path)
end
Also aliased as: exist?
foreach(dirname, &block) click to toggle source
# File lib/memfs/dir.rb, line 38
def self.foreach(dirname, &block)
  return to_enum(__callee__, dirname) unless block

  entries(dirname).each(&block)
end
getwd() click to toggle source
# File lib/memfs/dir.rb, line 44
def self.getwd
  fs.getwd
end
Also aliased as: pwd
glob(patterns, flags = 0) { |path| ... } click to toggle source
# File lib/memfs/dir.rb, line 49
def self.glob(patterns, flags = 0)
  patterns = [*patterns].map(&:to_s)
  list = fs.paths.select do |path|
    patterns.any? do |pattern|
      File.fnmatch?(pattern, path, flags | GLOB_FLAGS)
    end
  end
  # FIXME: ugly special case for /* and /
  list.delete('/') if patterns.first == '/*'
  return list unless block_given?
  list.each { |path| yield path }
  nil
end
home(*args) click to toggle source
# File lib/memfs/dir.rb, line 63
def self.home(*args)
  original_dir_class.home(*args)
end
mkdir(path, mode = 0777) click to toggle source
# File lib/memfs/dir.rb, line 67
def self.mkdir(path, mode = 0777)
  fs.mkdir path, mode
end
new(path) click to toggle source
# File lib/memfs/dir.rb, line 96
def initialize(path)
  self.entry = fs.find_directory!(path)
  self.state = :open
  @pos = 0
  self.max_seek = 0
end
open(dirname) { |dir| ... } click to toggle source
# File lib/memfs/dir.rb, line 71
def self.open(dirname)
  dir = new(dirname)

  if block_given?
    yield dir
  else
    dir
  end
ensure
  dir && dir.close if block_given?
end
pwd()
Alias for: getwd
rmdir(path) click to toggle source
# File lib/memfs/dir.rb, line 83
def self.rmdir(path)
  fs.rmdir path
end
Also aliased as: delete, unlink
tmpdir() click to toggle source
# File lib/memfs/dir.rb, line 87
def self.tmpdir
  '/tmp'
end

Private Class Methods

original_dir_class() click to toggle source
# File lib/memfs/dir.rb, line 154
def self.original_dir_class
  MemFs::OriginalDir
end

Public Instance Methods

close() click to toggle source
# File lib/memfs/dir.rb, line 103
def close
  fail IOError, 'closed directory' if state == :closed
  self.state = :closed
end
each(&block) click to toggle source
# File lib/memfs/dir.rb, line 108
def each(&block)
  return to_enum(__callee__) unless block
  entry.entry_names.each(&block)
end
path() click to toggle source
# File lib/memfs/dir.rb, line 113
def path
  entry.path
end
Also aliased as: to_path
pos=(position) click to toggle source
# File lib/memfs/dir.rb, line 118
def pos=(position)
  seek(position)
  position
end
read() click to toggle source
# File lib/memfs/dir.rb, line 123
def read
  name = entries[pos]
  @pos += 1
  self.max_seek = pos
  name
end
rewind() click to toggle source
# File lib/memfs/dir.rb, line 130
def rewind
  @pos = 0
  self
end
seek(position) click to toggle source
# File lib/memfs/dir.rb, line 135
def seek(position)
  @pos = position if (0..max_seek).cover?(position)
  self
end
tell() click to toggle source
# File lib/memfs/dir.rb, line 140
def tell
  @pos
end
to_path()
Alias for: path