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, perm = nil) ⇒ File

Returns a new instance of File.



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

def initialize(path, mode = nil, perm = nil)
  @path = path
  @mode = mode
  @file = FileSystem.find(path)
  @open = true
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



89
90
91
# File 'lib/fakefs/file.rb', line 89

def path
  @path
end

Class Method Details

.basename(*args) ⇒ Object



55
56
57
# File 'lib/fakefs/file.rb', line 55

def self.basename(*args)
  RealFile.basename(*args)
end

.const_missing(name) ⇒ Object



21
22
23
# File 'lib/fakefs/file.rb', line 21

def self.const_missing(name)
  RealFile.const_get(name)
end

.directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/fakefs/file.rb', line 25

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



59
60
61
# File 'lib/fakefs/file.rb', line 59

def self.dirname(path)
  RealFile.dirname(path)
end

.exist?(path) ⇒ Boolean Also known as: exists?

Returns:

  • (Boolean)


13
14
15
# File 'lib/fakefs/file.rb', line 13

def self.exist?(path)
  !!FileSystem.find(path)
end

.expand_path(*args) ⇒ Object



51
52
53
# File 'lib/fakefs/file.rb', line 51

def self.expand_path(*args)
  RealFile.expand_path(*args)
end

.extname(path) ⇒ Object



5
6
7
# File 'lib/fakefs/file.rb', line 5

def self.extname(path)
  RealFile.extname(path)
end

.file?(path) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/fakefs/file.rb', line 42

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



9
10
11
# File 'lib/fakefs/file.rb', line 9

def self.join(*parts)
  parts * PATH_SEPARATOR
end

.open(path, mode = 'r', perm = 0644) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/fakefs/file.rb', line 68

def self.open(path, mode='r', perm = 0644)
  if block_given?
    yield new(path, mode, perm)
  else
    new(path, mode, perm)
  end
end

.read(path) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/fakefs/file.rb', line 76

def self.read(path)
  file = new(path)
  if file.exists?
    file.read
  else
    raise Errno::ENOENT
  end
end

.readlines(path) ⇒ Object



85
86
87
# File 'lib/fakefs/file.rb', line 85

def self.readlines(path)
  read(path).split("\n")
end


63
64
65
66
# File 'lib/fakefs/file.rb', line 63

def self.readlink(path)
  symlink = FileSystem.find(path)
  FileSystem.find(symlink.target).to_s
end

.symlink?(path) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/fakefs/file.rb', line 34

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



97
98
99
# File 'lib/fakefs/file.rb', line 97

def close
  @open = false
end

#exists?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/fakefs/file.rb', line 106

def exists?
  @file
end

#flushObject



128
# File 'lib/fakefs/file.rb', line 128

def flush; self; end

#puts(*content) ⇒ Object



110
111
112
113
114
# File 'lib/fakefs/file.rb', line 110

def puts(*content)
  content.flatten.each do |obj|
    write(obj.to_s + "\n")
  end
end

#readObject

Raises:

  • (IOError)


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

def read
  raise IOError.new('closed stream') unless @open
  @file.content
end

#write(content) ⇒ Object Also known as: print, <<

Raises:

  • (IOError)


116
117
118
119
120
121
122
123
124
# File 'lib/fakefs/file.rb', line 116

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