Class: FakeFS::File
- Inherits:
-
Object
- Object
- FakeFS::File
- Defined in:
- lib/fakefs/file.rb
Constant Summary collapse
- PATH_SEPARATOR =
'/'
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
- .basename(*args) ⇒ Object
- .const_missing(name) ⇒ Object
- .directory?(path) ⇒ Boolean
- .dirname(path) ⇒ Object
- .exist?(path) ⇒ Boolean (also: exists?)
- .expand_path(*args) ⇒ Object
- .file?(path) ⇒ Boolean
- .join(*parts) ⇒ Object
- .open(path, mode = 'r') ⇒ Object
- .read(path) ⇒ Object
- .readlines(path) ⇒ Object
- .readlink(path) ⇒ Object
- .symlink?(path) ⇒ Boolean
Instance Method Summary collapse
- #close ⇒ Object
- #exists? ⇒ Boolean
- #flush ⇒ Object
-
#initialize(path, mode = nil) ⇒ File
constructor
A new instance of File.
- #puts(content) ⇒ Object
- #read ⇒ Object
- #write(content) ⇒ Object (also: #print, #<<)
Constructor Details
#initialize(path, mode = nil) ⇒ File
Returns a new instance of File.
86 87 88 89 90 91 |
# File 'lib/fakefs/file.rb', line 86 def initialize(path, mode = nil) @path = path @mode = mode @file = FileSystem.find(path) @open = true end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
85 86 87 |
# File 'lib/fakefs/file.rb', line 85 def path @path end |
Class Method Details
.basename(*args) ⇒ Object
51 52 53 |
# File 'lib/fakefs/file.rb', line 51 def self.basename(*args) RealFile.basename(*args) end |
.const_missing(name) ⇒ Object
17 18 19 |
# File 'lib/fakefs/file.rb', line 17 def self.const_missing(name) RealFile.const_get(name) end |
.directory?(path) ⇒ Boolean
21 22 23 24 25 26 27 28 |
# File 'lib/fakefs/file.rb', line 21 def self.directory?(path) if path.respond_to? :entry path.entry.is_a? FakeDir else result = FileSystem.find(path) result ? result.entry.is_a?(FakeDir) : false end end |
.dirname(path) ⇒ Object
55 56 57 |
# File 'lib/fakefs/file.rb', line 55 def self.dirname(path) RealFile.dirname(path) end |
.exist?(path) ⇒ Boolean Also known as: exists?
9 10 11 |
# File 'lib/fakefs/file.rb', line 9 def self.exist?(path) !!FileSystem.find(path) end |
.expand_path(*args) ⇒ Object
47 48 49 |
# File 'lib/fakefs/file.rb', line 47 def self.(*args) RealFile.(*args) end |
.file?(path) ⇒ Boolean
38 39 40 41 42 43 44 45 |
# File 'lib/fakefs/file.rb', line 38 def self.file?(path) if path.respond_to? :entry path.entry.is_a? FakeFile else result = FileSystem.find(path) result ? result.entry.is_a?(FakeFile) : false end end |
.join(*parts) ⇒ Object
5 6 7 |
# File 'lib/fakefs/file.rb', line 5 def self.join(*parts) parts * PATH_SEPARATOR end |
.open(path, mode = 'r') ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/fakefs/file.rb', line 64 def self.open(path, mode='r') if block_given? yield new(path, mode) else new(path, mode) end end |
.read(path) ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/fakefs/file.rb', line 72 def self.read(path) file = new(path) if file.exists? file.read else raise Errno::ENOENT end end |
.readlines(path) ⇒ Object
81 82 83 |
# File 'lib/fakefs/file.rb', line 81 def self.readlines(path) read(path).split("\n") end |
.readlink(path) ⇒ Object
59 60 61 62 |
# File 'lib/fakefs/file.rb', line 59 def self.readlink(path) symlink = FileSystem.find(path) FileSystem.find(symlink.target).to_s end |
.symlink?(path) ⇒ Boolean
30 31 32 33 34 35 36 |
# File 'lib/fakefs/file.rb', line 30 def self.symlink?(path) if path.respond_to? :entry path.is_a? FakeSymlink else FileSystem.find(path).is_a? FakeSymlink end end |
Instance Method Details
#close ⇒ Object
93 94 95 |
# File 'lib/fakefs/file.rb', line 93 def close @open = false end |
#exists? ⇒ Boolean
102 103 104 |
# File 'lib/fakefs/file.rb', line 102 def exists? @file end |
#flush ⇒ Object
122 |
# File 'lib/fakefs/file.rb', line 122 def flush; self; end |
#puts(content) ⇒ Object
106 107 108 |
# File 'lib/fakefs/file.rb', line 106 def puts(content) write(content + "\n") end |
#read ⇒ Object
97 98 99 100 |
# File 'lib/fakefs/file.rb', line 97 def read raise IOError.new('closed stream') unless @open @file.content end |
#write(content) ⇒ Object Also known as: print, <<
110 111 112 113 114 115 116 117 118 |
# File 'lib/fakefs/file.rb', line 110 def write(content) raise IOError.new('closed stream') unless @open if !File.exists?(@path) @file = FileSystem.add(path, FakeFile.new) end @file.content += content end |