Class: FakeFS::File

Inherits:
Object
  • Object
show all
Defined in:
lib/fakefs/file.rb

Constant Summary collapse

PATH_SEPARATOR =
'/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#pathObject (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

Returns:

  • (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?

Returns:

  • (Boolean)


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.expand_path(*args)
  RealFile.expand_path(*args)
end

.file?(path) ⇒ Boolean

Returns:

  • (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


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

Returns:

  • (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

#closeObject



93
94
95
# File 'lib/fakefs/file.rb', line 93

def close
  @open = false
end

#exists?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/fakefs/file.rb', line 102

def exists?
  @file
end

#flushObject



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

#readObject

Raises:

  • (IOError)


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, <<

Raises:

  • (IOError)


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