Class: FileReplicator::Joiner

Inherits:
Object
  • Object
show all
Includes:
ReplicatorHelper
Defined in:
lib/file_replicator/joiner.rb

Constant Summary

Constants included from ReplicatorHelper

ReplicatorHelper::READ_BUFFER

Instance Attribute Summary

Attributes included from ReplicatorHelper

#checksum, #colour, #options

Instance Method Summary collapse

Constructor Details

#initializeJoiner

Returns a new instance of Joiner.



11
12
13
14
# File 'lib/file_replicator/joiner.rb', line 11

def initialize
  @options = JoinerCmdParse.new.get_options
  @colour  = Pastel.new enabled: !@options[:no_colour]
end

Instance Method Details

#joinObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/file_replicator/joiner.rb', line 16

def join
  files = Dir.glob(
      File.join(
          File.dirname(@options[:first]),
          '*'
      )
  ).sort

  prepare @options[:output_path]
  output_file = File.open @options[:output_path], 'ab'

  first_index = files.index File.expand_path(@options[:first])
  last_index  = files.index File.expand_path(@options[:last])

  if progress?
    file_pb = ProgressBar.create(
        total: (first_index..last_index).size,
        title: colour.bright_blue("#{File.basename output_file}")
    )
  end
  files[first_index..last_index].each do |file|
    File.open file, 'rb' do |f|
      while (data = f.read(READ_BUFFER))
        output_file.write data
      end
    end
    file_pb.increment if progress?
  end

  file_pb.finish if progress?
rescue StandardError => e
  puts @colour.bright_red e.message unless quiet?
  file_pb.stop if progress?

  exit 1
ensure
  output_file.close unless output_file.nil?
end