Class: MemFs::Dir
- Inherits:
-
Object
- Object
- MemFs::Dir
- Extended by:
- FilesystemAccess
- Includes:
- Enumerable
- Defined in:
- lib/memfs/dir.rb
Instance Attribute Summary collapse
-
#pos ⇒ Object
Returns the value of attribute pos.
Class Method Summary collapse
- .[](*patterns) ⇒ Object
- .chdir(path, &block) ⇒ Object
- .chroot(path) ⇒ Object
- .entries(dirname, opts = {}) ⇒ Object
- .exists?(path) ⇒ Boolean (also: exist?)
- .foreach(dirname, &block) ⇒ Object
- .getwd ⇒ Object (also: pwd)
- .glob(patterns, flags = 0) ⇒ Object
- .home(*args) ⇒ Object
- .mkdir(path) ⇒ Object
- .open(dirname) ⇒ Object
- .rmdir(path) ⇒ Object (also: delete, unlink)
- .tmpdir ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(path) ⇒ Dir
constructor
A new instance of Dir.
- #path ⇒ Object (also: #to_path)
- #read ⇒ Object
- #rewind ⇒ Object
- #seek(position) ⇒ Object
- #tell ⇒ Object
Constructor Details
#initialize(path) ⇒ Dir
Returns a new instance of Dir.
96 97 98 99 100 101 |
# 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 |
Instance Attribute Details
#pos ⇒ Object
Returns the value of attribute pos.
8 9 10 |
# File 'lib/memfs/dir.rb', line 8 def pos @pos end |
Class Method Details
.[](*patterns) ⇒ Object
10 11 12 |
# File 'lib/memfs/dir.rb', line 10 def self.[](*patterns) glob(patterns) end |
.chdir(path, &block) ⇒ Object
14 15 16 17 |
# File 'lib/memfs/dir.rb', line 14 def self.chdir(path, &block) fs.chdir path, &block return 0 end |
.chroot(path) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/memfs/dir.rb', line 19 def self.chroot(path) unless Process.uid.zero? raise Errno::EPERM, path end dir = fs.find_directory!(path) dir.name = '/' fs.root = dir 0 end |
.entries(dirname, opts = {}) ⇒ Object
30 31 32 |
# File 'lib/memfs/dir.rb', line 30 def self.entries(dirname, opts = {}) fs.entries(dirname) end |
.exists?(path) ⇒ Boolean Also known as: exist?
34 35 36 |
# File 'lib/memfs/dir.rb', line 34 def self.exists?(path) File.directory?(path) end |
.foreach(dirname, &block) ⇒ Object
39 40 41 42 43 |
# File 'lib/memfs/dir.rb', line 39 def self.foreach(dirname, &block) return to_enum(__callee__, dirname) unless block entries(dirname).each(&block) end |
.getwd ⇒ Object Also known as: pwd
45 46 47 |
# File 'lib/memfs/dir.rb', line 45 def self.getwd fs.getwd end |
.glob(patterns, flags = 0) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/memfs/dir.rb', line 50 def self.glob(patterns, flags = 0) patterns = [*patterns] 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 } and nil end |
.home(*args) ⇒ Object
63 64 65 |
# File 'lib/memfs/dir.rb', line 63 def self.home(*args) original_dir_class.home(*args) end |
.mkdir(path) ⇒ Object
67 68 69 |
# File 'lib/memfs/dir.rb', line 67 def self.mkdir(path) fs.mkdir path end |
.open(dirname) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# 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 |
.rmdir(path) ⇒ Object Also known as: delete, unlink
83 84 85 |
# File 'lib/memfs/dir.rb', line 83 def self.rmdir(path) fs.rmdir path end |
.tmpdir ⇒ Object
87 88 89 |
# File 'lib/memfs/dir.rb', line 87 def self.tmpdir '/tmp' end |
Instance Method Details
#close ⇒ Object
103 104 105 106 107 108 |
# File 'lib/memfs/dir.rb', line 103 def close if state == :closed fail IOError, 'closed directory' end self.state = :closed end |
#each(&block) ⇒ Object
110 111 112 113 |
# File 'lib/memfs/dir.rb', line 110 def each(&block) return to_enum(__callee__) unless block entry.entry_names.each(&block) end |
#path ⇒ Object Also known as: to_path
115 116 117 |
# File 'lib/memfs/dir.rb', line 115 def path entry.path end |
#read ⇒ Object
125 126 127 128 129 130 |
# File 'lib/memfs/dir.rb', line 125 def read name = entries[pos] @pos += 1 self.max_seek = pos name end |
#rewind ⇒ Object
132 133 134 135 |
# File 'lib/memfs/dir.rb', line 132 def rewind @pos = 0 self end |
#seek(position) ⇒ Object
137 138 139 140 141 142 |
# File 'lib/memfs/dir.rb', line 137 def seek(position) if (0..max_seek).cover?(position) @pos = position end self end |
#tell ⇒ Object
144 145 146 |
# File 'lib/memfs/dir.rb', line 144 def tell @pos end |