Class: Staticky::Files::MemoryFileSystem::Node
- Inherits:
-
Object
- Object
- Staticky::Files::MemoryFileSystem::Node
- Defined in:
- lib/staticky/files/memory_file_system/node.rb
Overview
Memory file system node (directory or file)
File modes implementation inspired by www.calleluks.com/flags-bitmasks-and-unix-file-system-permissions-in-ruby/
Constant Summary collapse
- MODE_USER_READ =
0b100000000
- MODE_USER_WRITE =
0b010000000
- MODE_USER_EXECUTE =
0b001000000
- MODE_GROUP_READ =
0b000100000
- MODE_GROUP_WRITE =
0b000010000
- MODE_GROUP_EXECUTE =
0b000001000
- MODE_OTHERS_READ =
0b000000100
- MODE_OTHERS_WRITE =
0b000000010
- MODE_OTHERS_EXECUTE =
0b000000001
- DEFAULT_DIRECTORY_MODE =
Default directory mode: 0755
MODE_USER_READ | MODE_USER_WRITE | MODE_USER_EXECUTE | MODE_GROUP_READ | MODE_GROUP_EXECUTE | MODE_OTHERS_READ | MODE_GROUP_EXECUTE
- DEFAULT_FILE_MODE =
Default file mode: 0644
MODE_USER_READ | MODE_USER_WRITE | MODE_GROUP_READ | MODE_OTHERS_READ
- MODE_BASE =
16
- ROOT_PATH =
"/"
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#segment ⇒ Object
readonly
Returns the value of attribute segment.
Class Method Summary collapse
-
.root ⇒ Staticky::Files::MemoryFileSystem::Node
Instantiate a root node.
Instance Method Summary collapse
- #array_to_string(content) ⇒ Object
-
#chmod=(mode) ⇒ Object
Set UNIX mode It accepts base 2, 8, 10, and 16 numbers.
-
#directory? ⇒ TrueClass, FalseClass
Check if node is a directory.
-
#executable? ⇒ TrueClass, FalseClass
Check if node is executable for user.
-
#file? ⇒ TrueClass, FalseClass
Check if node is a file.
-
#get(segment) ⇒ Staticky::Files::MemoryFileSystem::Node, NilClass
Get a node child.
-
#initialize(segment, mode = DEFAULT_DIRECTORY_MODE) ⇒ Staticky::Files::MemoryFileSystem::Node
constructor
Instantiate a new node.
-
#read ⇒ String
Read file contents.
-
#readlines ⇒ Array<String>
Read file content lines.
-
#set(segment) ⇒ Object
Set a node child.
-
#unset(segment) ⇒ Object
Unset a node child.
-
#write(content) ⇒ Object
Write file contents IMPORTANT: This operation turns a node into a file.
Constructor Details
#initialize(segment, mode = DEFAULT_DIRECTORY_MODE) ⇒ Staticky::Files::MemoryFileSystem::Node
Instantiate a new node. It’s a directory node by default.
51 52 53 54 55 56 57 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 51 def initialize(segment, mode = DEFAULT_DIRECTORY_MODE) @segment = segment @children = nil @content = nil self.chmod = mode end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
40 41 42 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 40 def children @children end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
40 41 42 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 40 def mode @mode end |
#segment ⇒ Object (readonly)
Returns the value of attribute segment.
40 41 42 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 40 def segment @segment end |
Class Method Details
.root ⇒ Staticky::Files::MemoryFileSystem::Node
Instantiate a root node
36 37 38 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 36 def self.root new(ROOT_PATH) end |
Instance Method Details
#array_to_string(content) ⇒ Object
162 163 164 165 166 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 162 def array_to_string(content) content .map { |line| line.sub(NEW_LINE_MATCHER, EMPTY_CONTENT) } .join(NEW_LINE) + NEW_LINE end |
#chmod=(mode) ⇒ Object
Set UNIX mode It accepts base 2, 8, 10, and 16 numbers
151 152 153 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 151 def chmod=(mode) @mode = mode.to_s(MODE_BASE).hex end |
#directory? ⇒ TrueClass, FalseClass
Check if node is a directory
92 93 94 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 92 def directory? !file? end |
#executable? ⇒ TrueClass, FalseClass
Check if node is executable for user
158 159 160 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 158 def executable? (mode & MODE_USER_EXECUTE).positive? end |
#file? ⇒ TrueClass, FalseClass
Check if node is a file
99 100 101 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 99 def file? !@content.nil? end |
#get(segment) ⇒ Staticky::Files::MemoryFileSystem::Node, NilClass
Get a node child
node, if found
65 66 67 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 65 def get(segment) @children&.fetch(segment, nil) end |
#read ⇒ String
Read file contents
108 109 110 111 112 113 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 108 def read raise NotMemoryFileError, segment unless file? @content.rewind @content.read end |
#readlines ⇒ Array<String>
Read file content lines
120 121 122 123 124 125 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 120 def readlines raise NotMemoryFileError, segment unless file? @content.rewind @content.readlines end |
#set(segment) ⇒ Object
Set a node child
72 73 74 75 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 72 def set(segment) @children ||= {} @children[segment] ||= self.class.new(segment) end |
#unset(segment) ⇒ Object
Unset a node child
82 83 84 85 86 87 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 82 def unset(segment) @children ||= {} raise UnknownMemoryNodeError, segment unless @children.key?(segment) @children.delete(segment) end |
#write(content) ⇒ Object
Write file contents IMPORTANT: This operation turns a node into a file
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/staticky/files/memory_file_system/node.rb', line 133 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 |