progressor 0.1.0 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67733b5ccdcf4efdbe628a511246b38238f5bd368e320ae9251ec34dc1be7cc5
4
- data.tar.gz: 2c84c065e48fdfab6e9ab4079c7c595be60cbfe16f7378970a451cebe36af326
3
+ metadata.gz: e8b8a4cb127259a4f539935a5c3d33dde51918c5f06e2a6c0bfba817b13a44ab
4
+ data.tar.gz: 489755d94b178dfc2c98d75b12ee7839a652d47948b09c39fc1e4a8476b9897b
5
5
  SHA512:
6
- metadata.gz: 4735274940fcb4bd4b54089dce074bbbb6fe42cc530db202f3a3f4738c396624685d3d6740e8707882d8cfa9c0f4e2053851f5ed1c987c3d0a88c1fb665b2f28
7
- data.tar.gz: 375e03f4109ea4a74de4483b55aa889611d7d2d0ec8901950363b2e5196f8652fb8995d20bcddb8d8118e842e6ead21d71bb47dec00cd071795962361f505371
6
+ metadata.gz: e074b0f2820dbc2a898258f7f89a36fc9a2f1867b13fd134b02cfe432589cb30beb6355facf0d80b0cb36a6deb562c7a36409382a1ab61bcee6aaaa6ec2306de
7
+ data.tar.gz: c8ebbad12c98156313ef2a16c55ff148f27f54bc1f8a3fa76371e714d4346847db7756e406db07e0a1c9d3acd4a1eb60e5bf8e3bbb090dfead4be93f59884bee
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![Build Status](https://travis-ci.org/AndrewRadev/progressor.svg?branch=master)](https://travis-ci.org/AndrewRadev/progressor)
2
+ [![Gem Version](https://badge.fury.io/rb/progressor.svg)](https://badge.fury.io/rb/progressor)
3
+
1
4
  Full documentation for the latest released version can be found at: https://www.rubydoc.info/gems/progressor
2
5
 
3
6
  ## Basic example
@@ -97,6 +100,26 @@ The format is:
97
100
  <current>, t: <time from start>, t/i: <time per iteration>
98
101
  ```
99
102
 
103
+ ## Simpler iteration
104
+
105
+ For `ActiveRecord` and other iterable collections, it's possible to skip some boilerplate. For example, you might start from this:
106
+
107
+ ``` ruby
108
+ Record.not_processed.find_each do |record|
109
+ record.process
110
+ end
111
+ ```
112
+
113
+ In order to add measurements, you could instantiate a `Progressor` etc like in the above example, or you could do this:
114
+
115
+ ``` ruby
116
+ Progressor::Iteration.find_each(Record.not_processed) do |record|
117
+ record.process
118
+ end
119
+ ```
120
+
121
+ And it'll add some default print statements. Check the API documentation or the code for details.
122
+
100
123
  ## Configuration
101
124
 
102
125
  Apart from `total_count`, which is optional and affects the kind of sequence that will be stored, you can provide `min_samples` and `max_samples`. You can also provide a custom formatter:
@@ -0,0 +1,54 @@
1
+ module Progressor::Iteration
2
+ # Iterate the given collection, assuming it responds to:
3
+ #
4
+ # - The given `method` with a block that yields a single item
5
+ # - optionally, `count` that returns the number of results
6
+ #
7
+ # It yields two items -- the item from the collection and a numeric index. It
8
+ # prints the progress automatically for each loop.
9
+ #
10
+ # This is meant to be used as a convenience method for ActiveRecord
11
+ # collections with `find_each` or `each`, but it could really be used for
12
+ # anything that works with this interface.
13
+ #
14
+ # Inputs:
15
+ #
16
+ # - method: the method name to invoke on `collection`
17
+ # - collection: the iterable object
18
+ # - format: the method to use for printing each individual record. Defaults to `:to_s`
19
+ # - options: passed along to `Progressor::new`
20
+ #
21
+ def self.iterate(method, collection, format: :to_s, **options, &block)
22
+ if !collection.respond_to?(method)
23
+ raise Progressor::Error.new("Given collection doesn't respond to ##{method}")
24
+ end
25
+
26
+ if collection.respond_to?(:count)
27
+ progressor = Progressor.new(total_count: collection.count, **options)
28
+ else
29
+ progressor = Progressor.new(**options)
30
+ end
31
+
32
+ index = 0
33
+
34
+ collection.public_send(method) do |item|
35
+ progressor.run do |progress|
36
+ Kernel.puts "[#{progress}] Working on #{item.public_send(format)}"
37
+ block.call(item, index)
38
+ index += 1
39
+ end
40
+ end
41
+ end
42
+
43
+ # Iterates using `.iterate` and the `#find_each` method
44
+ #
45
+ def self.find_each(collection, **options, &block)
46
+ iterate(:find_each, collection, **options, &block)
47
+ end
48
+
49
+ # Iterates using `.iterate` and the `#each` method
50
+ #
51
+ def self.each(collection, **options, &block)
52
+ iterate(:each, collection, **options, &block)
53
+ end
54
+ end
@@ -61,7 +61,7 @@ class Progressor
61
61
  end
62
62
 
63
63
  # Outputs a textual representation of the current state of the
64
- # UnlimitedSequence. Shows:
64
+ # LimitedSequence. Shows:
65
65
  #
66
66
  # - the current number of iterations and the total count
67
67
  # - completion level in percentage
@@ -117,6 +117,14 @@ class Progressor
117
117
  remaining_time.round(2)
118
118
  end
119
119
 
120
+ # Returns the time since the object was instantiated, formatted like all
121
+ # the other durations. Useful for a final message to compare initial
122
+ # estimation to actual elapsed time.
123
+ #
124
+ def elapsed_time
125
+ format_time(Time.now - @start_time)
126
+ end
127
+
120
128
  private
121
129
 
122
130
  def average(collection)
@@ -71,7 +71,7 @@ class Progressor
71
71
 
72
72
  [
73
73
  "#{@current + 1}",
74
- "t: #{format_time(Time.now - @start_time)}",
74
+ "t: #{elapsed_time}",
75
75
  "t/i: #{format_time(per_iteration)}",
76
76
  ].join(', ')
77
77
  end
@@ -95,6 +95,13 @@ class Progressor
95
95
  # No estimation possible
96
96
  end
97
97
 
98
+ # Returns the time since the object was instantiated, formatted like all
99
+ # the other durations.
100
+ #
101
+ def elapsed_time
102
+ format_time(Time.now - @start_time)
103
+ end
104
+
98
105
  private
99
106
 
100
107
  def average(collection)
@@ -1,3 +1,3 @@
1
1
  class Progressor
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.3'
3
3
  end
data/lib/progressor.rb CHANGED
@@ -3,6 +3,7 @@ require 'progressor/error'
3
3
  require 'progressor/formatting'
4
4
  require 'progressor/limited_sequence'
5
5
  require 'progressor/unlimited_sequence'
6
+ require 'progressor/iteration'
6
7
 
7
8
  require 'benchmark'
8
9
 
@@ -34,7 +35,7 @@ require 'benchmark'
34
35
  # ...
35
36
  #
36
37
  class Progressor
37
- include Formatting
38
+ extend Formatting
38
39
 
39
40
  # Utility method to print a message with the time it took to run the contents
40
41
  # of the block.
@@ -48,8 +49,10 @@ class Progressor
48
49
  #
49
50
  def self.puts(message, &block)
50
51
  Kernel.puts "#{message}..."
51
- measurement = Benchmark.measure { block.call }
52
+ result = nil
53
+ measurement = Benchmark.measure { result = block.call }
52
54
  Kernel.puts "#{message} DONE: #{format_time(measurement.real)}"
55
+ result
53
56
  end
54
57
 
55
58
  # Set up a new Progressor instance. Optional parameters:
@@ -97,4 +100,12 @@ class Progressor
97
100
  def skip(n)
98
101
  @sequence.skip(n)
99
102
  end
103
+
104
+ # Returns the time since the sequence was started, formatted like all the
105
+ # other durations. Useful for a final message to compare initial estimation
106
+ # to actual elapsed time.
107
+ #
108
+ def elapsed_time
109
+ @sequence.elapsed_time
110
+ end
100
111
  end
metadata CHANGED
@@ -1,43 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: progressor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Radev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-15 00:00:00.000000000 Z
11
+ date: 2022-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.17'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.17'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
17
  - - "~>"
32
18
  - !ruby/object:Gem::Version
33
- version: '10.0'
19
+ version: 12.3.3
34
20
  type: :development
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
24
  - - "~>"
39
25
  - !ruby/object:Gem::Version
40
- version: '10.0'
26
+ version: 12.3.3
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rspec
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -81,6 +67,7 @@ files:
81
67
  - lib/progressor.rb
82
68
  - lib/progressor/error.rb
83
69
  - lib/progressor/formatting.rb
70
+ - lib/progressor/iteration.rb
84
71
  - lib/progressor/limited_sequence.rb
85
72
  - lib/progressor/unlimited_sequence.rb
86
73
  - lib/progressor/version.rb
@@ -88,7 +75,7 @@ homepage: https://github.com/AndrewRadev/progressor
88
75
  licenses:
89
76
  - MIT
90
77
  metadata: {}
91
- post_install_message:
78
+ post_install_message:
92
79
  rdoc_options: []
93
80
  require_paths:
94
81
  - lib
@@ -103,8 +90,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
90
  - !ruby/object:Gem::Version
104
91
  version: '0'
105
92
  requirements: []
106
- rubygems_version: 3.0.0
107
- signing_key:
93
+ rubygems_version: 3.1.6
94
+ signing_key:
108
95
  specification_version: 4
109
96
  summary: Measure iterations in a long-running task
110
97
  test_files: []