libin (owner)

Revisions

gist: 113827 Download_button fork
public
Description:
http://trak3r.blogspot.com/2009/05/better-progress-meter-for-your-rails.html
Public Clone URL: git://gist.github.com/113827.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Progress
  require 'action_view/helpers/date_helper'
  include ActionView::Helpers::DateHelper
    
  def initialize(total, interval = 10)
    @total = total
    @interval = interval
    @count = 0
    @start = Time.now
  end
    
  def tick
    @count += 1
    if 0 == @count % @interval
      sofar = Time.now
      elapsed = sofar - @start
      puts "been running for #{distance_of_time_in_words(@start, sofar)}"
      rate = elapsed / @count
      puts "at a rate of #{distance_of_time_in_words(sofar, (sofar - rate), true)} per item"
      finish = sofar + ((@total * rate) - elapsed)
      puts "should finish around #{distance_of_time_in_words(sofar, finish)}"
    end
  end
end
 
namespace :videos do
  desc "Remove videos from SpumCo if YouTube also removed them"
  task :purge => :environment do
    videos = Video.find(:all, :order => 'created_at asc')
    progress = Progress.new(videos.size)
    videos.each do |video|
      progress.tick
      video.destroy unless video.still_on_youtube?
    end
  end
end