file_scanner 3.1.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -22
- data/Rakefile +6 -0
- data/lib/file_scanner/version.rb +1 -1
- data/lib/file_scanner/worker.rb +1 -15
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4603c0a4323560155dedb12615cb60ed38b81cf6
|
4
|
+
data.tar.gz: 7852a338f0f88218649424d36a464ec7488b2ef5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4b730537560c4060a437e03c81df4bb21838d7bbcbf13e80e41949f1591586cd7bdfbe6ea9577098b8de21b3c3b64f9699ddcbc6156000bcda414fdec1bdbae6
|
7
|
+
data.tar.gz: 7e93e56d4a2e3c760be967fcc8d92c4d7ab14c6c071d03989e94bccf43c248123a971ff012d8765261d9921ba3eb2546f3a71893a5f93d9186fa618f194d7a03
|
data/README.md
CHANGED
@@ -9,16 +9,17 @@
|
|
9
9
|
* [Custom](#custom)
|
10
10
|
* [Worker](#worker)
|
11
11
|
* [Enumerator](#enumerator)
|
12
|
-
* [
|
12
|
+
* [Consuming results](#consuming-results)
|
13
13
|
* [Mode](#mode)
|
14
14
|
* [Check](#check)
|
15
15
|
* [Logger](#logger)
|
16
16
|
|
17
17
|
## Scope
|
18
|
-
This gem is aimed to collect a
|
18
|
+
This gem is aimed to lazily collect a list of files by path and a set of filters.
|
19
19
|
|
20
20
|
## Motivation
|
21
|
-
This gem is helpful to purge obsolete files or to promote relevant ones, by calling external services (CDN APIs) and/or local file system actions (copy, move, delete, etc).
|
21
|
+
This gem is helpful to purge obsolete files or to promote relevant ones, by calling external services (CDN APIs) and/or local file system actions (copy, move, delete, etc).
|
22
|
+
By working lazily, this library is aimed to work with a subset of large files list: just remember to apply a subset method to the final enumerator.
|
22
23
|
|
23
24
|
## Installation
|
24
25
|
Add this line to your application's Gemfile:
|
@@ -65,19 +66,17 @@ filters << ->(file) { File.directory?(file) }
|
|
65
66
|
The second step is to create the `Worker` instance by providing the path to scan and the list of filters to apply.
|
66
67
|
|
67
68
|
#### Enumerator
|
68
|
-
The `call` method of the worker return a lazy enumerator with the filtered elements
|
69
|
+
The `call` method of the worker return a lazy enumerator with the filtered elements:
|
69
70
|
```ruby
|
70
71
|
worker = FileScanner::Worker.new(path: "~/Downloads", filters: filters, slice: 35)
|
71
72
|
p worker.call
|
72
73
|
=> #<Enumerator::Lazy: ...
|
73
74
|
```
|
74
75
|
|
75
|
-
####
|
76
|
-
To
|
76
|
+
#### Consuming results
|
77
|
+
To leverage on the lazy behaviour remember to call a subset operator on the resulting enumerator:
|
77
78
|
```ruby
|
78
|
-
worker.call
|
79
|
-
# perform actions on a slice of at max 35 elements
|
80
|
-
end
|
79
|
+
worker.call.take(1000)
|
81
80
|
```
|
82
81
|
|
83
82
|
#### Mode
|
@@ -101,17 +100,4 @@ If you dare to trace what the worker is doing (including errors), you can specif
|
|
101
100
|
```ruby
|
102
101
|
my_logger = Logger.new("my_file.log")
|
103
102
|
worker = FileScanner::Worker.new(loader: loader, logger: my_logger)
|
104
|
-
worker.call do |slice|
|
105
|
-
fail "Doh!" # will log error to my_file.log and re-raise exception
|
106
|
-
end
|
107
|
-
```
|
108
|
-
|
109
|
-
If you want to easily pass the same logger instance to the actions you are performing, it's available as the second argument of the block:
|
110
|
-
```ruby
|
111
|
-
require "fileutils"
|
112
|
-
|
113
|
-
worker.call do |slice, logger|
|
114
|
-
logger.info { "going to remove #{slice.size} files from disk!" }
|
115
|
-
FileUtils.rm_rf(slice)
|
116
|
-
end
|
117
103
|
```
|
data/Rakefile
CHANGED
data/lib/file_scanner/version.rb
CHANGED
data/lib/file_scanner/worker.rb
CHANGED
@@ -4,7 +4,6 @@ require "file_scanner/filters"
|
|
4
4
|
|
5
5
|
module FileScanner
|
6
6
|
class Worker
|
7
|
-
SLICE = 1000
|
8
7
|
ALL = :all?
|
9
8
|
ANY = :any?
|
10
9
|
|
@@ -12,23 +11,18 @@ module FileScanner
|
|
12
11
|
|
13
12
|
def initialize(path:,
|
14
13
|
filters: Filters::defaults,
|
15
|
-
slice: SLICE,
|
16
14
|
all: false,
|
17
15
|
check: false,
|
18
16
|
logger: Logger.new(nil))
|
19
17
|
@path = File.expand_path(path)
|
20
18
|
@filters = filters
|
21
|
-
@slice = slice.to_i
|
22
19
|
@mode = mode(all)
|
23
20
|
@check = check
|
24
21
|
@logger = logger
|
25
22
|
end
|
26
23
|
|
27
24
|
def call
|
28
|
-
|
29
|
-
slices.each do |slice|
|
30
|
-
yield(slice, @logger)
|
31
|
-
end
|
25
|
+
paths.lazy.select { |file| valid?(file) && filter(file) }
|
32
26
|
rescue StandardError => e
|
33
27
|
@logger.error { e.message }
|
34
28
|
raise e
|
@@ -53,13 +47,5 @@ module FileScanner
|
|
53
47
|
private def paths
|
54
48
|
Find.find(@path)
|
55
49
|
end
|
56
|
-
|
57
|
-
private def filtered
|
58
|
-
paths.lazy.select { |file| valid?(file) && filter(file) }
|
59
|
-
end
|
60
|
-
|
61
|
-
private def slices
|
62
|
-
filtered.each_slice(@slice)
|
63
|
-
end
|
64
50
|
end
|
65
51
|
end
|