Class: FakeFS::Dir
Class Method Summary collapse
- .[](pattern) ⇒ Object
- .chdir(dir, &blk) ⇒ Object
- .chroot(string) ⇒ Object
- .delete(string) ⇒ Object (also: rmdir, unlink)
- .entries(dirname) ⇒ Object
- .foreach(dirname, &block) ⇒ Object
- .glob(pattern, &block) ⇒ Object
- .mkdir(string, integer = 0) ⇒ Object
- .open(string, &block) ⇒ Object
- .pwd ⇒ Object (also: getwd)
- .tmpdir ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(string) ⇒ Dir
constructor
A new instance of Dir.
- #path ⇒ Object
- #pos ⇒ Object
- #pos=(integer) ⇒ Object
- #read ⇒ Object
- #rewind ⇒ Object
- #seek(integer) ⇒ Object
Constructor Details
#initialize(string) ⇒ Dir
Returns a new instance of Dir.
5 6 7 8 9 10 11 |
# File 'lib/fakefs/dir.rb', line 5 def initialize(string) raise Errno::ENOENT, string unless FileSystem.find(string) @path = string @open = true @pointer = 0 @contents = [ '.', '..', ] + FileSystem.find(@path).values end |
Class Method Details
.[](pattern) ⇒ Object
55 56 57 |
# File 'lib/fakefs/dir.rb', line 55 def self.[](pattern) glob(pattern) end |
.chdir(dir, &blk) ⇒ Object
59 60 61 |
# File 'lib/fakefs/dir.rb', line 59 def self.chdir(dir, &blk) FileSystem.chdir(dir, &blk) end |
.chroot(string) ⇒ Object
63 64 65 |
# File 'lib/fakefs/dir.rb', line 63 def self.chroot(string) # Not implemented yet end |
.delete(string) ⇒ Object Also known as: rmdir, unlink
67 68 69 70 |
# File 'lib/fakefs/dir.rb', line 67 def self.delete(string) raise SystemCallError, "No such file or directory - #{string}" unless FileSystem.find(string).values.empty? FileSystem.delete(string) end |
.entries(dirname) ⇒ Object
72 73 74 75 |
# File 'lib/fakefs/dir.rb', line 72 def self.entries(dirname) raise SystemCallError, dirname unless FileSystem.find(dirname) Dir.new(dirname).map { |file| File.basename(file) } end |
.foreach(dirname, &block) ⇒ Object
77 78 79 |
# File 'lib/fakefs/dir.rb', line 77 def self.foreach(dirname, &block) Dir.open(dirname) { |file| yield file } end |
.glob(pattern, &block) ⇒ Object
81 82 83 84 |
# File 'lib/fakefs/dir.rb', line 81 def self.glob(pattern, &block) files = [FileSystem.find(pattern) || []].flatten.map(&:to_s).sort block_given? ? files.each { |file| block.call(file) } : files end |
.mkdir(string, integer = 0) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/fakefs/dir.rb', line 86 def self.mkdir(string, integer = 0) parent = string.split('/') parent.pop raise Errno::ENOENT, "No such file or directory - #{string}" unless parent.join == "" || parent.join == "." || FileSystem.find(parent.join('/')) raise Errno::EEXIST, "File exists - #{string}" if File.exists?(string) FileUtils.mkdir_p(string) end |
.open(string, &block) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/fakefs/dir.rb', line 94 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
106 107 108 |
# File 'lib/fakefs/dir.rb', line 106 def self.pwd FileSystem.current_dir.to_s end |
.tmpdir ⇒ Object
102 103 104 |
# File 'lib/fakefs/dir.rb', line 102 def self.tmpdir '/tmp' end |
Instance Method Details
#close ⇒ Object
13 14 15 16 17 18 |
# File 'lib/fakefs/dir.rb', line 13 def close @open = false @pointer = nil @contents = nil nil end |
#each(&block) ⇒ Object
20 21 22 23 24 |
# File 'lib/fakefs/dir.rb', line 20 def each(&block) while f = read yield f end end |
#path ⇒ Object
26 27 28 |
# File 'lib/fakefs/dir.rb', line 26 def path @path end |
#pos ⇒ Object
30 31 32 |
# File 'lib/fakefs/dir.rb', line 30 def pos @pointer end |
#pos=(integer) ⇒ Object
34 35 36 |
# File 'lib/fakefs/dir.rb', line 34 def pos=(integer) @pointer = integer end |
#read ⇒ Object
38 39 40 41 42 43 |
# File 'lib/fakefs/dir.rb', line 38 def read raise IOError, "closed directory" if @pointer == nil n = @contents[@pointer] @pointer += 1 n.to_s.gsub(path + '/', '') if n end |
#rewind ⇒ Object
45 46 47 |
# File 'lib/fakefs/dir.rb', line 45 def rewind @pointer = 0 end |
#seek(integer) ⇒ Object
49 50 51 52 53 |
# File 'lib/fakefs/dir.rb', line 49 def seek(integer) raise IOError, "closed directory" if @pointer == nil @pointer = integer @contents[integer] end |