Class: DataLoader::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/data_loader/loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder = '', separator = ',', table_prefix = 'load', connection = :root) {|_self| ... } ⇒ Loader

Returns a new instance of Loader.

Yields:

  • (_self)

Yield Parameters:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/data_loader/loader.rb', line 31

def initialize(folder = '', separator = ',', table_prefix = 'load', connection = :root)
  @folder, @separator = folder, separator
  @table_prefix, @connection = table_prefix, connection
  @default_ext = 'csv'
  @inspect_rows = 10
  @use_local = false  # with MySQL INFILE
  @log = true
  yield(self) if block_given?
  @logfile = File.expand_path(File.join(@folder, 'data_loader.textile'))
  puts @logfile
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



29
30
31
# File 'lib/data_loader/loader.rb', line 29

def connection
  @connection
end

#default_extObject

Returns the value of attribute default_ext.



29
30
31
# File 'lib/data_loader/loader.rb', line 29

def default_ext
  @default_ext
end

#folderObject

Returns the value of attribute folder.



29
30
31
# File 'lib/data_loader/loader.rb', line 29

def folder
  @folder
end

#inspect_rowsObject

Returns the value of attribute inspect_rows.



29
30
31
# File 'lib/data_loader/loader.rb', line 29

def inspect_rows
  @inspect_rows
end

#log(text) ⇒ Object

Returns the value of attribute log.



29
30
31
# File 'lib/data_loader/loader.rb', line 29

def log
  @log
end

#separatorObject

Returns the value of attribute separator.



29
30
31
# File 'lib/data_loader/loader.rb', line 29

def separator
  @separator
end

#table_prefixObject

Returns the value of attribute table_prefix.



29
30
31
# File 'lib/data_loader/loader.rb', line 29

def table_prefix
  @table_prefix
end

#use_localObject

Returns the value of attribute use_local.



29
30
31
# File 'lib/data_loader/loader.rb', line 29

def use_local
  @use_local
end

Instance Method Details

#clear_logObject



67
68
69
# File 'lib/data_loader/loader.rb', line 67

def clear_log
  FileUtils.remove(@logfile) if File.exist?(@logfile)
end

#load(filename, table = nil, hints = {}) ⇒ Object

load

  • filename - name of file to load (in folder and default_ext)

  • table - table to load file into (with table_prefix), derives from filename by default

  • hints - hash of column name => data type (one of :text, :string, :datetime, :integer)



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/data_loader/loader.rb', line 47

def load(filename, table = nil, hints = {})
  filename = [filename, default_ext].join('.') if File.extname(filename).empty?
  full_file = File.expand_path(File.join(@folder, filename))
  table = Migrator.derive_table_name(filename) if table.nil?
  table = [@table_prefix, table].join('_') unless @table_prefix.blank?
  columns = Inspector.inspect_file(full_file, @separator, @inspect_rows, hints)
  row_sep = Inspector.row_sep
  log_columns(table, columns)
  Migrator.migrate(full_file, columns, table, @separator, @connection, @use_local, row_sep)
  table
end