Permalink
Browse files

Added manager

  • Loading branch information...
1 parent b447617 commit ca4e560fef3f27864457791f9ea32c8e9a9e3831 @mdespuits committed Mar 31, 2012
Showing with 73 additions and 0 deletions.
  1. +1 −0 lib/custom_rails_importer.rb
  2. +72 −0 lib/custom_rails_importer/manager.rb
@@ -1,5 +1,6 @@
require "custom_rails_importer/version"
require "custom_rails_importer/stats"
+require "custom_rails_importer/manager"
module Importer
end
@@ -0,0 +1,72 @@
+require 'custom_rails_importer/stats'
+
+module Importer
+ class Manager
+
+ def self.connect(credentials = {}, local_conn)
+ @@remote = Mysql2::Client.new credentials
+ @@local ||= local_conn
+ end
+
+ def self.local(sql)
+ @@local.execute sql
+ end
+
+ def self.save(klass, other = {})
+ nice_class_name = klass.class.to_s.tableize.humanize
+ if klass.valid?
+ klass.save!
+ increment
+ klass
+ else
+ increment_failure(klass.class.to_s)
+ puts "Invalid #{nice_class_name} information:"
+ Stats.output("Additional Info about #{nice_class_name}", other)
+ Stats.output("#{nice_class_name} errors", klass.errors.full_messages)
+ Stats.output("#{nice_class_name} attributes:", klass.attributes)
+ return false
+ end
+ end
+
+ def self.remote(sql)
+ @@remote.query(sql)
+ end
+
+ def self.local_tables
+ tables = @@local.tables
+ tables.delete 'schema_migrations'
+ tables
+ end
+
+ def self.local_truncate(*tables)
+ tables.each { |table| local "TRUNCATE TABLE #{table.to_s}" }
+ end
+
+ def self.local_truncate_all
+ local_truncate *local_tables
+ end
+
+ def self.increment
+ @@total_records ||= 0
+ @@total_records += 1
+ end
+
+ def self.increment_failure(klass_name)
+ @@failures ||= Hash.new
+ @@failures[klass_name] ||= 0
+ @@failures[klass_name] += 1
+ end
+
+ def self.failures
+ @@failures
+ rescue
+ @@failures ||= Hash.new
+ end
+
+ def self.total_records
+ @@total_records
+ rescue
+ @@total_records ||= Hash.new
+ end
+ end
+end

0 comments on commit ca4e560

Please sign in to comment.