Class: Loader
- Inherits:
-
Object
- Object
- Loader
- Defined in:
- lib/dataload/loader.rb
Instance Attribute Summary collapse
-
#db_ops ⇒ Object
Returns the value of attribute db_ops.
-
#source_filename ⇒ Object
Returns the value of attribute source_filename.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
- #load! ⇒ Object
- #migrate! ⇒ Object
- #new_struct ⇒ Object
- #target_column_names ⇒ Object
- #target_hash_for_row(row) ⇒ Object
- #target_hashes ⇒ Object
Instance Attribute Details
#db_ops ⇒ Object
Returns the value of attribute db_ops.
18 19 20 |
# File 'lib/dataload/loader.rb', line 18 def db_ops @db_ops end |
#source_filename ⇒ Object
Returns the value of attribute source_filename.
18 19 20 |
# File 'lib/dataload/loader.rb', line 18 def source_filename @source_filename end |
#table_name ⇒ Object
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_struct ⇒ Object
39 40 41 |
# File 'lib/dataload/loader.rb', line 39 def new_struct Struct.new(*target_column_names) end |
#target_column_names ⇒ Object
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_hashes ⇒ Object
33 34 35 |
# File 'lib/dataload/loader.rb', line 33 def target_hashes source_rows.map { |x| target_hash_for_row(x) } end |