Class: Dry::Files::MemoryFileSystem::Node Private

Inherits:
Object
  • Object
show all
Defined in:
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 node (directory or file)

File modes implementation inspired by www.calleluks.com/flags-bitmasks-and-unix-file-system-permissions-in-ruby/

Since:

  • 0.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segment, mode = DEFAULT_DIRECTORY_MODE) ⇒ 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.

Instantiate a new node. It’s a directory node by default.

Parameters:

  • segment (String)

    the path segment of the node

  • mode (Integer) (defaults to: DEFAULT_DIRECTORY_MODE)

    the UNIX mode

See Also:

  • #mode=

Since:

  • 0.1.0



112
113
114
115
116
117
118
# File 'lib/dry/files/memory_file_system/node.rb', line 112

def initialize(segment, mode = DEFAULT_DIRECTORY_MODE)
  @segment = segment
  @children = nil
  @content = nil

  self.chmod = mode
end

Instance Attribute Details

#modeObject (readonly)

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.

Since:

  • 0.1.0



98
99
100
# File 'lib/dry/files/memory_file_system/node.rb', line 98

def mode
  @mode
end

#segmentObject (readonly)

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.

Since:

  • 0.1.0



98
99
100
# File 'lib/dry/files/memory_file_system/node.rb', line 98

def segment
  @segment
end

Class Method Details

.rootDry::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.

Instantiate a root node

Returns:

Since:

  • 0.1.0



92
93
94
# File 'lib/dry/files/memory_file_system/node.rb', line 92

def self.root
  new(ROOT_PATH)
end

Instance Method Details

#array_to_string(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.

Since:

  • 0.3.0



254
255
256
257
258
# File 'lib/dry/files/memory_file_system/node.rb', line 254

def array_to_string(content)
  content.map do |line|
    line.sub(NEW_LINE_MATCHER, EMPTY_CONTENT)
  end.join(NEW_LINE) + NEW_LINE
end

#chmod=(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.

Set UNIX mode It accepts base 2, 8, 10, and 16 numbers

Parameters:

  • mode (Integer)

    the file mode

Since:

  • 0.1.0



238
239
240
# File 'lib/dry/files/memory_file_system/node.rb', line 238

def chmod=(mode)
  @mode = mode.to_s(MODE_BASE).hex
end

#directory?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 node is a directory

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0



164
165
166
# File 'lib/dry/files/memory_file_system/node.rb', line 164

def directory?
  !file?
end

#executable?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 node is executable for user

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0



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

def executable?
  (mode & MODE_USER_EXECUTE).positive?
end

#file?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 node is a file

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.1.0



174
175
176
# File 'lib/dry/files/memory_file_system/node.rb', line 174

def file?
  !@content.nil?
end

#get(segment) ⇒ Dry::Files::MemoryFileSystem::Node, NilClass

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.

Get a node child

Parameters:

  • segment (String)

    the child path segment

Returns:

Since:

  • 0.1.0



128
129
130
# File 'lib/dry/files/memory_file_system/node.rb', line 128

def get(segment)
  @children&.fetch(segment, nil)
end

#readString

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

Returns:

  • (String)

    the file contents

Raises:

Since:

  • 0.1.0



186
187
188
189
190
191
# File 'lib/dry/files/memory_file_system/node.rb', line 186

def read
  raise NotMemoryFileError, segment unless file?

  @content.rewind
  @content.read
end

#readlinesArray<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 content lines

Returns:

  • (Array<String>)

    the file content lines

Raises:

Since:

  • 0.1.0



201
202
203
204
205
206
# File 'lib/dry/files/memory_file_system/node.rb', line 201

def readlines
  raise NotMemoryFileError, segment unless file?

  @content.rewind
  @content.readlines
end

#set(segment) ⇒ 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.

Set a node child

Parameters:

  • segment (String)

    the child path segment

Since:

  • 0.1.0



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

def set(segment)
  @children ||= {}
  @children[segment] ||= self.class.new(segment)
end

#unset(segment) ⇒ 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.

Unset a node child

Parameters:

  • segment (String)

    the child path segment

Raises:

Since:

  • 0.1.0



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

def unset(segment)
  @children ||= {}
  raise UnknownMemoryNodeError, segment unless @children.key?(segment)

  @children.delete(segment)
end

#write(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.

Write file contents IMPORTANT: This operation turns a node into a file

Parameters:

  • content (String, Array<String>)

    the file content

Raises:

Since:

  • 0.1.0



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/dry/files/memory_file_system/node.rb', line 217

def write(content)
  content = case content
            when String
              content
            when Array
              array_to_string(content)
            when NilClass
              EMPTY_CONTENT
            end

  @content = StringIO.new(content)
  @mode = DEFAULT_FILE_MODE
end