eta 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in eta.gemspec
4
4
  gemspec
5
+ gem 'rake'
6
+ gem 'minitest', '3.5.0'
7
+ gem 'timecop'
data/README.md CHANGED
@@ -18,7 +18,29 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ There are 2 ways to use Eta: around an `Eta::Process`, or with an
22
+ `Eta::Estimator`.
23
+
24
+ ### Eta::Process
25
+
26
+ TODO: Write usage instructions for `Eta::Process` here
27
+
28
+ ### Eta::Estimator
29
+
30
+ If you have a task, and can calculate what percentage of the task you're
31
+ finished with, you can use an `Eta::Estimator` to estimate how much time is
32
+ left, and when the task will be done.
33
+
34
+ Use it like this:
35
+
36
+ files_to_delete = Dir.glob('junk/*')
37
+ estimator = Eta::Estimator.new
38
+ estimator.start!
39
+ files_to_delete.each_with_index do |file, i|
40
+ File.delete(file)
41
+ estimator.percent_done = i.to_f / files_to_delete.size
42
+ puts "Should be done in #{estimator.time_left} seconds, by #{estimator.eta}"
43
+ end
22
44
 
23
45
  ## Contributing
24
46
 
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = "test/**/*_test.rb"
6
+ t.verbose = true
7
+ end
@@ -0,0 +1,29 @@
1
+ module Eta
2
+ class Estimator
3
+ attr_accessor :percent_done
4
+
5
+ def start!
6
+ @start_time = current_time
7
+ end
8
+
9
+ def eta
10
+ Time.at(current_time.to_i + time_left)
11
+ end
12
+
13
+ def time_left
14
+ expected_duration - elapsed_time
15
+ end
16
+
17
+ def expected_duration
18
+ elapsed_time / percent_done
19
+ end
20
+
21
+ def elapsed_time
22
+ current_time - @start_time
23
+ end
24
+
25
+ def current_time
26
+ Time.now.to_i
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Eta
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,72 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/mock'
3
+ require 'timecop'
4
+ require 'eta/estimator'
5
+
6
+ describe Eta::Estimator do
7
+ it 'should be able to stub #current_time' do
8
+ estimator = Eta::Estimator.new { 0 }
9
+ Timecop.freeze(Time.at(0))
10
+ estimator.current_time.must_equal 0
11
+ end
12
+
13
+ it 'can calculate #elapsed_time' do
14
+ Timecop.freeze(Time.at(0))
15
+ estimator = Eta::Estimator.new
16
+ estimator.start!
17
+ Timecop.freeze(Time.at(100))
18
+ estimator.elapsed_time.must_equal 100
19
+ end
20
+
21
+ it 'can calculate #expected_duration' do
22
+ Timecop.freeze(Time.at(0))
23
+ estimator = Eta::Estimator.new
24
+ estimator.start!
25
+
26
+ Timecop.freeze(Time.at(1000))
27
+
28
+ estimator.percent_done = 0.25
29
+ estimator.expected_duration.must_equal 4000
30
+ estimator.percent_done = 0.5
31
+ estimator.expected_duration.must_equal 2000
32
+ estimator.percent_done = 0.8
33
+ estimator.expected_duration.must_equal 1250
34
+
35
+ Timecop.freeze(Time.at(500))
36
+ estimator.expected_duration.must_equal 625
37
+ end
38
+
39
+ it 'can calculate #time_left' do
40
+ Timecop.freeze(Time.at(0))
41
+ estimator = Eta::Estimator.new
42
+ estimator.start!
43
+
44
+ Timecop.freeze(Time.at(1000))
45
+
46
+ estimator.percent_done = 0.25
47
+ estimator.time_left.must_equal 3000
48
+ estimator.percent_done = 0.5
49
+ estimator.time_left.must_equal 1000
50
+
51
+ Timecop.freeze(Time.at(3000))
52
+ estimator.percent_done = 0.75
53
+ estimator.time_left.must_equal 1000
54
+ end
55
+
56
+ it 'can calculate #eta' do
57
+ Timecop.freeze(Time.at(0))
58
+ estimator = Eta::Estimator.new
59
+ estimator.start!
60
+
61
+ Timecop.freeze(Time.at(500))
62
+
63
+ estimator.percent_done = 0.25
64
+ estimator.eta.must_equal Time.at(2000)
65
+ estimator.percent_done = 0.5
66
+ estimator.eta.must_equal Time.at(1000)
67
+
68
+ Timecop.freeze(Time.at(3000))
69
+ estimator.percent_done = 0.75
70
+ estimator.eta.must_equal Time.at(4000)
71
+ end
72
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000 Z
12
+ date: 2013-06-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: childprocess
@@ -41,7 +41,9 @@ files:
41
41
  - Rakefile
42
42
  - eta.gemspec
43
43
  - lib/eta.rb
44
+ - lib/eta/estimator.rb
44
45
  - lib/eta/version.rb
46
+ - test/eta/estimator_test.rb
45
47
  - test/functional.rb
46
48
  homepage: ''
47
49
  licenses: []
@@ -63,9 +65,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
65
  version: '0'
64
66
  requirements: []
65
67
  rubyforge_project:
66
- rubygems_version: 1.8.24
68
+ rubygems_version: 1.8.23
67
69
  signing_key:
68
70
  specification_version: 3
69
71
  summary: ''
70
72
  test_files:
73
+ - test/eta/estimator_test.rb
71
74
  - test/functional.rb
75
+ has_rdoc: