readlines 1.5 → 1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +129 -278
- data/lib/readlines/read.rb +34 -2
- data/lib/readlines/readlines/error.rb +27 -5
- data/lib/readlines/readlines/filter.rb +46 -0
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '01780b8ba53d712f49e869f05f707205cd2cc6206fe004a05c89b6adba755f8e'
|
4
|
+
data.tar.gz: 9af6c1d19859490a0727bc0e4e7a4e51810f0acc8d49c003ebdb7dc29b44fc41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05b2d6972fcd9f373d730d37678c0ab726fc9d8e3a07c37d1b9864832b3d6657bf5ceb46a786ea24de0c7e67904861b559d32ae8b3ff163af9ad96319a0c7564
|
7
|
+
data.tar.gz: 637d2e9a9dca27799fa10504ce4c570095e6e773e07c9c3d90388f747ebc313650b8f1b8ac74c093d351e5e64228d0e06c13a44f9fc38dc2f8d4337e6e7a9631
|
data/README.md
CHANGED
@@ -5,346 +5,197 @@ Readlines is a powerful and versatile Ruby library for file manipulation and pro
|
|
5
5
|
|
6
6
|
## Features
|
7
7
|
|
8
|
-
- Read the entire file content or specific parts
|
9
|
-
- Search for
|
10
|
-
-
|
11
|
-
- Replace specific patterns
|
12
|
-
- Remove empty lines
|
13
|
-
- Sort
|
14
|
-
- Split
|
15
|
-
- Merge multiple files
|
16
|
-
-
|
17
|
-
- Replace special characters
|
18
|
-
-
|
19
|
-
-
|
20
|
-
-
|
21
|
-
-
|
22
|
-
-
|
23
|
-
-
|
24
|
-
-
|
25
|
-
-
|
26
|
-
-
|
27
|
-
-
|
28
|
-
- Search
|
29
|
-
-
|
30
|
-
-
|
31
|
-
-
|
32
|
-
-
|
33
|
-
- Validate the file contents against a set of rules.
|
34
|
-
- Remove duplicate lines from the file.
|
35
|
-
- Get statistics about the file (number of lines, characters, words).
|
36
|
-
- Search for logical patterns in the file using AND and OR operators.
|
37
|
-
- Split the file based on a specified pattern.
|
38
|
-
- Delete specific columns from a CSV file.
|
39
|
-
- Replace a specific value in a CSV column.
|
40
|
-
- Check for spelling errors in the file using a dictionary.
|
41
|
-
- Encrypt the file contents using a key.
|
42
|
-
- Decrypt the encrypted file contents using a key.
|
43
|
-
- Get the file size in different units (bytes, kilobytes, megabytes, gigabytes).
|
8
|
+
- **File Reading**: Read the entire file content or specific parts.
|
9
|
+
- **Search Operations**: Search for values and display matching line numbers.
|
10
|
+
- **Line Counting**: Count lines based on keywords or delimiters.
|
11
|
+
- **Pattern Replacement**: Replace specific patterns or multiple patterns at once.
|
12
|
+
- **Delete Empty Lines**: Remove empty lines.
|
13
|
+
- **Alphabetical Sorting**: Sort lines alphabetically.
|
14
|
+
- **File Splitting**: Split files into parts or by size.
|
15
|
+
- **File Merging**: Merge multiple files with a separator.
|
16
|
+
- **Encoding Conversion**: Convert file encoding.
|
17
|
+
- **Character Replacement**: Replace special characters or unwanted characters.
|
18
|
+
- **Format Conversion**: Convert the file to txt, csv, or json.
|
19
|
+
- **Multiple Pattern Search**: Search and count multiple patterns.
|
20
|
+
- **Delimiter-Based Splitting**: Split files by delimiter or pattern.
|
21
|
+
- **Pattern Checking**: Check if patterns exist.
|
22
|
+
- **Delete Specific Lines**: Delete lines, specific columns in CSV, and duplicates.
|
23
|
+
- **Reverse Content**: Reverse the file’s content.
|
24
|
+
- **Range-Based Search**: Search within a specific line range.
|
25
|
+
- **Character and Word Counting**: Count characters or words.
|
26
|
+
- **Pattern Extraction**: Extract specific patterns.
|
27
|
+
- **Content Validation**: Validate against custom rules.
|
28
|
+
- **Logical Pattern Search**: AND/OR based pattern search.
|
29
|
+
- **Spelling Check**: Detect spelling errors with a dictionary.
|
30
|
+
- **Encryption/Decryption**: Encrypt and decrypt content.
|
31
|
+
- **File Size Calculation**: Display size in bytes, KB, MB, or GB.
|
32
|
+
- **Filter Operation**: Delete or replace lines based on matching criteria (`:start`, `:body`, or `:end`).
|
44
33
|
|
45
34
|
## Installation
|
46
35
|
|
47
|
-
To use the Readlines library in your Ruby project,
|
36
|
+
To use the Readlines library in your Ruby project, install it via RubyGems:
|
48
37
|
|
49
|
-
```
|
38
|
+
```bash
|
50
39
|
gem install readlines
|
51
40
|
```
|
52
41
|
|
53
|
-
|
42
|
+
Or add it to your Gemfile:
|
54
43
|
|
55
44
|
```ruby
|
56
45
|
gem 'readlines'
|
57
46
|
```
|
58
47
|
|
59
|
-
|
48
|
+
Run `bundle install` to install the library and its dependencies.
|
60
49
|
|
61
50
|
## Usage
|
62
51
|
|
63
|
-
|
52
|
+
Start by requiring `readlines` in your Ruby script:
|
64
53
|
|
65
54
|
```ruby
|
66
|
-
require 'readlines'
|
55
|
+
require 'readlines/read'
|
67
56
|
```
|
68
57
|
|
69
|
-
Create an instance of the `
|
58
|
+
Create an instance of the `Readlines::ReadDuc` class with the filename (or full path):
|
70
59
|
|
71
60
|
```ruby
|
72
|
-
|
73
|
-
read =
|
61
|
+
file_name = 'file.txt'
|
62
|
+
read = Readlines::ReadDuc.new(file_name)
|
74
63
|
```
|
75
64
|
|
76
|
-
|
65
|
+
### Examples
|
66
|
+
|
67
|
+
#### Search for a specific word and count occurrences
|
68
|
+
|
77
69
|
```ruby
|
78
|
-
#
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
puts read.line_count
|
70
|
+
# Search for the word "example" in the file
|
71
|
+
read = Readlines::ReadDuc.new('data.txt')
|
72
|
+
results = read.search_about('example', show_lines: true)
|
73
|
+
|
74
|
+
# Display the number of occurrences
|
75
|
+
puts "Number of occurrences: #{results.count}"
|
85
76
|
```
|
77
|
+
|
78
|
+
#### Basic Operations
|
79
|
+
|
86
80
|
```ruby
|
87
81
|
# Read the entire file content
|
88
82
|
content = read.read_lines
|
89
83
|
puts content
|
90
84
|
```
|
85
|
+
|
86
|
+
#### Counting Operations
|
87
|
+
|
91
88
|
```ruby
|
92
|
-
#
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
# Count
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
puts
|
103
|
-
```
|
104
|
-
```ruby
|
105
|
-
# Replace specific patterns in the file with replacement text
|
106
|
-
pattern = /example/
|
107
|
-
replacement = 'new_example'
|
108
|
-
updated_content = read.replace(pattern, replacement)
|
109
|
-
puts updated_content
|
89
|
+
# Count characters in the file or a specific line
|
90
|
+
total_characters = read.character_count
|
91
|
+
puts total_characters
|
92
|
+
line_characters = read.character_count(line_specific: 3)
|
93
|
+
puts line_characters
|
94
|
+
|
95
|
+
# Count words in the file or a specific line
|
96
|
+
total_words = read.word_count
|
97
|
+
puts total_words
|
98
|
+
line_words = read.word_count(line_specific: 3)
|
99
|
+
puts line_words
|
110
100
|
```
|
101
|
+
|
102
|
+
#### Replacement Operations
|
103
|
+
|
111
104
|
```ruby
|
112
|
-
#
|
113
|
-
updated_content = read.
|
105
|
+
# Replace specific patterns in the file
|
106
|
+
updated_content = read.replace(/example/, 'new_example')
|
114
107
|
puts updated_content
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
puts sorted_content
|
120
|
-
```
|
121
|
-
```ruby
|
122
|
-
# Split the file into a specified number of parts
|
123
|
-
num_parts = 3
|
124
|
-
read.split_file(num_parts)
|
125
|
-
```
|
126
|
-
```ruby
|
127
|
-
# Merge multiple files into a single file
|
128
|
-
file_paths = ['/path/to/file1.txt', '/path/to/file2.txt']
|
129
|
-
merged_file = read.merge_files(file_paths)
|
130
|
-
puts merged_file
|
131
|
-
```
|
132
|
-
```ruby
|
133
|
-
# Convert the encoding of the file from one encoding to another
|
134
|
-
from_encoding = 'UTF-8'
|
135
|
-
to_encoding = 'ISO-8859-1'
|
136
|
-
read.convert_encoding(from_encoding, to_encoding)
|
137
|
-
```
|
138
|
-
```ruby
|
139
|
-
# Replace special characters in the file with a replacement string
|
140
|
-
replacement = '_'
|
141
|
-
updated_content = read.replace_special_characters(replacement)
|
108
|
+
|
109
|
+
# Replace multiple patterns at once
|
110
|
+
pattern_replacement = {/example/ => 'new_example', /test/ => 'new_test'}
|
111
|
+
updated_content = read.replace_multiple_patterns(pattern_replacement)
|
142
112
|
puts updated_content
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
113
|
+
|
114
|
+
# Replace characters in a CSV column
|
115
|
+
updated_csv = read.replace_csv_value(2, 'old_value', 'new_value')
|
116
|
+
puts updated_csv
|
117
|
+
|
118
|
+
# Replace special characters with a specified replacement string
|
119
|
+
updated_content = read.replace_special_characters('_')
|
148
120
|
puts updated_content
|
149
121
|
```
|
122
|
+
|
123
|
+
#### Deletion Operations
|
124
|
+
|
150
125
|
```ruby
|
151
|
-
#
|
152
|
-
|
153
|
-
read.split_file_by_size(part_size)
|
154
|
-
```
|
155
|
-
```ruby
|
156
|
-
# Convert the file to a specified format
|
157
|
-
format = 'csv'
|
158
|
-
converted_file = read.convert_to_format(format)
|
159
|
-
puts converted_file
|
160
|
-
```
|
161
|
-
```ruby
|
162
|
-
# Search for multiple patterns in the file and count occurrences of each pattern
|
163
|
-
patterns = [/example/, /pattern/]
|
164
|
-
result = read.search_multiple_patterns(patterns)
|
165
|
-
puts result
|
166
|
-
```
|
167
|
-
```ruby
|
168
|
-
# Replace multiple patterns in the file in a single operation
|
169
|
-
pattern_replacement_hash = {/example/ => 'new_example', /pattern/ => 'new_pattern'}
|
170
|
-
updated_content = read.replace_multiple_patterns(pattern_replacement_hash)
|
126
|
+
# Delete empty lines from the file
|
127
|
+
updated_content = read.delete_empty_lines
|
171
128
|
puts updated_content
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
delimiter = '---'
|
176
|
-
read.split_by_delimiter(delimiter)
|
177
|
-
```
|
178
|
-
```ruby
|
179
|
-
# Merge multiple files into a single file with a specified separator between each file's content
|
180
|
-
file_paths = ['/path/to/file1.txt', '/path/to/file2.txt']
|
181
|
-
separator = '---'
|
182
|
-
merged_file = read.merge_files(file_paths, separator: separator)
|
183
|
-
puts merged_file
|
184
|
-
```
|
185
|
-
```ruby
|
186
|
-
# Check if a specific pattern exists in the file
|
187
|
-
pattern = /example/
|
188
|
-
exists = read.pattern_exists?(pattern)
|
189
|
-
puts exists
|
190
|
-
```
|
191
|
-
```ruby
|
192
|
-
# Delete specific lines from the file with the option to remove resulting empty lines
|
193
|
-
line_numbers = [2, 4, 6]
|
194
|
-
updated_content = read.delete_lines(line_numbers, delete_space: true)
|
129
|
+
|
130
|
+
# Delete specific lines with the option to remove empty lines
|
131
|
+
updated_content = read.delete_lines([2, 4, 6], delete_space: true)
|
195
132
|
puts updated_content
|
196
|
-
```
|
197
|
-
```ruby
|
198
|
-
# Reverse the order of the file contents
|
199
|
-
reversed_content = read.reverse_content
|
200
|
-
puts reversed_content
|
201
|
-
```
|
202
|
-
```ruby
|
203
|
-
# Search for a pattern within a specified range of lines
|
204
|
-
start_line = 5
|
205
|
-
end_line = 10
|
206
|
-
pattern = /example/
|
207
|
-
matched_lines = read.search_in_range(start_line, end_line, pattern)
|
208
|
-
puts matched_lines
|
209
|
-
```
|
210
|
-
```ruby
|
211
|
-
# Count the number of characters in the file or in a specific line
|
212
|
-
total_characters = read.character_count
|
213
|
-
puts total_characters
|
214
133
|
|
215
|
-
|
216
|
-
|
217
|
-
puts
|
218
|
-
```
|
219
|
-
```ruby
|
220
|
-
# Count the number of words in the file or in a specific line
|
221
|
-
total_words = read.word_count
|
222
|
-
puts total_words
|
134
|
+
# Delete unwanted characters
|
135
|
+
updated_content = read.delete_unwanted_characters('!@#$%^')
|
136
|
+
puts updated_content
|
223
137
|
|
224
|
-
|
225
|
-
line_words = read.word_count(line_specific: line_number)
|
226
|
-
puts line_words
|
227
|
-
```
|
228
|
-
```ruby
|
229
|
-
# Extract specific patterns from the file
|
230
|
-
patterns = [/email:\s*\S+/, /phone:\s*\S+/]
|
231
|
-
extracted_data = read.extract_patterns(patterns)
|
232
|
-
puts extracted_data
|
233
|
-
```
|
234
|
-
```ruby
|
235
|
-
# Convert the file contents into an array of lines
|
236
|
-
lines_array = read.convert_to_array
|
237
|
-
puts lines_array
|
238
|
-
```
|
239
|
-
```ruby
|
240
|
-
# Validate the file contents against a set of rules
|
241
|
-
rules = [/^[A-Z]/, /\b\d{3}-\d{3}-\d{4}\b/]
|
242
|
-
is_valid = read.validate_content(rules)
|
243
|
-
puts is_valid
|
244
|
-
```
|
245
|
-
```ruby
|
246
|
-
# Remove duplicate lines from the file
|
138
|
+
# Delete duplicate lines
|
247
139
|
unique_content = read.delete_duplicate_lines
|
248
140
|
puts unique_content
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
stats = read.file_statistics
|
253
|
-
puts stats
|
254
|
-
```
|
255
|
-
```ruby
|
256
|
-
# Search for logical patterns in the file using AND and OR operators
|
257
|
-
patterns = [/example/, /pattern/]
|
258
|
-
and_matched_lines = read.search_logical_patterns(patterns, 'AND')
|
259
|
-
puts and_matched_lines
|
260
|
-
```
|
261
|
-
```ruby
|
262
|
-
or_matched_lines = read.search_logical_patterns(patterns, 'OR')
|
263
|
-
puts or_matched_lines
|
264
|
-
```
|
265
|
-
```ruby
|
266
|
-
# Split the file based on a specified pattern
|
267
|
-
pattern = /Chapter \d+:/
|
268
|
-
read.split_by_pattern(pattern)
|
269
|
-
```
|
270
|
-
```ruby
|
271
|
-
# Delete specific columns from a CSV file
|
272
|
-
column_indices = [1, 3]
|
273
|
-
updated_csv = read.delete_csv_columns(column_indices)
|
274
|
-
puts updated_csv
|
275
|
-
```
|
276
|
-
```ruby
|
277
|
-
# Replace a specific value in a CSV column
|
278
|
-
column_index = 2
|
279
|
-
old_value = 'old_value'
|
280
|
-
new_value = 'new_value'
|
281
|
-
updated_csv = read.replace_csv_value(column_index, old_value, new_value)
|
141
|
+
|
142
|
+
# Delete specific columns in a CSV file
|
143
|
+
updated_csv = read.delete_csv_columns([1, 3])
|
282
144
|
puts updated_csv
|
283
145
|
```
|
284
|
-
```ruby
|
285
|
-
# Check for spelling errors in the file using a dictionary
|
286
|
-
dictionary = ['example', 'pattern', 'file']
|
287
|
-
misspelled_words = read.check_spelling(dictionary)
|
288
|
-
puts misspelled_words
|
289
|
-
```
|
290
|
-
```ruby
|
291
|
-
# Encrypt the file contents using a key
|
292
|
-
key = 5
|
293
|
-
encrypted_file = read.encrypt_content(key)
|
294
|
-
puts encrypted_file
|
295
|
-
```
|
296
|
-
```ruby
|
297
|
-
# Decrypt the encrypted file contents using a key
|
298
|
-
encrypted_file_path = '/path/to/encrypted_file.txt'
|
299
|
-
decrypted_file = read.decrypt_content(key, encrypted_file_path)
|
300
|
-
puts decrypted_file
|
301
|
-
```
|
302
|
-
```ruby
|
303
|
-
# Get the file size in different units
|
304
|
-
file_size_bytes = read.file_size
|
305
|
-
puts file_size_bytes
|
306
146
|
|
307
|
-
|
308
|
-
puts file_size_kb
|
147
|
+
#### File Splitting and Merging
|
309
148
|
|
310
|
-
|
311
|
-
|
149
|
+
```ruby
|
150
|
+
# Split the file into 3 parts
|
151
|
+
read.split_file(3)
|
312
152
|
|
313
|
-
|
314
|
-
|
315
|
-
```
|
153
|
+
# Split by size (in bytes)
|
154
|
+
read.split_file_by_size(1024) # 1 KB
|
316
155
|
|
317
|
-
|
156
|
+
# Split by delimiter
|
157
|
+
read.split_by_delimiter('---')
|
318
158
|
|
319
|
-
|
159
|
+
# Split by a pattern
|
160
|
+
read.split_by_pattern(/Chapter \d+/)
|
320
161
|
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
- `Readlines::InvalidUnitError`: Raised when an invalid unit is provided for file size conversion.
|
162
|
+
# Merge multiple files
|
163
|
+
merged_file = read.merge_files(['/path/to/file1.txt', '/path/to/file2.txt'])
|
164
|
+
puts merged_file
|
325
165
|
|
326
|
-
|
166
|
+
# Merge files with a separator
|
167
|
+
merged_file_with_separator = read.merge_files(['/file1.txt', '/file2.txt'], separator: '---')
|
168
|
+
puts merged_file_with_separator
|
169
|
+
```
|
327
170
|
|
328
|
-
|
171
|
+
#### Encoding and Format Conversion
|
329
172
|
|
330
|
-
|
173
|
+
```ruby
|
174
|
+
# Convert encoding
|
175
|
+
read.convert_encoding('UTF-8', 'ISO-8859-1')
|
331
176
|
|
332
|
-
|
177
|
+
# Convert file format to CSV
|
178
|
+
converted_csv = read.convert_to_format('csv')
|
179
|
+
puts converted_csv
|
180
|
+
```
|
333
181
|
|
334
|
-
|
182
|
+
#### Error Handling
|
335
183
|
|
336
|
-
The Readlines library
|
184
|
+
The Readlines library provides custom error classes for handling specific exceptions:
|
337
185
|
|
338
|
-
|
186
|
+
- **Readlines::Error**: General error class for all exceptions.
|
187
|
+
- **Readlines::NotFoundError**: Raised when the file is not found.
|
188
|
+
- **Readlines::MissingFilePathError**: Raised when a file path is missing or empty.
|
189
|
+
- **Readlines::InvalidUnitError**: Raised for invalid file size units.
|
339
190
|
|
340
|
-
|
191
|
+
These error classes help in handling and debugging exceptions that may occur while using the library.
|
341
192
|
|
342
|
-
|
343
|
-
- GitHub: [Maven's GitHub](https://github.com/abo5)
|
193
|
+
## Contributing
|
344
194
|
|
345
|
-
|
195
|
+
Contributions are welcome! If you find bugs, have suggestions, or want to add features, please open an issue or submit a pull request on the [GitHub repository](https://github.com/abo5/readlines).
|
346
196
|
|
197
|
+
When contributing, please ensure that your code follows the existing coding style and includes appropriate tests.
|
347
198
|
|
199
|
+
## License
|
348
200
|
|
349
|
-
|
350
|
-
# readlines
|
201
|
+
The Readlines library is open-source and released under the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/readlines/read.rb
CHANGED
@@ -13,6 +13,7 @@ require_relative 'readlines/replace'
|
|
13
13
|
require_relative 'readlines/search'
|
14
14
|
require_relative 'readlines/sort'
|
15
15
|
require_relative 'readlines/split'
|
16
|
+
require_relative 'readlines/filter'
|
16
17
|
require 'fileutils'
|
17
18
|
require 'json'
|
18
19
|
|
@@ -24,6 +25,7 @@ module Readlines
|
|
24
25
|
include Readlines::Search
|
25
26
|
include Readlines::Replace
|
26
27
|
include Readlines::File
|
28
|
+
include Readlines::Filter
|
27
29
|
include Readlines::Merge
|
28
30
|
include Readlines::Info
|
29
31
|
include Readlines::Pattern
|
@@ -33,12 +35,17 @@ module Readlines
|
|
33
35
|
attr_reader :file_path
|
34
36
|
|
35
37
|
def initialize(file_path, count: nil, split: nil)
|
38
|
+
file_path = ::File.join(Dir.pwd, file_path) unless ::File.absolute_path?(file_path)
|
39
|
+
|
40
|
+
raise Readlines::Error::MissingFilePathError, 'File not found at provided path' unless ::File.exist?(file_path)
|
36
41
|
raise Readlines::Error::MissingFilePathError, 'You should provide a file path, e.g., "/path/to/file.txt"' if file_path.nil? || file_path.empty?
|
37
|
-
|
42
|
+
|
43
|
+
|
38
44
|
@file_path = file_path
|
39
45
|
@count_keyword = count
|
40
46
|
@split_delimiter = split
|
41
47
|
end
|
48
|
+
|
42
49
|
|
43
50
|
include Readlines::Count
|
44
51
|
include Readlines::Delete
|
@@ -46,12 +53,14 @@ module Readlines
|
|
46
53
|
include Readlines::Search
|
47
54
|
include Readlines::Replace
|
48
55
|
include Readlines::File
|
56
|
+
include Readlines::Filter
|
49
57
|
include Readlines::Merge
|
50
58
|
include Readlines::Info
|
51
59
|
include Readlines::Pattern
|
52
60
|
include Readlines::Sort
|
53
61
|
include Readlines::Split
|
54
62
|
include Readlines::Content
|
63
|
+
include Readlines::Filter
|
55
64
|
|
56
65
|
# Count operations
|
57
66
|
def line_count
|
@@ -220,6 +229,20 @@ module Readlines
|
|
220
229
|
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
221
230
|
file_statistics_now()
|
222
231
|
end
|
232
|
+
|
233
|
+
## Replace and Delete lines from the file
|
234
|
+
def filter(value, query, operation, replacement = nil)
|
235
|
+
raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
|
236
|
+
|
237
|
+
processed_lines = []
|
238
|
+
::File.foreach(@file_path) do |line|
|
239
|
+
result = apply_filter(line, value, query, operation, replacement)
|
240
|
+
processed_lines << result if result
|
241
|
+
end
|
242
|
+
|
243
|
+
::File.write(@file_path, processed_lines.join)
|
244
|
+
processed_lines
|
245
|
+
end
|
223
246
|
|
224
247
|
# Pattern operations
|
225
248
|
def pattern_exists?(pattern)
|
@@ -260,4 +283,13 @@ module Readlines
|
|
260
283
|
decrypt_content_now(key, encrypted_file_path)
|
261
284
|
end
|
262
285
|
end
|
263
|
-
|
286
|
+
|
287
|
+
def filter(file_path, value, query, operation, replacement = nil)
|
288
|
+
processed_lines = []
|
289
|
+
File.foreach(file_path) do |line|
|
290
|
+
result = filter(line, value, query, operation, replacement)
|
291
|
+
processed_lines << result if result # Only add if result is not nil (for delete operation)
|
292
|
+
end
|
293
|
+
processed_lines
|
294
|
+
end
|
295
|
+
end
|
@@ -2,12 +2,34 @@
|
|
2
2
|
|
3
3
|
module Readlines
|
4
4
|
module Error
|
5
|
-
class
|
5
|
+
class BaseError < StandardError
|
6
|
+
def initialize(message = "An error occurred in Readlines")
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
6
10
|
|
7
|
-
class NotFoundError <
|
11
|
+
class NotFoundError < BaseError
|
12
|
+
def initialize(message = "File not found.")
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
8
16
|
|
9
|
-
class MissingFilePathError <
|
17
|
+
class MissingFilePathError < BaseError
|
18
|
+
def initialize(message = "File path is missing.")
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
10
22
|
|
11
|
-
class InvalidUnitError <
|
23
|
+
class InvalidUnitError < BaseError
|
24
|
+
def initialize(message = "Invalid unit provided.")
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class ArgumentError < BaseError
|
30
|
+
def initialize(message = "Invalid argument provided.")
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
12
34
|
end
|
13
|
-
end
|
35
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# readlines/lib/readlines/readlines/filter.rb
|
2
|
+
# Replace and Delete lines from the file
|
3
|
+
require_relative 'delete'
|
4
|
+
require_relative 'replace'
|
5
|
+
|
6
|
+
module Readlines
|
7
|
+
module Filter
|
8
|
+
def apply_filter(line, value, query, operation, replacement = nil)
|
9
|
+
case query
|
10
|
+
when :start
|
11
|
+
matched = line.strip.start_with?(value.to_s)
|
12
|
+
when :body
|
13
|
+
matched = line.include?(value.to_s)
|
14
|
+
when :end
|
15
|
+
matched = line.strip.end_with?(value.to_s)
|
16
|
+
else
|
17
|
+
raise Readlines::Error::ArgumentError, "Invalid query type. Use :start, :body, or :end."
|
18
|
+
end
|
19
|
+
|
20
|
+
if matched
|
21
|
+
case operation
|
22
|
+
when :delete
|
23
|
+
return nil
|
24
|
+
when :replace
|
25
|
+
raise Readlines::Error::ArgumentError, "Replacement value is required for replace operation." if replacement.nil?
|
26
|
+
|
27
|
+
pattern = case query
|
28
|
+
when :start
|
29
|
+
/^#{Regexp.quote(value.to_s)}/
|
30
|
+
when :body
|
31
|
+
/#{Regexp.quote(value.to_s)}/
|
32
|
+
when :end
|
33
|
+
/#{Regexp.quote(value.to_s)}$/
|
34
|
+
end
|
35
|
+
|
36
|
+
return line.gsub(pattern, replacement)
|
37
|
+
else
|
38
|
+
raise Readlines::Error::ArgumentError, "Invalid operation type. Use :delete or :replace."
|
39
|
+
end
|
40
|
+
else
|
41
|
+
line
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: readlines
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.7'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maven
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|
@@ -84,7 +84,7 @@ description: Readlines is a Ruby library offering advanced tools for reading, wr
|
|
84
84
|
and manipulating text files. It supports searching, replacing, sorting, splitting,
|
85
85
|
merging, handling CSVs, encryption, and more. Perfect for developers needing efficient
|
86
86
|
and complex file operations.
|
87
|
-
email:
|
87
|
+
email:
|
88
88
|
executables: []
|
89
89
|
extensions: []
|
90
90
|
extra_rdoc_files: []
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- lib/readlines/readlines/delete.rb
|
100
100
|
- lib/readlines/readlines/error.rb
|
101
101
|
- lib/readlines/readlines/file.rb
|
102
|
+
- lib/readlines/readlines/filter.rb
|
102
103
|
- lib/readlines/readlines/info.rb
|
103
104
|
- lib/readlines/readlines/merge.rb
|
104
105
|
- lib/readlines/readlines/pattern.rb
|
@@ -111,7 +112,7 @@ homepage: https://github.com/Abo5/readlines
|
|
111
112
|
licenses:
|
112
113
|
- MIT
|
113
114
|
metadata: {}
|
114
|
-
post_install_message:
|
115
|
+
post_install_message:
|
115
116
|
rdoc_options: []
|
116
117
|
require_paths:
|
117
118
|
- lib
|
@@ -127,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
128
|
version: '0'
|
128
129
|
requirements: []
|
129
130
|
rubygems_version: 3.3.3
|
130
|
-
signing_key:
|
131
|
+
signing_key:
|
131
132
|
specification_version: 4
|
132
133
|
summary: A Ruby library for advanced text file manipulation and processing.
|
133
134
|
test_files: []
|