file_scanner 1.0.5 → 1.1.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 +16 -11
- data/lib/file_scanner/filters.rb +20 -1
- data/lib/file_scanner/loader.rb +1 -1
- data/lib/file_scanner/refinements.rb +10 -0
- data/lib/file_scanner/version.rb +1 -1
- data/lib/file_scanner/worker.rb +13 -5
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33dbedb22e72fe24ab4758275639094feab57a47
|
4
|
+
data.tar.gz: 770ef5bf18c0c94569051ab6bbefef12b0878464
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5fdf0d4b098155ce1d5218ff31aeac4fac0a6e0313e9206279a099438cc3fc3d8be12d2d82487640869f377eec2aa2ed295c724d44c35ef90402f2e0ca846f86
|
7
|
+
data.tar.gz: 1e2c0cac747c3c68868991628ccf3a3c51e75604d95bcb79c09c90a11dd5e1180d4cbfcfbc6322aaf984bc8370961515667075614c4edc538e701b464c9a283b
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
* [Worker](#worker)
|
11
11
|
|
12
12
|
## Scope
|
13
|
-
This gem is aimed to collect a set of file paths starting by a wildcard rule, filter them by default/custom filters (access time, size range) and apply a set of custom policies to them.
|
13
|
+
This gem is aimed to collect a set of file paths starting by a wildcard rule, filter them by any default/custom filters (access time, size range) and apply a set of custom policies to them.
|
14
14
|
|
15
15
|
## Motivation
|
16
16
|
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).
|
@@ -42,32 +42,36 @@ loader = FileScanner::Loader.new(path: ENV["HOME"], extensions: %w[html txt])
|
|
42
42
|
```
|
43
43
|
|
44
44
|
### Filters
|
45
|
-
The second step is to provide the filters list to select
|
45
|
+
The second step is to provide the filters list to select file paths for which the `call` method is truthy.
|
46
|
+
Selection is done with the `any?` predicate, so also one matching filter will select the path.
|
46
47
|
|
47
48
|
#### Default
|
48
|
-
If you specify no filters the existing
|
49
|
+
If you specify no filters the existing ones will select files by:
|
49
50
|
* checking if file is older than *30 days*
|
50
|
-
* checking if file size is *
|
51
|
+
* checking if file size is within *0KB and 5KB*
|
52
|
+
* checking if file *basename matches* the specified *regexp* (if any)
|
51
53
|
|
52
|
-
You can
|
54
|
+
You can update default behaviours by passing custom arguments:
|
53
55
|
```ruby
|
54
|
-
|
55
|
-
|
56
|
+
a_week_ago = FileScanner::Filters::LastAccess.new(Time.now-7*24*3600)
|
57
|
+
one_two_mb = FileScanner::Filters::SizeRange.new(min: 1024**2, max: 2*1024**2)
|
58
|
+
hidden = FileScanner::Filters::MatchingName.new(/^\./)
|
56
59
|
|
57
60
|
filters = []
|
58
|
-
filters <<
|
59
|
-
filters <<
|
61
|
+
filters << a_week_ago
|
62
|
+
filters << one_two_mb
|
63
|
+
filters << hidden
|
60
64
|
```
|
61
65
|
|
62
66
|
#### Custom
|
63
|
-
It is convenient to create custom filters by
|
67
|
+
It is convenient to create custom filters by creating `Proc` instances that satisfy the `callable` protocol:
|
64
68
|
```ruby
|
65
69
|
filters << ->(file) { File.directory?(file) }
|
66
70
|
```
|
67
71
|
|
68
72
|
### Policies
|
69
73
|
The third step is creating custom policies objects (no defaults exist) to be applied to the list of filtered paths.
|
70
|
-
Again, it suffice the policy responds to the `call` method and
|
74
|
+
Again, it suffice the policy responds to the `call` method and accepts an array of paths as unique argument:
|
71
75
|
```ruby
|
72
76
|
require "fileutils"
|
73
77
|
|
@@ -109,6 +113,7 @@ end
|
|
109
113
|
If you dare to trace what the worker is doing (including errors), you can specify a logger to the worker class:
|
110
114
|
```ruby
|
111
115
|
my_logger = Logger.new("my_file.log")
|
116
|
+
|
112
117
|
worker = FileScanner::Worker.new(loader: loader, logger: my_logger)
|
113
118
|
worker.call do |slice|
|
114
119
|
fail "Doh!" # will log error to my_file.log and re-raise exception
|
data/lib/file_scanner/filters.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
+
require "file_scanner/refinements"
|
2
|
+
|
1
3
|
module FileScanner
|
4
|
+
using Refinements
|
2
5
|
module Filters
|
3
6
|
def self.defaults
|
4
7
|
constants.map do |name|
|
@@ -18,8 +21,24 @@ module FileScanner
|
|
18
21
|
end
|
19
22
|
end
|
20
23
|
|
24
|
+
class MatchingName
|
25
|
+
def initialize(regexp = nil)
|
26
|
+
@regexp = compile(regexp)
|
27
|
+
end
|
28
|
+
|
29
|
+
def call(file)
|
30
|
+
return unless @regexp
|
31
|
+
File.basename(file).matches?(@regexp)
|
32
|
+
end
|
33
|
+
|
34
|
+
private def compile(regexp)
|
35
|
+
return unless regexp
|
36
|
+
Regexp.compile(regexp.to_s)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
21
40
|
class SizeRange
|
22
|
-
def initialize(min:
|
41
|
+
def initialize(min: 0, max: 5*1024)
|
23
42
|
@range = min..max
|
24
43
|
end
|
25
44
|
|
data/lib/file_scanner/loader.rb
CHANGED
data/lib/file_scanner/version.rb
CHANGED
data/lib/file_scanner/worker.rb
CHANGED
@@ -4,7 +4,15 @@ module FileScanner
|
|
4
4
|
class Worker
|
5
5
|
attr_reader :filters, :policies
|
6
6
|
|
7
|
-
def
|
7
|
+
def self.default_logger
|
8
|
+
Logger.new(nil).tap do |logger|
|
9
|
+
logger.level = Logger::ERROR
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(loader:,
|
14
|
+
filters: Filters::defaults, policies: [],
|
15
|
+
logger: self.class.default_logger, slice: nil)
|
8
16
|
@loader = loader
|
9
17
|
@filters = filters
|
10
18
|
@policies = policies
|
@@ -16,7 +24,7 @@ module FileScanner
|
|
16
24
|
slices.each do |slice|
|
17
25
|
yield(slice) if block_given? && policies.empty?
|
18
26
|
policies.each do |policy|
|
19
|
-
@logger.info { "applying \e[
|
27
|
+
@logger.info { "applying \e[33m#{policy}\e[0m to #{slice.size} files" }
|
20
28
|
policy.call(slice)
|
21
29
|
end
|
22
30
|
end
|
@@ -26,9 +34,9 @@ module FileScanner
|
|
26
34
|
end
|
27
35
|
|
28
36
|
private def files
|
29
|
-
|
30
|
-
@filters.
|
31
|
-
@logger.info { "applying \e[
|
37
|
+
Array(@loader.call).select do |f|
|
38
|
+
@filters.any? do |filter|
|
39
|
+
@logger.info { "applying \e[35m#{filter}\e[0m to #{File.basename(f)}" }
|
32
40
|
filter.call(f)
|
33
41
|
end
|
34
42
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: file_scanner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- costajob
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- lib/file_scanner.rb
|
71
71
|
- lib/file_scanner/filters.rb
|
72
72
|
- lib/file_scanner/loader.rb
|
73
|
+
- lib/file_scanner/refinements.rb
|
73
74
|
- lib/file_scanner/version.rb
|
74
75
|
- lib/file_scanner/worker.rb
|
75
76
|
homepage: https://github.com/costajob/file_scanner
|