Class: Readlines::ReadDuc

Inherits:
Object
  • Object
show all
Includes:
Content, Convert, Count, Delete, File, Filter, Info, Merge, Pattern, Replace, Search, Sort, Split
Defined in:
lib/readlines/read.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Content

#decrypt_content_now, #encrypt_content_now, #reverse_content_now, #validate_content_now

Methods included from Split

#split_by_delimiter_now, #split_by_pattern_now, #split_file_by_size_now, #split_file_now

Methods included from Sort

#sort_alphabetically_now

Methods included from Pattern

#extract_patterns_now, #pattern_exists_now?

Methods included from Info

#file_size_now, #info_now

Methods included from Merge

#merge_files_now, #merge_files_with_specific_line_now

Methods included from Filter

#apply_filter

Methods included from File

#file_statistics_now

Methods included from Replace

#replace_csv_value_now, #replace_multiple_patterns_now, #replace_now, #replace_special_characters_now

Methods included from Search

#search_about_now, #search_in_range_now, #search_logical_patterns_now, #search_multiple_patterns_now

Methods included from Convert

#convert_encoding_now, #convert_to_array_now, #convert_to_format_now

Methods included from Delete

#delete_csv_columns_now, #delete_duplicate_lines_now, #delete_empty_lines_now, #delete_lines_now, #delete_right_left_separator_now, #delete_unwanted_characters_now

Methods included from Count

#character_count_now, #line_count_now, #read_lines_now, #word_count_now

Constructor Details

#initialize(file_path, count: nil, split: nil) ⇒ ReadDuc

Returns a new instance of ReadDuc.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/readlines/read.rb', line 37

def initialize(file_path, count: nil, split: nil)
  file_path = ::File.join(Dir.pwd, file_path) unless ::File.absolute_path?(file_path)
  
  raise Readlines::Error::MissingFilePathError, 'File not found at provided path' unless ::File.exist?(file_path)
  raise Readlines::Error::MissingFilePathError, 'You should provide a file path, e.g., "/path/to/file.txt"' if file_path.nil? || file_path.empty?


  @file_path = file_path
  @count_keyword = count
  @split_delimiter = split
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



35
36
37
# File 'lib/readlines/read.rb', line 35

def file_path
  @file_path
end

Instance Method Details

#character_count(line_specific: false) ⇒ Object



71
72
73
74
# File 'lib/readlines/read.rb', line 71

def character_count(line_specific: false)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  character_count_now(line_specific: line_specific)
end

#check_spelling(dictionary) ⇒ Object

Spelling operations



270
271
272
273
# File 'lib/readlines/read.rb', line 270

def check_spelling(dictionary)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  check_spelling_now(dictionary)
end

#convert_encoding(from_encoding, to_encoding) ⇒ Object

Convert operations



207
208
209
210
# File 'lib/readlines/read.rb', line 207

def convert_encoding(from_encoding, to_encoding)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  convert_encoding_now(from_encoding, to_encoding)
end

#convert_to_arrayObject



217
218
219
220
# File 'lib/readlines/read.rb', line 217

def convert_to_array
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  convert_to_array_now()
end

#convert_to_format(format) ⇒ Object



212
213
214
215
# File 'lib/readlines/read.rb', line 212

def convert_to_format(format)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  convert_to_format_now(format)
end

#decrypt_content(key, encrypted_file_path) ⇒ Object



281
282
283
284
# File 'lib/readlines/read.rb', line 281

def decrypt_content(key, encrypted_file_path)
  raise Readlines::Error::NotFoundError, "File not found: #{encrypted_file_path}" unless ::File.exist?(encrypted_file_path)
  decrypt_content_now(key, encrypted_file_path)
end

#delete_csv_columns(column_indices) ⇒ Object



156
157
158
159
# File 'lib/readlines/read.rb', line 156

def delete_csv_columns(column_indices)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  delete_csv_columns_now(column_indices)
end

#delete_duplicate_linesObject



151
152
153
154
# File 'lib/readlines/read.rb', line 151

def delete_duplicate_lines
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  delete_duplicate_lines_now()
end

#delete_empty_linesObject

Delete operations



136
137
138
139
# File 'lib/readlines/read.rb', line 136

def delete_empty_lines
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  delete_empty_lines_now()
end

#delete_lines(line_numbers, delete_space: false) ⇒ Object



146
147
148
149
# File 'lib/readlines/read.rb', line 146

def delete_lines(line_numbers, delete_space: false)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  delete_lines_now(line_numbers, delete_space: delete_space)
end

#delete_right_left_separator(separator, delete_right: false, delete_left: false, keep_separator: false) ⇒ Object



161
162
163
164
# File 'lib/readlines/read.rb', line 161

def delete_right_left_separator(separator, delete_right: false, delete_left: false, keep_separator: false)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  delete_right_left_separator_now(separator, delete_right: delete_right, delete_left: delete_left, keep_separator: keep_separator)
end

#delete_unwanted_characters(characters) ⇒ Object



141
142
143
144
# File 'lib/readlines/read.rb', line 141

def delete_unwanted_characters(characters)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  delete_unwanted_characters_now(characters)
end

#encrypt_content(key) ⇒ Object

Encryption operations



276
277
278
279
# File 'lib/readlines/read.rb', line 276

def encrypt_content(key)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  encrypt_content_now(key)
end

#extract_patterns(patterns) ⇒ Object



253
254
255
256
# File 'lib/readlines/read.rb', line 253

def extract_patterns(patterns)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  extract_patterns_now(patterns)
end

#file_size(unit = nil) ⇒ Object

File operations



223
224
225
226
# File 'lib/readlines/read.rb', line 223

def file_size(unit = nil)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  file_size_now(unit)
end

#file_statisticsObject



