class DataSource

Attributes

block[R]
csv_opts[R]
input_path[R]
table_name[R]

Public Class Methods

new(table_name, input_path, csv_opts = {}) click to toggle source
# File lib/data_source.rb, line 8
def initialize table_name, input_path, csv_opts = {}, &block
  @table_name, @input_path = table_name, input_path
  @csv_opts = csv_opts ? csv_opts.dup : {}
  @block = block
end

Public Instance Methods

drop_table() click to toggle source
# File lib/data_source.rb, line 29
def drop_table
  conn.drop_table table_name
end
import() click to toggle source
# File lib/data_source.rb, line 14
def import
  batch = []
  TableUtils::Progress.over csv do |row, bar|
    batch << common_columns.map{ |c| row[c] }
    if batch.count >= 1000 || bar.finished?
      model.import common_columns, batch, validate: false
      batch = []
    end
  end
end
table_exists?() click to toggle source
# File lib/data_source.rb, line 25
def table_exists?
  conn.table_exists? table_name
end

Private Instance Methods

common_columns() click to toggle source
# File lib/data_source.rb, line 59
def common_columns
  csv
  @common_columns
end
conn() click to toggle source
# File lib/data_source.rb, line 37
def conn
  @conn ||= ActiveRecord::Base.connection
end
csv() click to toggle source
# File lib/data_source.rb, line 47
def csv
  @csv ||= begin
             csv_opts[:headers] ||= model.column_names - %w(id created_at updated_at)
             csv_opts[:return_headers] = true
             CSV.open(input_path, csv_opts).tap do |csv|
               csv.shift
               @common_columns = model.column_names & csv.headers
               csv.shift if Array === csv_opts[:headers]
             end
           end
end
model() click to toggle source
# File lib/data_source.rb, line 41
def model
  conn.create_table table_name, &block unless table_exists?
  _table_name = table_name
  @model ||= Class.new(ActiveRecord::Base) { self.table_name = _table_name }
end