Class: Dry::Files::MemoryFileSystem::Node Private
- Inherits:
-
Object
- Object
- Dry::Files::MemoryFileSystem::Node
- 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/
Instance Attribute Summary collapse
- #mode ⇒ Object readonly private
- #segment ⇒ Object readonly private
Class Method Summary collapse
-
.root ⇒ Dry::Files::MemoryFileSystem::Node
private
Instantiate a root node.
Instance Method Summary collapse
- #array_to_string(content) ⇒ Object private
-
#chmod=(mode) ⇒ Object
private
Set UNIX mode It accepts base 2, 8, 10, and 16 numbers.
-
#directory? ⇒ TrueClass, FalseClass
private
Check if node is a directory.
-
#executable? ⇒ TrueClass, FalseClass
private
Check if node is executable for user.
-
#file? ⇒ TrueClass, FalseClass
private
Check if node is a file.
-
#get(segment) ⇒ Dry::Files::MemoryFileSystem::Node, NilClass
private
Get a node child.
-
#initialize(segment, mode = DEFAULT_DIRECTORY_MODE) ⇒ Dry::Files::MemoryFileSystem::Node
constructor
private
Instantiate a new node.
-
#read ⇒ String
private
Read file contents.
-
#readlines ⇒ Array<String>
private
Read file content lines.
-
#set(segment) ⇒ Object
private
Set a node child.
-
#unset(segment) ⇒ Object
private
Unset a node child.
-
#write(content) ⇒ Object
private
Write file contents IMPORTANT: This operation turns a node into a file.
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.
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
#mode ⇒ Object (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.
98 99 100 |
# File 'lib/dry/files/memory_file_system/node.rb', line 98 def mode @mode end |
#segment ⇒ Object (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.
98 99 100 |
# File 'lib/dry/files/memory_file_system/node.rb', line 98 def segment @segment end |
Class Method Details
.root ⇒ 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 root node
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.
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
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
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
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
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
128 129 130 |
# File 'lib/dry/files/memory_file_system/node.rb', line 128 def get(segment) @children&.fetch(segment, nil) end |
#read ⇒ 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
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 |
#readlines ⇒ 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.
Read file content lines
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
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
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
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 |