228
229
230
231
# File 'lib/readlines/read.rb', line 228

def file_statistics
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  file_statistics_now()
end

#filter(value, query, operation, replacement = nil) ⇒ Object

Replace and Delete lines from the file



234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/readlines/read.rb', line 234

def filter(value, query, operation, replacement = nil)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)

  processed_lines = []
  ::File.foreach(@file_path) do |line|
    result = apply_filter(line, value, query, operation, replacement)
    processed_lines << result if result 
  end

  ::File.write(@file_path, processed_lines.join)
  processed_lines
end

#infoObject



87
88
89
90
# File 'lib/readlines/read.rb', line 87

def info
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  info_now()
end

#line_countObject

Count operations



66
67
68
69
# File 'lib/readlines/read.rb', line 66

def line_count
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  line_count_now()
end

#merge_files(file_paths) ⇒ Object

Merge operations

Raises:

  • (ArgumentError)


196
197
198
199
# File 'lib/readlines/read.rb', line 196

def merge_files(file_paths)
  raise ArgumentError, "File paths array is empty" if file_paths.empty?
  merge_files_now(file_paths)
end

#merge_files_with_specific_line(file_paths, separator: "\n") ⇒ Object

Raises:

  • (ArgumentError)


201
202
203
204
# File 'lib/readlines/read.rb', line 201

def merge_files_with_specific_line(file_paths, separator: "\n")
  raise ArgumentError, "File paths array is empty" if file_paths.empty?
  merge_files_with_specific_line_now(file_paths, separator: separator)
end

#pattern_exists?(pattern) ⇒ Boolean

Pattern operations

Returns:

  • (Boolean)

Raises:



248
249
250
251
# File 'lib/readlines/read.rb', line 248

def pattern_exists?(pattern)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  pattern_exists_now?(pattern)
end

#read_linesObject

Read operations



82
83
84
85
# File 'lib/readlines/read.rb', line 82

def read_lines
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  read_lines_now()
end

#replace(pattern, replacement) ⇒ Object

Replace operations



115
116
117
118
# File 'lib/readlines/read.rb', line 115

def replace(pattern, replacement)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  replace_now(pattern, replacement)
end

#replace_csv_value(column_index, old_value, new_value) ⇒ Object



130
131
132
133
# File 'lib/readlines/read.rb', line 130

def replace_csv_value(column_index, old_value, new_value)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  replace_csv_value_now(column_index, old_value, new_value)
end

#replace_multiple_patterns(pattern_replacement_hash) ⇒ Object



125
126
127
128
# File 'lib/readlines/read.rb', line 125

def replace_multiple_patterns(pattern_replacement_hash)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  replace_multiple_patterns_now(pattern_replacement_hash)
end

#replace_special_characters(replacement) ⇒ Object



120
121
122
123
# File 'lib/readlines/read.rb', line 120

def replace_special_characters(replacement)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  replace_special_characters_now(replacement)
end

#reverse_contentObject

Content operations



259
260
261
262
# File 'lib/readlines/read.rb', line 259

def reverse_content
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  reverse_content_now()
end

#search_about(value, show_lines: false) ⇒ Object

Search operations



93
94
95
96
# File 'lib/readlines/read.rb', line 93

def search_about(value, show_lines: false)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  search_about_now(value, show_lines: show_lines)
end

#search_in_range(start_line, end_line, pattern) ⇒ Object



103
104
105
106
# File 'lib/readlines/read.rb', line 103

def search_in_range(start_line, end_line, pattern)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  search_in_range_now(start_line, end_line, pattern)
end

#search_logical_patterns(patterns, operator) ⇒ Object



108
109
110
111
112
# File 'lib/readlines/read.rb', line 108

def search_logical_patterns(patterns, operator)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  raise ArgumentError, "Invalid logical operator: #{operator}" unless %w[AND OR].include?(operator)
  search_logical_patterns_now(patterns, operator)
end

#search_multiple_patterns(patterns) ⇒ Object



98
99
100
101
# File 'lib/readlines/read.rb', line 98

def search_multiple_patterns(patterns)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  search_multiple_patterns_now(patterns)
end

#sort_alphabeticallyObject

Sort operations



167
168
169
170
# File 'lib/readlines/read.rb', line 167

def sort_alphabetically
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  sort_alphabetically_now()
end

#split_by_delimiter(delimiter) ⇒ Object



185
186
187
188
# File 'lib/readlines/read.rb', line 185

def split_by_delimiter(delimiter)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  split_by_delimiter_now(delimiter)
end

#split_by_pattern(pattern) ⇒ Object



190
191
192
193
# File 'lib/readlines/read.rb', line 190

def split_by_pattern(pattern)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  split_by_pattern_now(pattern)
end

#split_file(num_parts) ⇒ Object

Split operations



173
174
175
176
177
# File 'lib/readlines/read.rb', line 173

def split_file(num_parts)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  raise ArgumentError, "Number of parts must be greater than 0" if num_parts <= 0
  split_file_now(num_parts)
end

#split_file_by_size(part_size) ⇒ Object



179
180
181
182
183
# File 'lib/readlines/read.rb', line 179

def split_file_by_size(part_size)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  raise ArgumentError, "Part size must be greater than 0" if part_size <= 0
  split_file_by_size_now(part_size)
end

#validate_content(rules) ⇒ Object



264
265
266
267
# File 'lib/readlines/read.rb', line 264

def validate_content(rules)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  validate_content_now(rules)
end

#word_count(line_specific: false) ⇒ Object



76
77
78
79
# File 'lib/readlines/read.rb', line 76

def word_count(line_specific: false)
  raise Readlines::Error::NotFoundError, "File not found: #{@file_path}" unless ::File.exist?(@file_path)
  word_count_now(line_specific: line_specific)
end