Class: Dry::Files::MemoryFileSystem Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/files/memory_file_system.rb,
lib/dry/files/memory_file_system/node.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Memory File System abstraction to support ‘Dry::Files`

Since:

  • 0.1.0

Defined Under Namespace

Classes: Node

Instance Method Summary collapse

Constructor Details

#initialize(root: Node.root) ⇒ Dry::Files::MemoryFileSystem

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new instance

Parameters:

Since:

  • 0.1.0



28
29
30
# File 'lib/dry/files/memory_file_system.rb', line 28

def initialize(root: Node.root)
  @root = root
end

Instance Method Details

#chdir(path, &blk) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Temporary changes the current working directory of the process to the given path and yield the given block.

The argument ‘path` is intended to be a directory.

Parameters:

  • path (String)

    the target directory

  • blk (Proc)

    the code to execute with the target directory

Raises:

Since:

  • 0.1.0



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/dry/files/memory_file_system.rb', line 179

def chdir(path, &blk)
  path = Path[path]
  directory = find(path)

  raise IOError, Errno::ENOENT.new(path.to_s) if directory.nil?
  raise IOError, Errno::ENOTDIR.new(path.to_s) unless directory.directory?

  current_root = @root
  @root = directory
  blk.call
ensure
  @root = current_root
end

#chmod(path, mode) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sets node UNIX mode

Parameters:

  • path (String, Array<String>)

    the path to the node

  • mode (Integer)

    a UNIX mode, in base 2, 8, 10, or 16

Raises:

Since:

  • 0.1.0



321
322
323
324
325
326
327
328
# File 'lib/dry/files/memory_file_system.rb', line 321

def chmod(path, mode)
  path = Path[path]
  node = find(path)

  raise IOError, Errno::ENOENT.new(path.to_s) if node.nil?

  node.chmod = mode
end

#cp(source, destination) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copies file content from ‘source` to `destination` All the intermediate `destination` directories are created.

Parameters:

  • source (String, Array<String>)

    the file(s) or directory to copy

  • destination (String, Array<String>)

    the directory destination

Raises:

Since:

  • 0.1.0



248
249
250
251
# File 'lib/dry/files/memory_file_system.rb', line 248

def cp(source, destination)
  content = read(source)
  write(destination, content)
end

#directory?(path) ⇒ TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the given path corresponds to a directory.

Parameters:

  • path (String, Array<String>)

    the path to the directory

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0



368
369
370
371
# File 'lib/dry/files/memory_file_system.rb', line 368

def directory?(path)
  path = Path[path]
  !find_directory(path).nil?
end

#executable?(path) ⇒ TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the given path is an executable.

Parameters:

  • path (String, Array<String>)

    the path to the node

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0



380
381
382
383
384
385
386
387
# File 'lib/dry/files/memory_file_system.rb', line 380

def executable?(path)
  path = Path[path]

  node = find(path)
  return false if node.nil?

  node.executable?
end

#exist?(path) ⇒ TrueClass, FalseClass

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if the given path exist.

Parameters:

  • path (String, Array<String>)

    the path to the node

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0



355
356
357
358
359
# File 'lib/dry/files/memory_file_system.rb', line 355

def exist?(path)
  path = Path[path]

  !find(path).nil?
end

#expand_path(path, dir) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a path to an absolute path.

Parameters:

  • path (String, Array<String>)

    the path to the file

  • dir (String, Array<String>)

    the base directory

Since:

  • 0.1.0



150
151
152
153
154
# File 'lib/dry/files/memory_file_system.rb', line 150

def expand_path(path, dir)
  return path if Path.absolute?(path)

  join(dir, path)
end

#join(*path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new string formed by joining the strings using Operating System path separator

Parameters:

  • path (String, Array<String>)

    path tokens

Returns:

  • (String)

    the joined path

Since:

  • 0.1.0



139
140
141
# File 'lib/dry/files/memory_file_system.rb', line 139

def join(*path)
  Path[path]
end

#mkdir(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a directory and all its parent directories.

The argument ‘path` is intended to be a directory that you want to explicitly create.

Parameters:

  • path (String, Array<String>)

    the directory to create

Raises:

See Also:

Since:

  • 0.1.0



206
207
208
209
210
211
212
213
214
# File 'lib/dry/files/memory_file_system.rb', line 206

def mkdir(path)
  path = Path[path]
  node = @root

  for_each_segment(path) do |segment|
    node = node.set(segment)
    raise IOError, Errno::EEXIST.new(path.to_s) if node.file?
  end
end

#mkdir_p(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a directory and all its parent directories.

The argument ‘path` is intended to be a file, where its directory ancestors will be implicitly created.

Parameters:

  • path (String, Array<String>)

    the file that will be in the directories that this method creates

Raises:

See Also:

Since:

  • 0.1.0



