staticky-files 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff9719d82a9de8cde1777bad0d9c2bbcfc9b2aafd747cbdef4e550041a1b2ce9
4
- data.tar.gz: 78d26cfd1220fd7ce8abde206d60f2f38d152ac35b9b45246f5a3026c302cb54
3
+ metadata.gz: f934ea2685768e6efe873cc81056f3409f2c154cfcd141991db8e43ba012f5e1
4
+ data.tar.gz: 536412a65bbc92a0ee48192f1e947452e4729044b12a3183d72ce0caac8aade8
5
5
  SHA512:
6
- metadata.gz: 7b60b8cf0c46b51fd200edeb6bf6441422318d6a7f4b9ddf2183758c5341ce03b9ac00596aaa2adb99026c928ebcdcd0593ccd990ab32fdbc95de12b787a001c
7
- data.tar.gz: e7d118cf6aa846809deab019a02f538284e52ea77d5c99db88f4bab2063bceae676ba7cb76db86e363ed8ad61658b0be2aef42535b8d62686b83938b66031e88
6
+ metadata.gz: effef9f068f1caafd8907f44be7b30499cafe5340420df601a12b42ccd3f211aec73d4c33535191e8f0f2476c47acd6497bb09034ce54bd6647357d70cce9272
7
+ data.tar.gz: 7b67b101ce0f91e8edbf88b26cf5521cfd92c22a420ec944929d3fba03348fdba3cbab6a9a27263450b51140d6bc9fb0b62043c22a283a4f46ae2998ba679b93
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## 0.2.1 - 2025-10-09
4
+
5
+ - Fixes `#glob` for `MemoryFileSystem` not handling empty folders
6
+
7
+ ## 0.2.0 - 2025-10-09
8
+
9
+ - Adds `#glob` for more directory file access
10
+ - Implements permissions for `#write`, `#readlines`, `#mkdir`, `#mkdir_p` for
11
+ `MemoryFileSystem`
12
+
3
13
  ## [0.1.0] - 2024-08-14
4
14
 
5
15
  - Initial release
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  This is a hard fork of `dry-files` that adds extensions for:
4
4
 
5
5
  - `entries` to walk entries in a directory
6
+ - `glob` to search for a glob pattern
6
7
 
7
8
  ## Installation
8
9
 
@@ -312,6 +312,16 @@ module Staticky
312
312
  end
313
313
  end
314
314
 
315
+ # Returns an array of file paths that match the given pattern.
316
+ #
317
+ # @param pattern [String] the glob pattern to match
318
+ # @return [Array<String>] the matching file paths
319
+ def glob(pattern)
320
+ with_error_handling do
321
+ Dir.glob(pattern)
322
+ end
323
+ end
324
+
315
325
  private
316
326
 
317
327
  # Catch `SystemCallError` and re-raise a `Staticky::Files::IOError`.
@@ -20,15 +20,23 @@ module Staticky
20
20
  MODE_OTHERS_EXECUTE = 0b000000001
21
21
 
22
22
  # Default directory mode: 0755
23
- DEFAULT_DIRECTORY_MODE = MODE_USER_READ | MODE_USER_WRITE | MODE_USER_EXECUTE |
24
- MODE_GROUP_READ | MODE_GROUP_EXECUTE |
25
- MODE_OTHERS_READ | MODE_GROUP_EXECUTE
23
+ DEFAULT_DIRECTORY_MODE = MODE_USER_READ |
24
+ MODE_USER_WRITE |
25
+ MODE_USER_EXECUTE |
26
+ MODE_GROUP_READ |
27
+ MODE_GROUP_EXECUTE |
28
+ MODE_OTHERS_READ |
29
+ MODE_GROUP_EXECUTE
26
30
 
27
31
  # Default file mode: 0644
28
- DEFAULT_FILE_MODE = MODE_USER_READ | MODE_USER_WRITE | MODE_GROUP_READ | MODE_OTHERS_READ
32
+ DEFAULT_FILE_MODE = MODE_USER_READ |
33
+ MODE_USER_WRITE |
34
+ MODE_GROUP_READ |
35
+ MODE_OTHERS_READ
29
36
 
30
37
  MODE_BASE = 16
31
38
  ROOT_PATH = "/"
