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:



26
27
28
29
30
31
32
# File 'lib/data_loader/loader.rb', line 26

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
  yield(self) if block_given?
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



24
25
26
# File 'lib/data_loader/loader.rb', line 24

def connection
  @connection
end

#default_extObject

Returns the value of attribute default_ext.



24
25
26
# File 'lib/data_loader/loader.rb', line 24

def default_ext
  @default_ext
end

#folderObject

Returns the value of attribute folder.



24
25
26
# File 'lib/data_loader/loader.rb', line 24

def folder
  @folder
end

#inspect_rowsObject

Returns the value of attribute inspect_rows.



24
25
26
# File 'lib/data_loader/loader.rb', line 24

def inspect_rows
  @inspect_rows
end

#separatorObject

Returns the value of attribute separator.



24
25
26
# File 'lib/data_loader/loader.rb', line 24

def separator
  @separator
end

#table_prefixObject

Returns the value of attribute table_prefix.



24
25
26
# File 'lib/data_loader/loader.rb', line 24

def table_prefix
  @table_prefix
end

Instance Method Details

#load(filename, table = nil) ⇒ Object



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

def load(filename, table = nil)
  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)
  Migrator.migrate(full_file, columns, table, @separator, @connection)
  table
end