Class: Staticky::Files::MemoryFileSystem::Node

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segment, mode = DEFAULT_DIRECTORY_MODE) ⇒ Staticky::Files::MemoryFileSystem::Node

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=


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

#childrenObject (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

#modeObject (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

#segmentObject (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

.rootStaticky::Files::MemoryFileSystem::Node

Instantiate a root node

Returns:



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

Parameters:

  • mode (Integer)

    the file mode



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

Returns:

  • (TrueClass, FalseClass)

    the result of the check



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

Returns:

  • (TrueClass, FalseClass)

    the result of the check



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

Returns:

  • (TrueClass, FalseClass)

    the result of the check



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

Parameters:

  • segment (String)

    the child path segment

Returns:



65
66
67
# File 'lib/staticky/files/memory_file_system/node.rb', line 65

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

#readString

Read file contents

Returns:

  • (String)

    the file contents

Raises:



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

#readlinesArray<String>

Read file content lines

Returns:

  • (Array<String>)

    the file content lines

Raises:



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

Parameters:

  • segment (String)

    the child path segment



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

Parameters:

  • segment (String)

    the child path segment

Raises:



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

Parameters:

  • content (String, Array<String>)

    the file content

Raises:



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