Module: Readlines::Merge

Included in:
ReadDuc
Defined in:
lib/readlines/readlines/merge.rb

Instance Method Summary collapse

Instance Method Details

#merge_files_now(file_paths) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/readlines/readlines/merge.rb', line 8

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

  merged_content = file_paths.map do |file_path|
    raise Readlines::NotFoundError, "File not found: #{file_path}" unless ::File.exist?(file_path)
    ::File.read(file_path)
  end.join

  file_name = ::File.basename(@file_path, ::File.extname(@file_path))
  merged_file_name = "#{file_name}_merged#{::File.extname(@file_path)}"
  ::File.write(merged_file_name, merged_content)

  merged_file_name
end

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

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/readlines/readlines/merge.rb', line 23

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

  merged_content = file_paths.map do |file_path|
    raise Readlines::NotFoundError, "File not found: #{file_path}" unless ::File.exist?(file_path)
    ::File.read(file_path)
  end.join(separator)

  file_name = ::File.basename(@file_path, ::File.extname(@file_path))
  merged_file_name = "#{file_name}_merged#{::File.extname(@file_path)}"
  ::File.write(merged_file_name, merged_content)

  merged_file_name
end