ke 0.0.1 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fbfaca5a83423f479bc2a76e0b7903397b5fdd0
4
- data.tar.gz: eff7b260a09a7ae6c2c5e4d7c8d58ac37843c1be
3
+ metadata.gz: a454abebf9c74fc4899e037d679ffc93ff2c3c5f
4
+ data.tar.gz: ddb889ef03b9d3ebfcd45aec0cc492825d0db945
5
5
  SHA512:
6
- metadata.gz: 9bb0ebe5335d9f3b31c2a21aa71bfb7c4e67c65f102a76d5be0818e84fd25b904902f6a32b4e035adefb7f9060dc02b04214daad3356713ad857c1b383da3d9b
7
- data.tar.gz: c5718d042ff068d8dd43520ab670d6b5478ed91aab5934c47cd44dc7696cd882c4573d1374c6c804d0450a1e6cefd50bff77707d1142ee713b5a1edda5cd768d
6
+ metadata.gz: e9c14271dbff7f588832f90d102a006ad808a6c6a6c9e606f50bc950e2e587aa601fc53e94aa750d8332905a92cef6dd9e7d5de5bf237f9d0813c06fdbb870b4
7
+ data.tar.gz: c1dfe86497dab79e7957510475b461a46a5a32719056b4d420efdabed6247c9ce08d088a5327d24b9bf0f982fbb6d0788b6f970015a6df97ae4561766fd9c939
data/README.md CHANGED
@@ -4,45 +4,88 @@ Measure progress of Ruby code with the ability to provide estimates for time unt
4
4
 
5
5
  Supports tasks with a known (determinate) and unknown (indeterminate) number of iterations. For indeterminate tasks it's only possible to provide a rate. For determinate tasks it's possible to provide both a rate and an estimated remaining time.
6
6
 
7
+ ## Installation
8
+
9
+ Install them gem with `gem install ke`.
10
+
11
+ If you're using Bundler then add it to your Gemfile with `gem ke` and run `bundle install`.
12
+
7
13
  ## Usage
8
14
 
9
- Install them gem with `gem install ke`. If you're using Bundler then add it to your Gemfile with `gem ke` and run `bundle install`. Usage examples below:
15
+ Assume this is your code before, performing some work each iteration:
10
16
 
11
- ### Example 1: An indeterminate task with a multi line reporting output
17
+ ```ruby
18
+ 5.times do
19
+ sleep 0.1 # do work
20
+ end
21
+ ```
22
+
23
+ To report on the progress as the code runs, you need to wrap with in `Ke.job {}` block, and wrap the "work" code in the iteration part in a `job.tick {}` block:
12
24
 
13
25
  ```ruby
14
- require 'ke'
15
-
16
- task = Ke::IndeterminateTask.new
17
- reporter = Ke::MultiLineReporter.new(task, "indeterminate task")
18
- task.start
19
- reporter.print_start
20
- loop do
21
- break if rand(200) == 50
22
- sleep 0.1
23
- task.tick
24
- reporter.print_tick if task.tick_count % 10 == 0
26
+ Ke.job do |job|
27
+ 5.times do
28
+ job.tick do
29
+ sleep 1
30
+ end
31
+ end
25
32
  end
26
- task.complete
27
- reporter.print_complete
28
33
  ```
29
34
 
30
- ### Example 2: A determinate task with a single line reporting output
35
+ Now when we run this, we'll see a progress report printed to `STDOUT` as the iteration proceeds:
36
+
37
+ ```
38
+ Starting task
39
+ Running task, 0.0 minutes elapsed, 27027.03 ticks/second
40
+ Running task, 0.02 minutes elapsed, 2.0 ticks/second
41
+ Running task, 0.03 minutes elapsed, 1.5 ticks/second
42
+ Running task, 0.05 minutes elapsed, 1.33 ticks/second
43
+ Running task, 0.07 minutes elapsed, 1.25 ticks/second
44
+ Completed task, 5.005121 total duration
45
+ ```
46
+
47
+ ### More examples
31
48
 
32
49
  ```ruby
33
- require 'ke'
34
-
35
- task = Ke::DeterminateTask.new(total_ticks: 50)
36
- reporter = Ke::SingleLineReporter.new(task, "determinate task")
37
- task.start
38
- reporter.print_start
39
- 50.times do
40
- sleep 0.1
41
- task.tick
42
- reporter.print_tick if task.tick_count % 10 == 0
50
+ # Set a specific reporter.
51
+ Ke.job(reporter: Ke::MultiLineReporter) do |job|
52
+ 50.times do
53
+ job.tick do
54
+ sleep 0.1
55
+ end
56
+ end
57
+ end
58
+
59
+ # Set the total number of ticks, so we use DeterminateTask and get a time remaining estimates.
60
+ Ke.job(50) do |job|
61
+ 50.times do
62
+ job.tick do
63
+ sleep 0.1
64
+ end
65
+ end
66
+ end
67
+
68
+ # Report progress every 10 ticks.
69
+ Ke.job(report_every: 10) do |job|
70
+ 50.times do
71
+ job.tick do
72
+ sleep 0.1
73
+ end
74
+ end
75
+ end
76
+
77
+ # Report to a file.
78
+ File.open("log.txt", "w") do |f|
79
+ f.sync = true
80
+
81
+ Ke.job(io: f) do |job|
82
+ 50.times do
83
+ job.tick do
84
+ sleep 0.1
85
+ end
86
+ end
87
+ end
43
88
  end
44
- task.complete
45
- reporter.print_complete
46
89
  ```
