Sha256: d6ae0090e6444b5498da7508b21e5f2ac38a44c23d04c2b3c345b7becd298651

Contents?: true

Size: 1.36 KB

Versions: 9

Compression:

Stored size: 1.36 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 :truncate_values_at, default: 40
      setting :table_renderer, default: :basic
      setting :fields_exclude, default: ['lids', 'type']
      setting :header_row, default: true

      def load(record)
        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 if @config.header_row), rows: rows)
        puts @table.render(
          @config.table_renderer.to_sym,
          padding: [0, 2, 0, 0]
        )
      end

      def records
        @records ||= []
      end

      private

      def build_rows(records, headers)
        records.map do |record|
          values = record.transform_keys(&:to_sym).values_at(*headers).map{|value| value.to_s }
          values = values.map { |value| force_utf8(value) }
          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

9 entries across 9 versions & 1 rubygems

Version Path
chronicle-etl-0.5.5 lib/chronicle/etl/loaders/table_loader.rb
chronicle-etl-0.5.4 lib/chronicle/etl/loaders/table_loader.rb
chronicle-etl-0.5.3 lib/chronicle/etl/loaders/table_loader.rb
chronicle-etl-0.5.2 lib/chronicle/etl/loaders/table_loader.rb
chronicle-etl-0.5.1 lib/chronicle/etl/loaders/table_loader.rb
chronicle-etl-0.5.0 lib/chronicle/etl/loaders/table_loader.rb
chronicle-etl-0.4.4 lib/chronicle/etl/loaders/table_loader.rb
chronicle-etl-0.4.3 lib/chronicle/etl/loaders/table_loader.rb
chronicle-etl-0.4.2 lib/chronicle/etl/loaders/table_loader.rb