Permalink
Browse files

Doesn't puts to terminal by default. Saves to a file in the Rails app

  • Loading branch information...
1 parent f95b5f4 commit ecc6203595bbe5a18f07a23828ec9bb55f819cb8 @mdespuits committed Apr 6, 2012
Showing with 53 additions and 39 deletions.
  1. +6 −5 lib/transplant/planter.rb
  2. +47 −34 lib/transplant/stats.rb
View
@@ -1,11 +1,12 @@
module Transplant
class Planter
- attr_accessor :app_name, :connection
+ attr_accessor :app_name, :connection, :statistician
def initialize(app_name, connection)
@app_name = app_name
@connection = connection
+ @statistician = Stats.new(self)
@queries ||= []
@results ||= {}
end
@@ -24,10 +25,10 @@ def save(klass, other = {})
klass
else
fail(klass_name)
- puts "Invalid #{klass_name} information:"
- Stats.output("Additional Info about #{klass_name}", other)
- Stats.output("#{klass_name} errors", klass.errors.full_messages)
- Stats.output("#{klass_name} attributes", klass.attributes)
+ @statistician.output "Invalid #{klass_name} information:"
+ @statistician.output("Additional Info about #{klass_name}", other)
+ @statistician.output("#{klass_name} errors", klass.errors.full_messages)
+ @statistician.output("#{klass_name} attributes", klass.attributes)
return false
end
end
View
@@ -2,6 +2,7 @@ module Transplant
class Stats
def initialize(transplanter)
+ @result_set = []
@planter = transplanter
end
@@ -10,70 +11,82 @@ def output_all_info(opts = {})
total_import_records
successes
failures
+ output_to_file
end
- class << self
+ def output_to_file
+ timestamp = Time.now.utc.to_datetime.to_formatted_s(:number)
+ Dir.chdir Rails.root
+ filepath = ".import.#{timestamp}_#{@planter.app_name.downcase}"
+ f = File.open(filepath, "w")
+ @result_set.each { |output| f.puts output }
+ f.close
+ end
- def output(header, input, depth = 0, sub_output = false)
- if input.is_a? Hash
- hash_output(header, input, depth, sub_output)
- elsif input.is_a? Array
- array_output(header, input, depth, sub_output)
- end
- end
+ def add_to_results(output)
+ @result_set << output
+ end
- def hash_output(header, hash, depth = 0, sub_hash = false)
- puts tabs(depth) + "#{header}:" if hash.any? && header.present?
- hash.each_pair do |key, value|
- if value.is_a?(Hash) || value.is_a?(Array)
- output(key, value, depth + 1, sub_hash = true)
- else
- puts tabs(depth + 1) + "* #{key}: #{value}"
- end
- end
+ def output(header, input = {}, depth = 0, sub_output = false)
+ if input.is_a? Hash
+ hash_output(header, input, depth, sub_output)
+ elsif input.is_a? Array
+ array_output(header, input, depth, sub_output)
+ elsif input.is_a? String
+ add_to_results input
end
+ end
- def array_output(header, array, depth = 0, sub_array = false)
- puts tabs(depth) + "#{header}:" if array.any? && header.present?
- array.each do |item|
- if item.is_a?(Hash) || item.is_a?(Array)
- output("", item, depth + 1, sub_hash = true)
- else
- puts tabs(depth + 1) + "* #{item}"
- end
+ def hash_output(header, hash, depth = 0, sub_hash = false)
+ add_to_results tabs(depth) + "#{header}:" if hash.any? && header.present?
+ hash.each_pair do |key, value|
+ if value.is_a?(Hash) || value.is_a?(Array)
+ output(key, value, depth + 1, sub_hash = true)
+ else
+ add_to_results tabs(depth + 1) + "* #{key}: #{value}"
end
end
+ end
- def tabs(count)
- "\t"*count
+ def array_output(header, array, depth = 0, sub_array = false)
+ add_to_results tabs(depth) + "#{header}:" if array.any? && header.present?
+ array.each do |item|
+ if item.is_a?(Hash) || item.is_a?(Array)
+ output("", item, depth + 1, sub_hash = true)
+ else
+ add_to_results tabs(depth + 1) + "* #{item}"
+ end
end
- private :tabs
+ end
+ def tabs(count)
+ "\t"*count
end
+ private :tabs
def total_import_records
- puts "Estimated number of records imported into #{@planter.app_name}: #{@planter.total_successes}"
+ add_to_results "Estimated number of records imported into #{@planter.app_name}: #{@planter.total_successes}"
end
def total_import_time(m)
- puts "Total time taken to import everything into #{@planter.app_name}: #{(m.real/60).round(2)} minutes"
+ add_to_results "Total time taken to import everything into #{@planter.app_name}: #{(m.real/60).round(2)} minutes"
end
def successes
if @planter.successes.count <= 0
- puts "\nNo records were imported! Boo!!!!\n"
+ add_to_results "\nNo records were imported! Boo!!!!\n"
else
count = Hash[@planter.successes.map { |key, value| [key.tableize.humanize, value] }]
- self.class.output("\nEstimated number of successful imports to #{@planter.app_name}", count)
+ output("\nEstimated number of successful imports to #{@planter.app_name}", count)
end
end
def failures
if @planter.failures.count <= 0
- puts "\nNo failed record imports!!!! Time to par-tay!!!!\n"
+ add_to_results "\nNo failed record imports!!!! Time to par-tay!!!!\n"
else
count = Hash[@planter.failures.map { |key, value| [key.tableize.humanize, value] }]
- self.class.output("\nEstimated number failed imports to #{@planter.app_name}", count)
+ output("\nEstimated number failed imports to #{@planter.app_name}", count)
end
end

0 comments on commit ecc6203

Please sign in to comment.