Class: FakeFS::Dir
Overview
FakeFs Dir class
Defined Under Namespace
Modules: Tmpname
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .[](*pattern) ⇒ Object
- ._check_for_valid_file(path) ⇒ Object
- .chdir(dir, &blk) ⇒ Object
- .chroot(_string) ⇒ Object
- .delete(string) ⇒ Object (also: rmdir, unlink)
- .entries(dirname, _opts = {}) ⇒ Object
- .exists?(path) ⇒ Boolean (also: exist?)
- .foreach(dirname, &_block) ⇒ Object
- .glob(pattern, _flags = 0, &block) ⇒ Object
- .home(user = nil) ⇒ Object
- .mkdir(string, _integer = 0) ⇒ Object
- .open(string, &_block) ⇒ Object
- .pwd ⇒ Object (also: getwd)
- .tmpdir ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #each ⇒ Object
-
#initialize(string) ⇒ Dir
constructor
A new instance of Dir.
- #pos ⇒ Object
- #pos=(integer) ⇒ Object
- #read ⇒ Object
- #rewind ⇒ Object
- #seek(integer) ⇒ Object
Constructor Details
#initialize(string) ⇒ Dir
Returns a new instance of Dir.
11 12 13 14 15 16 17 18 |
# File 'lib/fakefs/dir.rb', line 11 def initialize(string) self.class._check_for_valid_file(string) @path = FileSystem.normalize_path(string) @open = true @pointer = 0 @contents = ['.', '..'] + FileSystem.find(@path).entries end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/fakefs/dir.rb', line 5 def path @path end |
Class Method Details
.[](*pattern) ⇒ Object
62 63 64 |
# File 'lib/fakefs/dir.rb', line 62 def self.[](*pattern) glob pattern end |
._check_for_valid_file(path) ⇒ Object
7 8 9 |
# File 'lib/fakefs/dir.rb', line 7 def self._check_for_valid_file(path) fail Errno::ENOENT, path unless FileSystem.find(path) end |
.chdir(dir, &blk) ⇒ Object
70 71 72 |
# File 'lib/fakefs/dir.rb', line 70 def self.chdir(dir, &blk) FileSystem.chdir(dir, &blk) end |
.chroot(_string) ⇒ Object
74 75 76 |
# File 'lib/fakefs/dir.rb', line 74 def self.chroot(_string) fail NotImplementedError end |
.delete(string) ⇒ Object Also known as: rmdir, unlink
78 79 80 81 82 83 |
# File 'lib/fakefs/dir.rb', line 78 def self.delete(string) _check_for_valid_file(string) fail Errno::ENOTEMPTY, string unless FileSystem.find(string).empty? FileSystem.delete(string) end |
.entries(dirname, _opts = {}) ⇒ Object
85 86 87 88 89 |
# File 'lib/fakefs/dir.rb', line 85 def self.entries(dirname, _opts = {}) _check_for_valid_file(dirname) Dir.new(dirname).map { |file| File.basename(file) } end |
.exists?(path) ⇒ Boolean Also known as: exist?
66 67 68 |
# File 'lib/fakefs/dir.rb', line 66 def self.exists?(path) File.exist?(path) && File.directory?(path) end |
.foreach(dirname, &_block) ⇒ Object
91 92 93 |
# File 'lib/fakefs/dir.rb', line 91 def self.foreach(dirname, &_block) Dir.open(dirname) { |file| yield file } end |
.glob(pattern, _flags = 0, &block) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/fakefs/dir.rb', line 95 def self.glob(pattern, _flags = 0, &block) matches_for_pattern = lambda do |matcher| [FileSystem.find(matcher) || []].flatten.map do |e| if Dir.pwd.match(%r{\A/?\z}) || !e.to_s.match(%r{\A#{Dir.pwd}/?}) e.to_s else e.to_s.match(%r{\A#{Dir.pwd}/?}).post_match end end.sort end if pattern.is_a? Array files = pattern.map do |matcher| matches_for_pattern.call matcher end.flatten else files = matches_for_pattern.call pattern end block_given? ? files.each { |file| block.call(file) } : files end |
.home(user = nil) ⇒ Object
119 120 121 |
# File 'lib/fakefs/dir.rb', line 119 def self.home(user = nil) RealDir.home(user) end |
.mkdir(string, _integer = 0) ⇒ Object
124 125 126 |
# File 'lib/fakefs/dir.rb', line 124 def self.mkdir(string, _integer = 0) FileUtils.mkdir(string) end |
.open(string, &_block) ⇒ Object
128 129 130 131 132 133 134 |
# File 'lib/fakefs/dir.rb', line 128 def self.open(string, &_block) if block_given? Dir.new(string).each { |file| yield(file) } else Dir.new(string) end end |
.pwd ⇒ Object Also known as: getwd
140 141 142 |
# File 'lib/fakefs/dir.rb', line 140 def self.pwd FileSystem.current_dir.to_s end |
.tmpdir ⇒ Object
136 137 138 |
# File 'lib/fakefs/dir.rb', line 136 def self.tmpdir '/tmp' end |
Instance Method Details
#close ⇒ Object
20 21 22 23 24 25 |
# File 'lib/fakefs/dir.rb', line 20 def close @open = false @pointer = nil @contents = nil nil end |
#each ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/fakefs/dir.rb', line 27 def each if block_given? while (f = read) yield f end else @contents.map { |entry| entry_to_relative_path(entry) }.each end end |
#pos ⇒ Object
37 38 39 |
# File 'lib/fakefs/dir.rb', line 37 def pos @pointer end |
#pos=(integer) ⇒ Object
41 42 43 |
# File 'lib/fakefs/dir.rb', line 41 def pos=(integer) @pointer = integer end |
#read ⇒ Object
45 46 47 48 49 50 |
# File 'lib/fakefs/dir.rb', line 45 def read fail IOError, 'closed directory' unless @pointer entry = @contents[@pointer] @pointer += 1 entry_to_relative_path(entry) if entry end |
#rewind ⇒ Object
52 53 54 |
# File 'lib/fakefs/dir.rb', line 52 def rewind @pointer = 0 end |
#seek(integer) ⇒ Object
56 57 58 59 60 |
# File 'lib/fakefs/dir.rb', line 56 def seek(integer) fail IOError, 'closed directory' if @pointer.nil? @pointer = integer @contents[integer] end |