readlines 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/gem-push.yml +45 -0
  3. data/.gitignore +46 -0
  4. data/.rspec +0 -0
  5. data/CHANGELOG.md +36 -0
  6. data/CODE_OF_CONDUCT.md +0 -0
  7. data/Gemfile +5 -0
  8. data/Gemfile.lock +30 -0
  9. data/LICENSE +21 -0
  10. data/README.md +349 -0
  11. data/Rakefile +9 -0
  12. data/examples/example1.rb +13 -0
  13. data/examples/example10.rb +13 -0
  14. data/examples/example11.rb +13 -0
  15. data/examples/example12.rb +13 -0
  16. data/examples/example13.rb +13 -0
  17. data/examples/example14.rb +13 -0
  18. data/examples/example15.rb +13 -0
  19. data/examples/example16.rb +13 -0
  20. data/examples/example17.rb +13 -0
  21. data/examples/example18.rb +13 -0
  22. data/examples/example19.rb +13 -0
  23. data/examples/example2.rb +13 -0
  24. data/examples/example20.rb +13 -0
  25. data/examples/example21.rb +13 -0
  26. data/examples/example22.rb +13 -0
  27. data/examples/example23.rb +13 -0
  28. data/examples/example24.rb +13 -0
  29. data/examples/example25.rb +13 -0
  30. data/examples/example26.rb +13 -0
  31. data/examples/example27.rb +13 -0
  32. data/examples/example28.rb +13 -0
  33. data/examples/example29.rb +13 -0
  34. data/examples/example3.rb +13 -0
  35. data/examples/example30.rb +13 -0
  36. data/examples/example31.rb +13 -0
  37. data/examples/example32.rb +13 -0
  38. data/examples/example33.rb +13 -0
  39. data/examples/example34.rb +13 -0
  40. data/examples/example35.rb +13 -0
  41. data/examples/example36.rb +13 -0
  42. data/examples/example37.rb +13 -0
  43. data/examples/example38.rb +13 -0
  44. data/examples/example4.rb +13 -0
  45. data/examples/example5.rb +13 -0
  46. data/examples/example6.rb +13 -0
  47. data/examples/example7.rb +13 -0
  48. data/examples/example8.rb +13 -0
  49. data/examples/example9.rb +13 -0
  50. data/lib/read.rb +9 -0
  51. data/lib/readlines/read.rb +263 -0
  52. data/lib/readlines/readlines/check.rb +16 -0
  53. data/lib/readlines/readlines/content.rb +45 -0
  54. data/lib/readlines/readlines/convert.rb +43 -0
  55. data/lib/readlines/readlines/count.rb +58 -0
  56. data/lib/readlines/readlines/delete.rb +89 -0
  57. data/lib/readlines/readlines/error.rb +13 -0
  58. data/lib/readlines/readlines/file.rb +20 -0
  59. data/lib/readlines/readlines/info.rb +48 -0
  60. data/lib/readlines/readlines/merge.rb +38 -0
  61. data/lib/readlines/readlines/pattern.rb +22 -0
  62. data/lib/readlines/readlines/replace.rb +50 -0
  63. data/lib/readlines/readlines/search.rb +62 -0
  64. data/lib/readlines/readlines/sort.rb +18 -0
  65. data/lib/readlines/readlines/split.rb +79 -0
  66. data/lib/readlines/readlines/version.rb +7 -0
  67. data/readlines.gemspec +25 -0
  68. metadata +182 -0