39
+ EMPTY_CONTENT = ""
32
40
 
33
41
  # Instantiate a root node
34
42
  #
@@ -132,12 +140,12 @@ module Staticky
132
140
  # @raise [Staticky::Files::NotMemoryFileError] if node isn't a file
133
141
  def write(content)
134
142
  content = case content
135
- when String
136
- content
137
- when Array
138
- array_to_string(content)
139
- when NilClass
140
- EMPTY_CONTENT
143
+ when String
144
+ content
145
+ when Array
146
+ array_to_string(content)
147
+ when NilClass
148
+ EMPTY_CONTENT
141
149
  end
142
150
 
143
151
  @content = StringIO.new(content)
@@ -155,8 +163,12 @@ module Staticky
155
163
  # Check if node is executable for user
156
164
  #
157
165
  # @return [TrueClass,FalseClass] the result of the check
166
+ def readable?
167
+ mode.anybits?(MODE_USER_READ)
168
+ end
169
+
158
170
  def executable?
159
- (mode & MODE_USER_EXECUTE).positive?
171
+ mode.anybits?(MODE_USER_EXECUTE)
160
172
  end
161
173
 
162
174
  def array_to_string(content)
@@ -59,13 +59,14 @@ module Staticky
59
59
  # @return [Array<String>] the file contents
60
60
  #
61
61
  # @raise [Staticky::Files::IOError] in case the target path is a directory
62
- # or if the file cannot be found
62
+ # or if the file cannot be found, or if the file is not readable
63
63
  def readlines(path)
64
64
  path = Path[path]
65
65
  node = find(path)
66
66
 
67
67
  raise IOError, Errno::ENOENT.new(path.to_s) if node.nil?
68
68
  raise IOError, Errno::EISDIR.new(path.to_s) if node.directory?
69
+ raise IOError, Errno::EACCES.new(path.to_s) unless node.readable?
69
70
 
70
71
  node.readlines
71
72
  end
@@ -91,12 +92,17 @@ module Staticky
91
92
  #
92
93
  # @param path [String, Array<String>] the target path
93
94
  # @param content [String, Array<String>] the content to write
95
+ #
96
+ # @raise [Staticky::Files::IOError] in case the target path is a directory
94
97
  def write(path, *content)
95
98
  path = Path[path]
99
+ raise IOError, Errno::EISDIR.new(path.to_s) if directory?(path)
100
+
96
101
  node = @root
97
102
 
98
103
  for_each_segment(path) do |segment|
99
104
  node = node.set(segment)
105
+ raise IOError, Errno::EACCES.new(path.to_s) unless node.readable?
100
106
  end
101
107
 
102
108
  node.write(*content)
@@ -164,7 +170,7 @@ module Staticky
164
170
  # @param path [String,Array<String>] the directory to create
165
171
  #
166
172
  # @raise [Staticky::Files::IOError] in case path is an already existing
167
- # file
173
+ # file, or the file system cannot be accessed
168
174
  def mkdir(path)
169
175
  path = Path[path]
170
176
  node = @root
@@ -172,6 +178,7 @@ module Staticky
172
178
  for_each_segment(path) do |segment|
173
179
  node = node.set(segment)
174
180
  raise IOError, Errno::EEXIST.new(path.to_s) if node.file?
181
+ raise IOError, Errno::EACCES.new(path.to_s) unless node.readable?
175
182
  end
176
183
  end
177
184
 
@@ -337,11 +344,33 @@ module Staticky
337
344
  [".", ".."] + Array(node.children&.keys)
338
345
  end
339
346
 
347
+ # Returns an array of file paths that match the given pattern.
348
+ #
349
+ # @param pattern [Pathname, String] the glob pattern to match
350
+ # @return [Array<String>] the matching file paths
351
+ def glob(pattern)
352
+ matches = []
353
+ traverse(@root, "/", pattern.to_s, matches)
354
+ matches
355
+ end
356
+
340
357
  private
341
358
 
