Sha256: 6b9e29676331374a12e1e517ac6478ac30463c3b67c2d70460a120f47fe06256

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

require 'tty/table'
require 'active_support/core_ext/string/filters'
require 'active_support/core_ext/hash/reverse_merge'

module Chronicle
  module ETL
    class TableLoader < Chronicle::ETL::Loader
      register_connector do |r|
        r.description = 'an ASCII table'
      end

      setting :fields_limit, default: nil
      setting :fields_exclude, default: ['lids', 'type']
      setting :fields_include, default: []
      setting :truncate_values_at, default: 40
      setting :table_renderer, default: :basic

      def load(record)
        @records ||= []
        @records << record.to_h_flattened
      end

      def finish
        return if @records.empty?

        headers = build_headers(@records)
        rows = build_rows(@records, headers)

        @table = TTY::Table.new(header: headers, rows: rows)
        puts @table.render(
          @config.table_renderer.to_sym,
          padding: [0, 2, 0, 0]
        )
      end

      private

      def build_headers(records)
        headers =
          if @config.fields_include.any?
            Set[*@config.fields_include]
          else
            # use all the keys of the flattened record hash
            Set[*records.map(&:keys).flatten.map(&:to_s).uniq]
          end

        headers = headers.delete_if { |header| header.end_with?(*@config.fields_exclude) } if @config.fields_exclude.any?
        headers = headers.first(@config.fields_limit) if @config.fields_limit

        headers.to_a.map(&:to_sym)
      end

      def build_rows(records, headers)
        records.map do |record|
          values = record.values_at(*headers).map{|value| value.to_s }

          if @config.truncate_values_at
            values = values.map{ |value| value.truncate(@config.truncate_values_at) }
          end

          values
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chronicle-etl-0.4.0 lib/chronicle/etl/loaders/table_loader.rb