Class: FlatKit::Merge

Inherits:
Object
  • Object
show all
Defined in:
lib/flat_kit/merge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs:, input_fallback: "auto", output:, output_fallback: "auto", compare_fields:) ⇒ Merge

Returns a new instance of Merge.



7
8
9
10
11
12
13
14
15
# File 'lib/flat_kit/merge.rb', line 7

def initialize(inputs:, input_fallback: "auto",
               output:, output_fallback: "auto",
               compare_fields:)
  @compare_fields = compare_fields
  @readers = ::FlatKit::Reader.create_readers_from_paths(paths: inputs, compare_fields: @compare_fields,
                                                         fallback: input_fallback)
  @writer  = ::FlatKit::Writer.create_writer_from_path(path: output, fallback: output_fallback,
                                                       reader_format: @readers.first.format_name)
end

Instance Attribute Details

#compare_fieldsObject (readonly)

Returns the value of attribute compare_fields.



5
6
7
# File 'lib/flat_kit/merge.rb', line 5

def compare_fields
  @compare_fields
end

#readersObject (readonly)

Returns the value of attribute readers.



3
4
5
# File 'lib/flat_kit/merge.rb', line 3

def readers
  @readers
end

#writerObject (readonly)

Returns the value of attribute writer.



4
5
6
# File 'lib/flat_kit/merge.rb', line 4

def writer
  @writer
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/flat_kit/merge.rb', line 17

def call
  ::FlatKit.logger.info "Merging the following files into #{writer.destination}"
  ::FlatKit.logger.info "Using this key for sorting: #{compare_fields.join(", ")}"
  readers.each do |r|
    ::FlatKit.logger.info "  #{r.source}"
  end

  merge_tree = ::FlatKit::MergeTree.new(readers)
  merge_tree.each do |record|
    writer.write(record)
  end
  readers.each do |r|
    ::FlatKit.logger.info "  #{r.source} produced #{r.count} records"
  end
  writer.close
  ::FlatKit.logger.info "Wrote #{writer.count} records to #{writer.destination}"
end