file_scanner 2.3.0 → 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -43
- data/file_scanner.gemspec +1 -1
- data/lib/file_scanner/version.rb +1 -1
- data/lib/file_scanner/worker.rb +22 -12
- metadata +4 -5
- data/lib/file_scanner/loader.rb +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28d079722f80fd8d08f242cea61b0994d6b2bb35
|
4
|
+
data.tar.gz: 0713446d2aa7ce967b7005aa96dcead35c3ea4e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df069987cf734fe0ec58b7c857fd24524179fcac86ac5b7ab8663548bdfb6b28ad366c8182e2ed75b01c7c00735d928ebc30352b5d49e70bb509b2fa49a8fe89
|
7
|
+
data.tar.gz: 8dd06c2d10ae10c60a437b574431e7eb439845c1175b4e95b09370f755bfccf76dd91773001981a4950b906d98b92f02f437b6c2dbe975b613842f85cd776638
|
data/README.md
CHANGED
@@ -4,15 +4,13 @@
|
|
4
4
|
* [Motivation](#motivation)
|
5
5
|
* [Installation](#installation)
|
6
6
|
* [Usage](#usage)
|
7
|
-
* [Loader](#loader)
|
8
7
|
* [Filters](#filters)
|
9
8
|
* [Defaults](#defaults)
|
10
9
|
* [Custom](#custom)
|
11
10
|
* [Worker](#worker)
|
12
|
-
* [Mode](#mode)
|
13
|
-
* [Batches](#batches)
|
14
|
-
* [Limit](#limit)
|
15
11
|
* [Enumerator](#enumerator)
|
12
|
+
* [Block](#block)
|
13
|
+
* [Mode](#mode)
|
16
14
|
* [Logger](#logger)
|
17
15
|
|
18
16
|
## Scope
|
@@ -39,16 +37,8 @@ gem install file_scanner
|
|
39
37
|
|
40
38
|
## Usage
|
41
39
|
|
42
|
-
### Loader
|
43
|
-
The first step is to create a `Loader` instance by specifying the path where the files need to be scanned with optional extensions list:
|
44
|
-
```ruby
|
45
|
-
require "file_scanner"
|
46
|
-
|
47
|
-
loader = FileScanner::Loader.new(path: ENV["HOME"], extensions: %w[html txt])
|
48
|
-
```
|
49
|
-
|
50
40
|
### Filters
|
51
|
-
The
|
41
|
+
The first step is to provide the filters list to select file paths for which the `call` method is *truthy*.
|
52
42
|
|
53
43
|
#### Defaults
|
54
44
|
If you specify no filters the default ones are loaded, selecting files by:
|
@@ -65,51 +55,35 @@ filters = [a_week_ago, one_two_mb, hidden]
|
|
65
55
|
```
|
66
56
|
|
67
57
|
#### Custom
|
68
|
-
It is convenient to create custom filters by
|
58
|
+
It is convenient to create custom filters by using `Proc` instances that satisfy the `callable` protocol:
|
69
59
|
```ruby
|
70
60
|
filters << ->(file) { File.directory?(file) }
|
71
61
|
```
|
72
62
|
|
73
63
|
### Worker
|
74
|
-
|
75
|
-
```ruby
|
76
|
-
worker = FileScanner::Worker.new(loader: loader, filters: filters)
|
77
|
-
worker.call do |paths|
|
78
|
-
# do whatever you want with the paths list
|
79
|
-
end
|
80
|
-
```
|
81
|
-
|
82
|
-
### Mode
|
83
|
-
By default the worker will select paths by applying any of the matching filters: this is it, it suffice just one of the specified filters to be true to grab the path.
|
84
|
-
In case you want restrict paths selection by all matching filters, just specify it:
|
85
|
-
```ruby
|
86
|
-
worker = FileScanner::Worker.new(loader: loader, filters: filters, all: true)
|
87
|
-
```
|
64
|
+
The second step is to create the `Worker` instance by providing the path to scan and the list of filters to apply.
|
88
65
|
|
89
|
-
####
|
90
|
-
|
91
|
-
The `Worker` constructor accepts a `slice` attribute to give you a chance to distribute loading:
|
66
|
+
#### Enumerator
|
67
|
+
The `call` method of the worker return a lazy enumerator with the filtered elements, sliced by the specified number (default to 1000):
|
92
68
|
```ruby
|
93
|
-
worker = FileScanner::Worker.new(
|
94
|
-
worker.call
|
95
|
-
|
96
|
-
end
|
69
|
+
worker = FileScanner::Worker.new(path: "~/Downloads", filters: filters, slice: 35)
|
70
|
+
p worker.call
|
71
|
+
=> #<Enumerator::Lazy: ...
|
97
72
|
```
|
98
73
|
|
99
|
-
####
|
100
|
-
|
74
|
+
#### Block
|
75
|
+
To perform actions on each of the sliced paths just pass a block:
|
101
76
|
```ruby
|
102
|
-
worker = FileScanner::Worker.new(loader: loader, slice: 1000, limit: 6000)
|
103
77
|
worker.call do |slice|
|
104
|
-
#
|
78
|
+
# perform actions on a slice of at max 35 elements
|
105
79
|
end
|
106
80
|
```
|
107
81
|
|
108
|
-
####
|
109
|
-
|
82
|
+
#### Mode
|
83
|
+
By default the worker will select paths by applying any of the matching filters: this is it, it suffice just one of the specified filters to be true to grab the path.
|
84
|
+
In case you want restrict paths selection by all matching filters, just specify it:
|
110
85
|
```ruby
|
111
|
-
|
112
|
-
count = slices.flatten.size
|
86
|
+
worker = FileScanner::Worker.new(loader: loader, filters: filters, all: true)
|
113
87
|
```
|
114
88
|
|
115
89
|
#### Logger
|
data/file_scanner.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.version = FileScanner::VERSION
|
9
9
|
s.authors = ["costajob"]
|
10
10
|
s.email = ["costajob@gmail.com"]
|
11
|
-
s.summary = "A library to collect a
|
11
|
+
s.summary = "A library to collect a list of files by path and a set of filters, sliced by the specified value"
|
12
12
|
s.homepage = "https://github.com/costajob/file_scanner"
|
13
13
|
s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec|test|s|features)/}) }
|
14
14
|
s.require_paths = ["lib"]
|
data/lib/file_scanner/version.rb
CHANGED
data/lib/file_scanner/worker.rb
CHANGED
@@ -1,16 +1,24 @@
|
|
1
|
+
require "find"
|
1
2
|
require "logger"
|
2
3
|
require "file_scanner/filters"
|
3
|
-
require "file_scanner/loader"
|
4
4
|
|
5
5
|
module FileScanner
|
6
6
|
class Worker
|
7
|
-
|
7
|
+
SLICE = 1000
|
8
|
+
ALL = :all?
|
9
|
+
ANY = :any?
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
+
attr_reader :filters
|
12
|
+
|
13
|
+
def initialize(path:,
|
14
|
+
filters: Filters::defaults,
|
15
|
+
slice: SLICE,
|
16
|
+
all: false,
|
17
|
+
logger: Logger.new(nil))
|
18
|
+
@path = File.expand_path(path)
|
11
19
|
@filters = filters
|
12
|
-
@mode = mode(all)
|
13
20
|
@slice = slice.to_i
|
21
|
+
@mode = mode(all)
|
14
22
|
@logger = logger
|
15
23
|
end
|
16
24
|
|
@@ -24,13 +32,8 @@ module FileScanner
|
|
24
32
|
raise e
|
25
33
|
end
|
26
34
|
|
27
|
-
private def filtered
|
28
|
-
@loader.call.select { |file| filter(file) }
|
29
|
-
end
|
30
|
-
|
31
35
|
private def mode(all)
|
32
|
-
|
33
|
-
:any?
|
36
|
+
all ? ALL : ANY
|
34
37
|
end
|
35
38
|
|
36
39
|
private def filter(file)
|
@@ -40,8 +43,15 @@ module FileScanner
|
|
40
43
|
end
|
41
44
|
end
|
42
45
|
|
46
|
+
private def paths
|
47
|
+
Find.find(@path)
|
48
|
+
end
|
49
|
+
|
50
|
+
private def filtered
|
51
|
+
paths.lazy.select { |file| filter(file) }
|
52
|
+
end
|
53
|
+
|
43
54
|
private def slices
|
44
|
-
return [filtered] if @slice.zero?
|
45
55
|
filtered.each_slice(@slice)
|
46
56
|
end
|
47
57
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: file_scanner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- costajob
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -83,7 +83,6 @@ files:
|
|
83
83
|
- file_scanner.gemspec
|
84
84
|
- lib/file_scanner.rb
|
85
85
|
- lib/file_scanner/filters.rb
|
86
|
-
- lib/file_scanner/loader.rb
|
87
86
|
- lib/file_scanner/refinements.rb
|
88
87
|
- lib/file_scanner/version.rb
|
89
88
|
- lib/file_scanner/worker.rb
|
@@ -110,6 +109,6 @@ rubyforge_project:
|
|
110
109
|
rubygems_version: 2.6.8
|
111
110
|
signing_key:
|
112
111
|
specification_version: 4
|
113
|
-
summary: A library to collect a
|
114
|
-
|
112
|
+
summary: A library to collect a list of files by path and a set of filters, sliced
|
113
|
+
by the specified value
|
115
114
|
test_files: []
|
data/lib/file_scanner/loader.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
module FileScanner
|
2
|
-
class Loader
|
3
|
-
def initialize(path:, extensions: [], limit: -1)
|
4
|
-
@path = File.expand_path(path)
|
5
|
-
@extensions = extensions
|
6
|
-
@limit = limit.to_i
|
7
|
-
end
|
8
|
-
|
9
|
-
def call
|
10
|
-
paths = Dir.glob(files_path)
|
11
|
-
return paths if @limit <= 0
|
12
|
-
paths.sample(@limit)
|
13
|
-
end
|
14
|
-
|
15
|
-
private def files_path
|
16
|
-
File.join(@path, "**", extensions_path)
|
17
|
-
end
|
18
|
-
|
19
|
-
private def extensions_path
|
20
|
-
return "*" if @extensions.empty?
|
21
|
-
"*.{#{@extensions.join(",")}}"
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|