230
231
232
233
234
235
236
# File 'lib/dry/files/memory_file_system.rb', line 230

def mkdir_p(path)
  path = Path[path]

  mkdir(
    Path.dirname(path)
  )
end

#mode(path) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Gets node UNIX mode

Parameters:

  • path (String, Array<String>)

    the path to the node

Returns:

  • (Integer)

    the UNIX mode

Raises:

Since:

  • 0.1.0



339
340
341
342
343
344
345
346
# File 'lib/dry/files/memory_file_system.rb', line 339

def mode(path)
  path = Path[path]
  node = find(path)

  raise IOError, Errno::ENOENT.new(path.to_s) if node.nil?

  node.mode
end

#open(path) {|| ... } ⇒ Dry::Files::MemoryFileSystem::Node

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Opens (or creates) a new file for read/write operations.

Parameters:

  • path (String)

    the target file

Yield Parameters:

Returns:

Since:

  • 0.1.0



40
41
42
43
44
45
46
47
48
# File 'lib/dry/files/memory_file_system.rb', line 40

def open(path, *)
  file = touch(path)

  if block_given?
    yield file
  else
    file
  end
end

#pwdString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the name of the current working directory.

Returns:

  • (String)

    the current working directory.

Since:

  • 0.1.0



162
163
164
# File 'lib/dry/files/memory_file_system.rb', line 162

def pwd
  @root.segment
end

#read(path) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Read file contents

Parameters:

  • path (String, Array<String>)

    the target path

Returns:

  • (String)

    the file contents

Raises:

  • (Dry::Files::IOError)

    in case the target path is a directory or if the file cannot be found

Since:

  • 0.1.0



60
61
62
63
64
65
66
67
68
# File 'lib/dry/files/memory_file_system.rb', line 60

def read(path)
  path = Path[path]
  raise IOError, Errno::EISDIR.new(path.to_s) if directory?(path)

  file = find_file(path)
  raise IOError, Errno::ENOENT.new(path.to_s) if file.nil?

  file.read
end

#readlines(path) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads the entire file specified by path as individual lines, and returns those lines in an array

Parameters:

  • path (String, Array<String>)

    the target path

Returns:

  • (Array<String>)

    the file contents

Raises:

  • (Dry::Files::IOError)

    in case the target path is a directory or if the file cannot be found

Since:

  • 0.1.0



81
82
83
84
85
86
87
88
89
# File 'lib/dry/files/memory_file_system.rb', line 81

def readlines(path)
  path = Path[path]
  node = find(path)

  raise IOError, Errno::ENOENT.new(path.to_s) if node.nil?
  raise IOError, Errno::EISDIR.new(path.to_s) if node.directory?

  node.readlines
end

#rm(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes (deletes) a file

Parameters:

  • path (String, Array<String>)

    the file to remove

Raises:

See Also:

Since:

  • 0.1.0



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/dry/files/memory_file_system.rb', line 263

def rm(path)
  path = Path[path]
  file = nil
  parent = @root
  node = @root

  for_each_segment(path) do |segment|
    break unless node

    file = segment
    parent = node
    node = node.get(segment)
  end

  raise IOError, Errno::ENOENT.new(path.to_s) if node.nil?
  raise IOError, Errno::EPERM.new(path.to_s) if node.directory?

  parent.unset(file)
end

#rm_rf(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes (deletes) a directory

Parameters:

  • path (String, Array<String>)

    the directory to remove

Raises:

See Also:

Since:

  • 0.1.0



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/dry/files/memory_file_system.rb', line 293

def rm_rf(path)
  path = Path[path]
  file = nil
  parent = @root
  node = @root

  for_each_segment(path) do |segment|
    break unless node

    file = segment
    parent = node
    node = node.get(segment)
  end

  raise IOError, Errno::ENOENT.new(path.to_s) if node.nil?

  parent.unset(file)
end

#touch(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a file, if it doesn’t exist, and set empty content.

If the file was already existing, it’s a no-op.

Parameters:

  • path (String, Array<String>)

    the target path

Raises:

Since:

  • 0.1.0



101
102
103
104
105
106
107
# File 'lib/dry/files/memory_file_system.rb', line 101

def touch(path)
  path = Path[path]
  raise IOError, Errno::EISDIR.new(path.to_s) if directory?(path)

  content = read(path) if exist?(path)
  write(path, content || EMPTY_CONTENT)
end

#write(path, *content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new file or rewrites the contents of an existing file for the given path and content All the intermediate directories are created.

Parameters:

  • path (String, Array<String>)

    the target path

  • content (String, Array<String>)

    the content to write

Since:

  • 0.1.0



118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dry/files/memory_file_system.rb', line 118

def write(path, *content)
  path = Path[path]
  node = @root

  for_each_segment(path) do |segment|
    node = node.set(segment)
  end

  node.write(*content)
  node
end