Class: Loader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#db_opsObject

Returns the value of attribute db_ops.



18
19
20
# File 'lib/dataload/loader.rb', line 18

def db_ops
  @db_ops
end

#source_filenameObject

Returns the value of attribute source_filename.



18
19
20
# File 'lib/dataload/loader.rb', line 18

def source_filename
  @source_filename
end

#table_nameObject

Returns the value of attribute table_name.



18
19
20
# File 'lib/dataload/loader.rb', line 18

def table_name
  @table_name
end

Instance Method Details

#load!Object



77
78
79
80
81
# File 'lib/dataload/loader.rb', line 77

def load!
  ActiveRecord::Base.establish_connection(db_ops)
  migrate!
  ar_objects.each { |x| x.save! }
end

#migrate!Object



67
68
69
70
71
72
73
# File 'lib/dataload/loader.rb', line 67

def migrate!
  ar.find(:first)
rescue => exp
  puts "find failed"
  puts exp.inspect
  migration.migrate(:up)
end

#new_structObject



39
40
41
# File 'lib/dataload/loader.rb', line 39

def new_struct
  Struct.new(*target_column_names)
end

#target_column_namesObject



36
37
38
# File 'lib/dataload/loader.rb', line 36

def target_column_names
  columns.map { |x| x.target_name }
end

#target_hash_for_row(row) ⇒ Object



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

def target_hash_for_row(row)
  h = {}
  columns.each do |col|
    h[col.target_name] = col.target_value(row)
  end
  h
end

#target_hashesObject



33
34
35
# File 'lib/dataload/loader.rb', line 33

def target_hashes
  source_rows.map { |x| target_hash_for_row(x) }
end