class PostgresUpsert::ReadAdapters::FileAdapter

Public Class Methods

new(source, options) click to toggle source
# File lib/postgres_upsert/read_adapters/file_adapter.rb, line 4
def initialize(source, options)
  @options = sanitize_options(options)
  @source = File.open(source, 'r')
end

Public Instance Methods

columns() click to toggle source
# File lib/postgres_upsert/read_adapters/file_adapter.rb, line 26
def columns
  @columns ||= begin
    columns_list = @options[:columns] ? @options[:columns].map(&:to_s) : []
    if @options[:header]
      # if header is present, we need to strip it from io, whether we use it for the columns list or not.
      line = gets
      if columns_list.empty?
        columns_list = line.strip.split(@options[:delimiter])
      end
    end
    columns_list = columns_list.map { |c| @options[:map][c.to_s] } if @options[:map]
    columns_list
  end
end
continuous_write_enabled() click to toggle source
# File lib/postgres_upsert/read_adapters/file_adapter.rb, line 18
def continuous_write_enabled
  true
end
gets() click to toggle source
# File lib/postgres_upsert/read_adapters/file_adapter.rb, line 22
def gets
  @source.gets
end
sanitize_options(options) click to toggle source
# File lib/postgres_upsert/read_adapters/file_adapter.rb, line 9
def sanitize_options(options)
  options.slice(
    :delimiter, :header, :columns, :map, :unique_key
  ).reverse_merge(
    header: true,
    delimiter: ',',
  )
end