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
- .children(dirname, opts = {}) ⇒ Object
- .chroot(_string) ⇒ Object
- .delete(string) ⇒ Object (also: rmdir, unlink)
- .each_child(dirname, &_block) ⇒ Object
- .empty?(dirname) ⇒ Boolean
- .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.
13 14 15 16 17 18 19 20 |
# File 'lib/fakefs/dir.rb', line 13 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.
7 8 9 |
# File 'lib/fakefs/dir.rb', line 7 def path @path end |
Class Method Details
.[](*pattern) ⇒ Object
64 65 66 |
# File 'lib/fakefs/dir.rb', line 64 def self.[](*pattern) glob pattern end |
._check_for_valid_file(path) ⇒ Object
9 10 11 |
# File 'lib/fakefs/dir.rb', line 9 def self._check_for_valid_file(path) fail Errno::ENOENT, path unless FileSystem.find(path) end |
.chdir(dir, &blk) ⇒ Object
72 73 74 |
# File 'lib/fakefs/dir.rb', line 72 def self.chdir(dir, &blk) FileSystem.chdir(dir, &blk) end |
.children(dirname, opts = {}) ⇒ Object
93 94 95 |
# File 'lib/fakefs/dir.rb', line 93 def self.children(dirname, opts = {}) entries(dirname, opts) - %w(. ..) end |
.chroot(_string) ⇒ Object
76 77 78 |
# File 'lib/fakefs/dir.rb', line 76 def self.chroot(_string) fail NotImplementedError end |
.delete(string) ⇒ Object Also known as: rmdir, unlink
80 81 82 83 84 85 |
# File 'lib/fakefs/dir.rb', line 80 def self.delete(string) _check_for_valid_file(string) fail Errno::ENOTEMPTY, string unless FileSystem.find(string).empty? FileSystem.delete(string) end |
.each_child(dirname, &_block) ⇒ Object
97 98 99 100 101 102 |
# File 'lib/fakefs/dir.rb', line 97 def self.each_child(dirname, &_block) Dir.open(dirname) do |file| next if %w(. ..).include?(file) yield file end end |
.empty?(dirname) ⇒ Boolean
105 106 107 108 109 110 111 112 |
# File 'lib/fakefs/dir.rb', line 105 def self.empty?(dirname) _check_for_valid_file(dirname) if File.directory?(dirname) Dir.new(dirname).count <= 2 else false end end |
.entries(dirname, _opts = {}) ⇒ Object
87 88 89 90 91 |
# File 'lib/fakefs/dir.rb', line 87 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?
68 69 70 |
# File 'lib/fakefs/dir.rb', line 68 def self.exists?(path) File.exist?(path) && File.directory?(path) end |
.foreach(dirname, &_block) ⇒ Object
115 116 117 |
# File 'lib/fakefs/dir.rb', line 115 def self.foreach(dirname, &_block) Dir.open(dirname) { |file| yield file } end |
.glob(pattern, _flags = 0, &block) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/fakefs/dir.rb', line 119 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 files = if pattern.is_a?(Array) pattern.map do |matcher| matches_for_pattern.call matcher end.flatten else matches_for_pattern.call pattern end block_given? ? files.each { |file| block.call(file) } : files end |
.home(user = nil) ⇒ Object
144 145 146 |
# File 'lib/fakefs/dir.rb', line 144 def self.home(user = nil) RealDir.home(user) end |
.mkdir(string, _integer = 0) ⇒ Object
149 150 151 |
# File 'lib/fakefs/dir.rb', line 149 def self.mkdir(string, _integer = 0) FileUtils.mkdir(string) end |
.open(string, &_block) ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/fakefs/dir.rb', line 153 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
165 166 167 |
# File 'lib/fakefs/dir.rb', line 165 def self.pwd FileSystem.current_dir.to_s end |
.tmpdir ⇒ Object
161 162 163 |
# File 'lib/fakefs/dir.rb', line 161 def self.tmpdir '/tmp' end |
Instance Method Details
#close ⇒ Object
22 23 24 25 26 27 |
# File 'lib/fakefs/dir.rb', line 22 def close @open = false @pointer = nil @contents = nil nil end |
#each ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/fakefs/dir.rb', line 29 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
39 40 41 |
# File 'lib/fakefs/dir.rb', line 39 def pos @pointer end |
#pos=(integer) ⇒ Object
43 44 45 |
# File 'lib/fakefs/dir.rb', line 43 def pos=(integer) @pointer = integer end |
#read ⇒ Object
47 48 49 50 51 52 |
# File 'lib/fakefs/dir.rb', line 47 def read fail IOError, 'closed directory' unless @pointer entry = @contents[@pointer] @pointer += 1 entry_to_relative_path(entry) if entry end |
#rewind ⇒ Object
54 55 56 |
# File 'lib/fakefs/dir.rb', line 54 def rewind @pointer = 0 end |
#seek(integer) ⇒ Object
58 59 60 61 62 |
# File 'lib/fakefs/dir.rb', line 58 def seek(integer) fail IOError, 'closed directory' if @pointer.nil? @pointer = integer @contents[integer] end |