progressor 0.1.2 → 0.1.3
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 +4 -4
- data/README.md +20 -0
- data/lib/progressor/iteration.rb +54 -0
- data/lib/progressor/version.rb +1 -1
- data/lib/progressor.rb +9 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8b8a4cb127259a4f539935a5c3d33dde51918c5f06e2a6c0bfba817b13a44ab
|
4
|
+
data.tar.gz: 489755d94b178dfc2c98d75b12ee7839a652d47948b09c39fc1e4a8476b9897b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e074b0f2820dbc2a898258f7f89a36fc9a2f1867b13fd134b02cfe432589cb30beb6355facf0d80b0cb36a6deb562c7a36409382a1ab61bcee6aaaa6ec2306de
|
7
|
+
data.tar.gz: c8ebbad12c98156313ef2a16c55ff148f27f54bc1f8a3fa76371e714d4346847db7756e406db07e0a1c9d3acd4a1eb60e5bf8e3bbb090dfead4be93f59884bee
|
data/README.md
CHANGED
@@ -100,6 +100,26 @@ The format is:
|
|
100
100
|
<current>, t: <time from start>, t/i: <time per iteration>
|
101
101
|
```
|
102
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
|
+
|
103
123
|
## Configuration
|
104
124
|
|
105
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
|
data/lib/progressor/version.rb
CHANGED
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
|
|
@@ -99,4 +100,12 @@ class Progressor
|
|
99
100
|
def skip(n)
|
100
101
|
@sequence.skip(n)
|
101
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
|
102
111
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: progressor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Radev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- lib/progressor.rb
|
68
68
|
- lib/progressor/error.rb
|
69
69
|
- lib/progressor/formatting.rb
|
70
|
+
- lib/progressor/iteration.rb
|
70
71
|
- lib/progressor/limited_sequence.rb
|
71
72
|
- lib/progressor/unlimited_sequence.rb
|
72
73
|
- lib/progressor/version.rb
|