342
- def for_each_segment(path, &blk)
359
+ def traverse(node, current_path, pattern, matches)
360
+ if node.file? && File.fnmatch(pattern, current_path, File::FNM_PATHNAME)
361
+ matches << current_path
362
+ elsif node.directory?
363
+ return if node.children.nil?
364
+
365
+ node.children.each do |name, child|
366
+ traverse(child, File.join(current_path, name), pattern, matches)
367
+ end
368
+ end
369
+ end
370
+
371
+ def for_each_segment(path, &)
343
372
  segments = Path.split(path)
344
- segments.each(&blk)
373
+ segments.each(&)
345
374
  end
346
375
 
347
376
  def find_directory(path)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Staticky
4
4
  class Files
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.1"
6
6
  end
7
7
  end
@@ -58,9 +58,10 @@ module Staticky
58
58
 
59
59
  # Sets UNIX permissions of the file at the given path.
60
60
  #
61
- # Accepts permissions in numeric mode only, best provided as octal numbers matching the
62
- # standard UNIX octal permission modes, such as `0o544` for a file writeable by its owner and
63
- # readable by others, or `0o755` for a file writeable by its owner and executable by everyone.
61
+ # Accepts permissions in numeric mode only, best provided as octal numbers
62
+ # matching the standard UNIX octal permission modes, such as `0o544` for a
63
+ # file writeable by its owner and readable by others, or `0o755` for a
64
+ # file writeable by its owner and executable by everyone.
64
65
  #
65
66
  # @param path [String,Pathname] the path to the file
66
67
  # @param mode [Integer] the UNIX permissions mode
@@ -69,7 +70,7 @@ module Staticky
69
70
  def chmod(path, mode)
70
71
  unless mode.is_a?(Integer)
71
72
  raise Staticky::Files::Error,
72
- "mode should be an integer (e.g. 0o755)"
73
+ "mode should be an integer (e.g. 0o755)"
73
74
  end
74
75
 
75
76
  adapter.chmod(path, mode)
@@ -128,8 +129,8 @@ module Staticky
128
129
  # @param blk [Proc] the code to execute with the target directory
129
130
  #
130
131
  # @raise [Staticky::Files::IOError] in case of I/O error
131
- def chdir(path, &blk)
132
- adapter.chdir(path, &blk)
132
+ def chdir(path, &)
133
+ adapter.chdir(path, &)
133
134
  end
134
135
 
135
136
  # Creates a directory for the given path.
@@ -837,12 +838,22 @@ module Staticky
837
838
  #
838
839
  # @raise [Staticky::Files::IOError] in case of I/O error
839
840
  #
840
- # @since 1.0.1
841
+ # @since 0.1.0
841
842
  # @api public
842
843
  def entries(path)
843
844
  adapter.entries(path)
844
845
  end
845
846
 
847
+ # Reads files matching the given glob pattern
848
+ #
849
+ # @param pattern [String,Pathname] the glob pattern
850
+ #
851
+ # @since 0.1.0
852
+ # @api public
853
+ def glob(pattern)
854
+ adapter.glob(pattern)
855
+ end
856
+
846
857
  private
847
858
 
848
859
  class Delimiter
@@ -925,9 +936,9 @@ module Staticky
925
936
  delimiter,
926
937
  count_offset = 0
927
938
  )
928
- blocks_count = content.count { |line|
939
+ blocks_count = content.count do |line|
929
940
  line.match?(delimiter.opening_matcher)
930
- } + count_offset
941
+ end + count_offset
931
942
  matching_line = content.find do |line|
932
943
  blocks_count -= 1 if line.match?(delimiter.closing_matcher)
933
944
  line if blocks_count.zero?
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: staticky-files
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nolan J Tait
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-08-15 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Better file management with adapters for testing and production
14
13
  email:
@@ -40,7 +39,6 @@ metadata:
40
39
  source_code_uri: https://github.com/nolantait/staticky-files
41
40
  changelog_uri: https://github.com/nolantait/staticky-files
42
41
  rubygems_mfa_required: 'true'
43
- post_install_message:
44
42
  rdoc_options: []
45
43
  require_paths:
46
44
  - lib
@@ -55,8 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
53
  - !ruby/object:Gem::Version
56
54
  version: '0'
57
55
  requirements: []
58
- rubygems_version: 3.5.11
59
- signing_key:
56
+ rubygems_version: 3.7.2
60
57
  specification_version: 4
61
58
  summary: Better file management with adapters for testing and production
62
59
  test_files: []