47
90
 
48
91
  ## Custom reporters
@@ -51,7 +94,6 @@ You can add your own reporters as needed. Scope the existing reporters to learn
51
94
 
52
95
  ## TODO
53
96
 
54
- - Provide some syntactic sugar for the API, it's a bit rough at present
55
97
  - Provide some pleasant looking default reporters (the current 2 work, but are ugly)
56
98
  - Add some real tests
57
99
  - Add more Ruby versions to Travis CI (anything 1.9+ compat should work)
data/examples.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'ke'
2
+
3
+ # Simplest usage, no arguments.
4
+ Ke.job do |job|
5
+ 50.times do
6
+ job.tick do
7
+ sleep 0.1
8
+ end
9
+ end
10
+ end
11
+
12
+ # Set a specific reporter.
13
+ Ke.job(reporter: Ke::MultiLineReporter) do |job|
14
+ 50.times do
15
+ job.tick do
16
+ sleep 0.1
17
+ end
18
+ end
19
+ end
20
+
21
+ # Set the total number of ticks, so we use DeterminateTask and get a time remaining estimates.
22
+ Ke.job(50) do |job|
23
+ 50.times do
24
+ job.tick do
25
+ sleep 0.1
26
+ end
27
+ end
28
+ end
29
+
30
+ # Report progress every 10 ticks.
31
+ Ke.job(report_every: 10) do |job|
32
+ 50.times do
33
+ job.tick do
34
+ sleep 0.1
35
+ end
36
+ end
37
+ end
38
+
39
+ # Report to a file.
40
+ File.open("log.txt", "w") do |f|
41
+ f.sync = true
42
+
43
+ Ke.job(io: f) do |job|
44
+ 50.times do
45
+ job.tick do
46
+ sleep 0.1
47
+ end
48
+ end
49
+ end
50
+ end
data/lib/ke.rb CHANGED
@@ -4,3 +4,29 @@ require 'ke/indeterminate_task'
4
4
  require 'ke/determinate_task'
5
5
  require 'ke/reporters/single_line_reporter'
6
6
  require 'ke/reporters/multi_line_reporter'
7
+ require 'ke/job'
8
+
9
+ module Ke
10
+ def self.job(*args, &blk)
11
+ total_ticks = args[0] if Integer === args[0]
12
+ label = args[1] if String === args[1]
13
+ opts = args.detect { |arg| Hash === arg } || {}
14
+
15
+ task_class = total_ticks ? DeterminateTask : IndeterminateTask
16
+ task_opts = opts[:task_opts] || {}
17
+ task_opts = task_opts.merge(total_ticks: total_ticks) if total_ticks
18
+
19
+ reporter_class = opts[:reporter] || MultiLineReporter
20
+ reporter_label = label || "task"
21
+ reporter_io = opts[:io] || STDOUT
22
+
23
+ job_class = Job
24
+ job_opts = opts || {}
25
+
26
+ task = task_class.new(task_opts)
27
+ reporter = reporter_class.new(task, reporter_label, reporter_io)
28
+ job = job_class.new(task, reporter, job_opts)
29
+
30
+ job.run(&blk)
31
+ end
32
+ end
@@ -1,5 +1,7 @@
1
1
  module Ke
2
2
  class DeterminateTask < IndeterminateTask
3
+ attr_reader :total_ticks
4
+
3
5
  def initialize(opts = {})
4
6
  super
5
7
  @total_ticks = opts[:total_ticks]
data/lib/ke/job.rb ADDED
@@ -0,0 +1,32 @@
1
+ module Ke
2
+ class Job
3
+ attr_reader :task, :reporter, :opts
4
+
5
+ def initialize(task, reporter, opts = {})
6
+ @task = task
7
+ @reporter = reporter
8
+ @opts = opts
9
+
10
+ @opts[:report_every] ||= if @task.respond_to?(:total_ticks)
11
+ [1, [@task.total_ticks / 10, 100].min].max
12
+ else
13
+ 1
14
+ end
15
+ end
16
+
17
+ def tick
18
+ task.tick
19
+ reporter.print_tick if task.tick_count % opts[:report_every] == 0
20
+ yield
21
+ end
22
+
23
+ def run
24
+ task.start
25
+ reporter.print_start
26
+ return_val = yield(self)
27
+ task.complete
28
+ reporter.print_complete
29
+ return_val
30
+ end
31
+ end
32
+ end
data/lib/ke/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ke
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ke
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Dodwell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-28 00:00:00.000000000 Z
11
+ date: 2014-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -51,11 +51,13 @@ files:
51
51
  - LICENSE
52
52
  - README.md
53
53
  - Rakefile
54
+ - examples.rb
54
55
  - ke.gemspec
55
56
  - lib/ke.rb
56
57
  - lib/ke/capped_sample.rb
57
58
  - lib/ke/determinate_task.rb
58
59
  - lib/ke/indeterminate_task.rb
60
+ - lib/ke/job.rb
59
61
  - lib/ke/reporters/multi_line_reporter.rb
60
62
  - lib/ke/reporters/single_line_reporter.rb
61
63
  - lib/ke/version.rb
@@ -88,3 +90,4 @@ summary: Measure progress of Ruby code.
88
90
  test_files:
89
91
  - test/ke_test.rb
90
92
  - test/test_helper.rb
93
+ has_rdoc: