philiprehberger-pathname_kit 0.6.0 → 0.7.0

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: 76ddd3566c382deffcfaa116b8f24e6715f9b6a724301b3c0f1cfd8168abff7b
4
- data.tar.gz: e373af1ca11000c3512a96a6c0e7f5c7a476dc7536fb2b595bb8deb1767321b3
3
+ metadata.gz: 20819d82a21880e8492296309312b448ff83c13c08c6cabedfd1a4a9b931765a
4
+ data.tar.gz: ac0609e05c9f3ffe88267588c4c1b5554445719a0fa290521e56735324a48297
5
5
  SHA512:
6
- metadata.gz: e3c3f9a0c310b8cb644d0740b22b7242aca2b97f64dc6085026734ef762010c0d1c6a02e93450ffcaf1ca949c58c26416f6c2b600d2cfe16a3bf0dfc9b864880
7
- data.tar.gz: 9233e060d7a9cfdd6754cad84bcdbf27e961eb82bdf3ada5f26f72b4aad937eecd8ce3502408f3b1181e6b9428a2c9910b342577476e46caa83ddd354e94c782
6
+ metadata.gz: '08fd38afb8c71fb98eecdabe34daccad18236b604a32a6eb62e1c4a32611f7b61e709105e252985b8f156b0a69b3772a9de2562685475d6ded4888bad27630cd'
7
+ data.tar.gz: 41b84e7711008b394b62746cf95edd4db1ff2e8de8f735d8451abc3b520e7c10c843b1c45dc0495984aefb0faf9538f735af2d9d3d55436425e2cbe653d491ef
data/CHANGELOG.md CHANGED
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.7.0] - 2026-04-28
11
+
12
+ ### Added
13
+ - `tail(path, n = 10)` — return the last `n` lines of a file as an Array. Streams the file through a bounded ring so memory usage stays constant for large files.
14
+
10
15
  ## [0.6.0] - 2026-04-15
11
16
 
12
17
  ### Added
data/README.md CHANGED
@@ -92,6 +92,16 @@ end
92
92
  Philiprehberger::PathnameKit.size("logs/app.log") # => 4096
93
93
  ```
94
94
 
95
+ ### Tail
96
+
97
+ Read the last `n` lines of a file (defaults to 10). Memory usage is bounded
98
+ regardless of file size:
99
+
100
+ ```ruby
101
+ Philiprehberger::PathnameKit.tail("logs/app.log") # => last 10 lines
102
+ Philiprehberger::PathnameKit.tail("logs/app.log", 50) # => last 50 lines
103
+ ```
104
+
95
105
  ### Tempdir Helper
96
106
 
97
107
  ```ruby
@@ -158,6 +168,7 @@ Philiprehberger::PathnameKit.relative_to("/a/foo", "/a/bar") # => "../foo"
158
168
  | `.tempfile(ext) { \|path\| }` | Create a temp file and yield its path |
159
169
  | `.touch(path)` | Create or update a file's modification time |
160
170
  | `.line_count(path)` | Count the number of lines in a file |
171
+ | `.tail(path, n = 10)` | Return the last n lines of a file as an Array |
161
172
  | `.copy(src, dest)` | Copy file with parent directory creation |
162
173
  | `.move(src, dest)` | Move file with parent directory creation |
163
174
  | `.checksum(path, algorithm: :sha256)` | Compute file digest (md5, sha1, sha256, sha512) |
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module PathnameKit
5
- VERSION = '0.6.0'
5
+ VERSION = '0.7.0'
6
6
  end
7
7
  end
@@ -118,6 +118,29 @@ module Philiprehberger
118
118
  File.readlines(path).size
119
119
  end
120
120
 
121
+ # Return the last n lines of a file as an Array.
122
+ #
123
+ # Streams the file line by line through a bounded ring of size n so memory
124
+ # usage stays constant regardless of file size.
125
+ #
126
+ # @param path [String] the file path
127
+ # @param n [Integer] number of trailing lines to return
128
+ # @return [Array<String>] the last n lines (or fewer if the file is shorter)
129
+ # @raise [Error] if path is nil/empty, n is non-positive, or the file does not exist
130
+ def self.tail(path, n = 10)
131
+ raise Error, 'path cannot be nil' if path.nil?
132
+ raise Error, 'path cannot be empty' if path.to_s.empty?
133
+ raise Error, 'n must be positive' unless n.is_a?(Integer) && n.positive?
134
+ raise Error, "file not found: #{path}" unless File.exist?(path)
135
+
136
+ buffer = []
137
+ File.foreach(path) do |line|
138
+ buffer << line
139
+ buffer.shift if buffer.size > n
140
+ end
141
+ buffer
142
+ end
143
+
121
144
  # Copies a file to a destination, creating parent directories as needed.
122
145
  #
123
146
  # @param src [String] source file path
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-pathname_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-15 00:00:00.000000000 Z
11
+ date: 2026-04-29 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Pathname utility library providing atomic writes, safe deletes, directory
14
14
  creation, glob-based file finding, tempfile helpers, copy, move, checksum, append,