@@ -0,0 +1,50 @@
1
+ # readlines/lib/readlines/readlines/replace.rb
2
+ # Replace a specific pattern with a replacement string in the file
3
+ require 'fileutils'
4
+ require 'json'
5
+
6
+ module Readlines
7
+ module Replace
8
+ def replace_now(pattern, replacement)
9
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
10
+
11
+ content = ::File.read(@file_path)
12
+ updated_content = content.gsub(pattern, replacement)
13
+ ::File.write(@file_path, updated_content)
14
+ updated_content
15
+ end
16
+
17
+ def replace_special_characters_now(replacement)
18
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
19
+
20
+ content = ::File.read(@file_path, encoding: 'UTF-8')
21
+ updated_content = content.gsub(/[^[:alnum:]\s]/, replacement)
22
+ ::File.write(@file_path, updated_content)
23
+ updated_content
24
+ end
25
+
26
+ def replace_multiple_patterns_now(pattern_replacement_hash)
27
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
28
+
29
+ content = ::File.read(@file_path)
30
+ pattern_replacement_hash.each do |pattern, replacement|
31
+ content.gsub!(pattern, replacement)
32
+ end
33
+ ::File.write(@file_path, content)
34
+ content
35
+ end
36
+
37
+ def replace_csv_value_now(column_index, old_value, new_value)
38
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
39
+
40
+ content = ::File.readlines(@file_path)
41
+ updated_content = content.map do |line|
42
+ values = line.chomp.split(',')
43
+ values[column_index] = values[column_index] == old_value ? new_value : values[column_index]
44
+ values.join(',')
45
+ end.join("\n")
46
+ ::File.write(@file_path, updated_content)
47
+ updated_content
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,62 @@
1
+ # readlines/lib/readlines/readlines/search.rb
2
+ # Search for a specific value in the file and optionally show the line numbers
3
+ require 'fileutils'
4
+ require 'json'
5
+ module Readlines
6
+ module Search
7
+ def search_about_now(value, show_lines: false)
8
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
9
+
10
+ content = ::File.read(@file_path)
11
+
12
+ if content.include?(value)
13
+ if show_lines
14
+ lines = content.lines
15
+ matched_lines = lines.each_with_index.select { |line, index| line.include?(value) }
16
+ matched_lines.map { |(line, line_number)| line_number + 1 }
17
+ else
18
+ true
19
+ end
20
+ else
21
+ false
22
+ end
23
+ end
24
+
25
+ def search_multiple_patterns_now(patterns)
26
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
27
+
28
+ content = ::File.read(@file_path)
29
+ result = {}
30
+ patterns.each do |pattern|
31
+ count = content.scan(pattern).length
32
+ result[pattern] = count
33
+ end
34
+ result
35
+ end
36
+
37
+ def search_in_range_now(start_line, end_line, pattern)
38
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
39
+
40
+ content = ::File.readlines(@file_path)
41
+ range = (start_line - 1)..(end_line - 1)
42
+ matched_lines = content[range].select { |line| line.match?(pattern) }
43
+ matched_lines
44
+ end
45
+
46
+ def search_logical_patterns_now(patterns, operator)
47
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
48
+ raise ArgumentError, "Invalid logical operator: #{operator}" unless %w[AND OR].include?(operator)
49
+
50
+ content = ::File.read(@file_path)
51
+ matched_lines = content.lines.select do |line|
52
+ case operator
53
+ when 'AND'
54
+ patterns.all? { |pattern| line.match?(pattern) }
55
+ when 'OR'
56
+ patterns.any? { |pattern| line.match?(pattern) }
57
+ end
58
+ end
59
+ matched_lines
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,18 @@
1
+ # readlines/lib/readlines/readlines/sort.rb
2
+ # Sort the lines of the file alphabetically
3
+ require 'fileutils'
4
+ require 'json'
5
+
6
+ module Readlines
7
+ module Sort
8
+ def sort_alphabetically_now
9
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
10
+
11
+ content = ::File.read(@file_path)
12
+ sorted_lines = content.lines.sort_by { |line| line.strip }
13
+ updated_content = sorted_lines.join
14
+ ::File.write(@file_path, updated_content)
15
+ updated_content
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,79 @@
1
+ # readlines/lib/readlines/readlines/split.rb
2
+ require 'fileutils'
3
+ require 'json'
4
+
5
+ module Readlines
6
+ module Split
7
+ def split_file_now(num_parts)
8
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
9
+ raise ArgumentError, "Number of parts must be greater than 0" if num_parts <= 0
10
+
11
+ content = ::File.read(@file_path)
12
+ lines = content.lines
13
+ part_size = (lines.size.to_f / num_parts).ceil
14
+
15
+ file_name = ::File.basename(@file_path, ::File.extname(@file_path))
16
+ file_ext = ::File.extname(@file_path)
17
+
18
+ num_parts.times do |i|
19
+ start_index = i * part_size
20
+ end_index = [(i + 1) * part_size - 1, lines.size - 1].min
21
+ part_content = lines[start_index..end_index].join
22
+
23
+ part_file_name = "#{file_name}_part#{i + 1}#{file_ext}"
24
+ ::File.write(part_file_name, part_content)
25
+ end
26
+ end
27
+
28
+ def split_file_by_size_now(part_size)
29
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
30
+ raise ArgumentError, "Part size must be greater than 0" if part_size <= 0
31
+
32
+ content = ::File.read(@file_path)
33
+ file_size = content.bytesize
34
+ num_parts = (file_size.to_f / part_size).ceil
35
+
36
+ file_name = ::File.basename(@file_path, ::File.extname(@file_path))
37
+ file_ext = ::File.extname(@file_path)
38
+
39
+ start_index = 0
40
+ num_parts.times do |i|
41
+ end_index = [start_index + part_size - 1, file_size - 1].min
42
+ part_content = content.byteslice(start_index..end_index)
43
+
44
+ part_file_name = "#{file_name}_part#{i + 1}#{file_ext}"
45
+ ::File.write(part_file_name, part_content)
46
+
47
+ start_index = end_index + 1
48
+ end
49
+ end
50
+
51
+ def split_by_delimiter_now(delimiter)
52
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
53
+
54
+ content = ::File.read(@file_path)
55
+ parts = content.split(delimiter)
56
+ file_name = ::File.basename(@file_path, ::File.extname(@file_path))
57
+ file_ext = ::File.extname(@file_path)
58
+
59
+ parts.each_with_index do |part, index|
60
+ part_file_name = "#{file_name}_part#{index + 1}#{file_ext}"
61
+ ::File.write(part_file_name, part)
62
+ end
63
+ end
64
+
65
+ def split_by_pattern_now(pattern)
66
+ raise Readlines::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
67
+
68
+ content = ::File.read(@file_path)
69
+ categories = content.split(pattern)
70
+ file_name = ::File.basename(@file_path, ::File.extname(@file_path))
71
+ file_ext = ::File.extname(@file_path)
72
+
73
+ categories.each_with_index do |category, index|
74
+ category_file_name = "#{file_name}_category#{index + 1}#{file_ext}"
75
+ ::File.write(category_file_name, category)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,7 @@
1
+ # lib/readlines/readlines/version.rb
2
+
3
+ module Readlines
4
+ module Version
5
+ VERSION = "1.0"
6
+ end
7
+ end
data/readlines.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "readlines"
3
+ spec.version = "1.0"
4
+ spec.authors = ['Maven']
5
+ spec.email = ['qppn@hotmail.com']
6
+
7
+ spec.summary = "A Ruby library for advanced text file manipulation and processing."
8
+ spec.description = "Readlines is a Ruby library offering advanced tools for reading, writing, and manipulating text files. It supports searching, replacing, sorting, splitting, merging, handling CSVs, encryption, and more. Perfect for developers needing efficient and complex file operations."
9
+ spec.homepage = "https://github.com/Abo5/readlines"
10
+ spec.license = "MIT"
11
+
12
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
13
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
14
+ end
15
+ spec.bindir = "exe"
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_runtime_dependency 'json', '~> 2.0'
20
+ spec.add_runtime_dependency 'fileutils', '~> 1.7.2'
21
+
22
+ spec.add_development_dependency "bundler", "~> 2.0"
23
+ spec.add_development_dependency "rake", "~> 13.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: readlines
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Maven
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fileutils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.7.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Readlines is a Ruby library offering advanced tools for reading, writing,
84
+ and manipulating text files. It supports searching, replacing, sorting, splitting,
85
+ merging, handling CSVs, encryption, and more. Perfect for developers needing efficient
86
+ and complex file operations.
87
+ email:
88
+ - qppn@hotmail.com
89
+ executables: []
90
+ extensions: []
91
+ extra_rdoc_files: []
92
+ files:
93
+ - ".github/workflows/gem-push.yml"
94
+ - ".gitignore"
95
+ - ".rspec"
96
+ - CHANGELOG.md
97
+ - CODE_OF_CONDUCT.md
98
+ - Gemfile
99
+ - Gemfile.lock
100
+ - LICENSE
101
+ - README.md
102
+ - Rakefile
103
+ - examples/example1.rb
104
+ - examples/example10.rb
105
+ - examples/example11.rb
106
+ - examples/example12.rb
107
+ - examples/example13.rb
108
+ - examples/example14.rb
109
+ - examples/example15.rb
110
+ - examples/example16.rb
111
+ - examples/example17.rb
112
+ - examples/example18.rb
113
+ - examples/example19.rb
114
+ - examples/example2.rb
115
+ - examples/example20.rb
116
+ - examples/example21.rb
117
+ - examples/example22.rb
118
+ - examples/example23.rb
119
+ - examples/example24.rb
120
+ - examples/example25.rb
121
+ - examples/example26.rb
122
+ - examples/example27.rb
123
+ - examples/example28.rb
124
+ - examples/example29.rb
125
+ - examples/example3.rb
126
+ - examples/example30.rb
127
+ - examples/example31.rb
128
+ - examples/example32.rb
129
+ - examples/example33.rb
130
+ - examples/example34.rb
131
+ - examples/example35.rb
132
+ - examples/example36.rb
133
+ - examples/example37.rb
134
+ - examples/example38.rb
135
+ - examples/example4.rb
136
+ - examples/example5.rb
137
+ - examples/example6.rb
138
+ - examples/example7.rb
139
+ - examples/example8.rb
140
+ - examples/example9.rb
141
+ - lib/read.rb
142
+ - lib/readlines/read.rb
143
+ - lib/readlines/readlines/check.rb
144
+ - lib/readlines/readlines/content.rb
145
+ - lib/readlines/readlines/convert.rb
146
+ - lib/readlines/readlines/count.rb
147
+ - lib/readlines/readlines/delete.rb
148
+ - lib/readlines/readlines/error.rb
149
+ - lib/readlines/readlines/file.rb
150
+ - lib/readlines/readlines/info.rb
151
+ - lib/readlines/readlines/merge.rb
152
+ - lib/readlines/readlines/pattern.rb
153
+ - lib/readlines/readlines/replace.rb
154
+ - lib/readlines/readlines/search.rb
155
+ - lib/readlines/readlines/sort.rb
156
+ - lib/readlines/readlines/split.rb
157
+ - lib/readlines/readlines/version.rb
158
+ - readlines.gemspec
159
+ homepage: https://github.com/Abo5/readlines
160
+ licenses:
161
+ - MIT
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubygems_version: 3.3.3
179
+ signing_key:
180
+ specification_version: 4
181
+ summary: A Ruby library for advanced text file manipulation and processing.
182
+ test_files: []