Skip to content

Commit

Permalink
Added logging to to_csv_file tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
quirkey committed Feb 6, 2009
1 parent fed755c commit 5ee66fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/porterable.rb
Expand Up @@ -7,11 +7,11 @@

module Porterable

def export_filename(prefix = nil)
def self.export_filename(prefix = nil)
"#{prefix}_export-" + Time.now.strftime("%Y%m%d-%H%M%S") + ".csv"
end

def export_content_type(user_agent = nil)
def self.export_content_type(user_agent = nil)
if user_agent =~ /windows/i
'application/vnd.ms-excel'
else
Expand Down
12 changes: 10 additions & 2 deletions lib/porterable/is_porterable.rb
Expand Up @@ -64,9 +64,17 @@ def to_csv(options = {}, &block)
end

def to_csv_file(filename, options = {}, &block)
csv_data = to_csv(options, &block)
csv_data = to_csv(options) do |count, total_rows|
pct = (count.to_f / total_rows.to_f) * 100.0
logger.info "Exported row #{count}/#{total_rows} - #{pct}%"
yield(count, total_rows) if block_given?
end
temp_file = Tempfile.new("#{rand Time.now.to_i}-#{rand(1000)}--")
temp_file.close
temp_path = temp_file.path
File.open(temp_path, 'w') {|f| f << csv_data }
FileUtils.mkdir_p(File.dirname(filename))
File.open(filename, 'w') {|f| f << csv_data }
FileUtils.mv(temp_path, filename)
end

def load_csv_str(data)
Expand Down

0 comments on commit 5ee66fb

Please sign in to comment.