Skip to content

Loading…

Fixing elapsed time presentation on import task #480

Closed
wants to merge 1 commit into from

2 participants

@wandenberg

The time was being shown in a wrong way when the elapsed time is a float number.

@karmi karmi closed this in 6fef487
@karmi
Owner

Hi, thanks for looking into that so thoroughly! I consider the Rake task just a utility method, though, and don't think we should add some Utils namespace just for that...

@wandenberg

Hi,
I moved the code from tasks.rb to utils.rb to be easier test it.
If you want I can apply the fix on tasks.rb only, just will stay without tests.
Can I do that? The fix is simple but a good presentation be part of a good software.

Regards,
Wandenberg

@karmi
Owner

Hi, you're absolutely right it should have been fixed. I just fixed the method in tasks.rb, see karmi/retire@6fef487.

While the import Rake task is very useful, it's not a end-all solution and people should be more then ready to write their own feeder scripts; it's very, very convenient and easy to do so in Ruby. The Rake task is a mere convenience for simple cases and a help for beginners.

@NOX73 NOX73 added a commit to NOX73/tire that referenced this pull request
@karmi Improved the elapsed time reporting in the Rake task
Closes #480
5d6b0cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 13, 2012
  1. @wandenberg
This page is out of date. Refresh to see the latest.
Showing with 28 additions and 18 deletions.
  1. +1 −18 lib/tire/tasks.rb
  2. +20 −0 lib/tire/utils.rb
  3. +7 −0 test/unit/tire_test.rb
View
19 lib/tire/tasks.rb
@@ -17,23 +17,6 @@
DESC
desc full_comment_import
task :import do |t|
-
- def elapsed_to_human(elapsed)
- hour = 60*60
- day = hour*24
-
- case elapsed
- when 0..59
- "#{sprintf("%1.5f", elapsed)} seconds"
- when 60..hour-1
- "#{elapsed/60} minutes and #{elapsed % 60} seconds"
- when hour..day
- "#{elapsed/hour} hours and #{elapsed % hour} minutes"
- else
- "#{elapsed/hour} hours"
- end
- end
-
if ENV['CLASS'].to_s == ''
puts '='*90, 'USAGE', '='*90, full_comment_import, ""
exit(1)
@@ -95,7 +78,7 @@ def paginate(options = {})
end
end
- puts "", '='*80, "Import finished in #{elapsed_to_human(elapsed)}"
+ puts "", '='*80, "Import finished in #{Tire::Utils.elapsed_to_human(elapsed)}"
end
namespace :index do
View
20 lib/tire/utils.rb
@@ -12,6 +12,26 @@ def unescape(s)
URI.decode_www_form_component(s)
end
+ def elapsed_to_human(elapsed)
+ hour = 60*60
+ day = hour*24
+
+ seconds = sprintf("%1.5f", (elapsed % 60))
+ minutes = ((elapsed/60) % 60).to_i
+ hours = (elapsed/hour).to_i
+
+ case elapsed
+ when 0..59
+ "#{seconds} seconds"
+ when 60..hour-1
+ "#{minutes} minutes and #{seconds} seconds"
+ when hour..day
+ "#{hours} hours and #{minutes} minutes"
+ else
+ "#{hours} hours"
+ end
+ end
+
extend self
end
end
View
7 test/unit/tire_test.rb
@@ -118,6 +118,13 @@ class TireTest < Test::Unit::TestCase
assert_equal 'foo!', Utils.unescape('foo%21')
end
+ should "format time to print" do
+ assert_equal '30.51235 seconds' , Utils.elapsed_to_human(30.5123478)
+ assert_equal '1 minutes and 10.51235 seconds', Utils.elapsed_to_human(70.5123478)
+ assert_equal '1 hours and 10 minutes' , Utils.elapsed_to_human(4200.5123478)
+ assert_equal '25 hours' , Utils.elapsed_to_human(90600.5123478)
+ end
+
end
end
Something went wrong with that request. Please try again.