file_scanner 1.0.5 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3b3798bb1314d4039d0508aaf7e3a067dd35985
4
- data.tar.gz: 82177994587d8288ba5650e9fdfd2bd72dfa46fa
3
+ metadata.gz: 33dbedb22e72fe24ab4758275639094feab57a47
4
+ data.tar.gz: 770ef5bf18c0c94569051ab6bbefef12b0878464
5
5
  SHA512:
6
- metadata.gz: 046f99929f5c43e475b5e4d8c6815712b112b3f0c873ef36257112fb17a06f37808e6985c1055ad35401543dc1c8cf50f0934211c20bee891beeba29051328b5
7
- data.tar.gz: 734ddfae382c25e98e6bc2ff5bc9996c4f2ce083754a608ed93ae7ebed86aa7c7a73bbeb9fff4eb624c4d2306a621ef56b738370d5ee1aee5d22244946359c67
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 files for which the `call` method is truthy.
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 onee will select files by:
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 *smaller than 100 bytes*
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 configure default behaviour by passing different arguments:
54
+ You can update default behaviours by passing custom arguments:
53
55
  ```ruby
54
- accessed_a_week_ago = FileScanner::Filters::LastAccess.new(Time.now-7*24*3600)
55
- one_to_two_mega = FileScanner::Filters::SizeRange.new(min: 1024**2, max: 2*1024**2)
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 << accessed_a_week_ago
59
- filters << one_to_two_mega
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 just relying on `Proc` instances that satisfy the `callable` protocol:
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 accept an array of paths as an argument:
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
@@ -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: 100, max: Float::INFINITY)
41
+ def initialize(min: 0, max: 5*1024)
23
42
  @range = min..max
24
43
  end
25
44
 
@@ -1,7 +1,7 @@
1
1
  module FileScanner
2
2
  class Loader
3
3
  def initialize(path:, extensions: [])
4
- @path = path
4
+ @path = File.expand_path(path)
5
5
  @extensions = extensions
6
6
  end
7
7
 
@@ -0,0 +1,10 @@
1
+ module FileScanner
2
+ module Refinements
3
+ refine(String) do
4
+ def matches?(re)
5
+ return match?(re) if defined?("".match?)
6
+ !!match(re)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module FileScanner
2
- VERSION = "1.0.5"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -4,7 +4,15 @@ module FileScanner
4
4
  class Worker
5
5
  attr_reader :filters, :policies
6
6
 
7
- def initialize(loader:, filters: Filters::defaults, policies: [], logger: Logger.new(nil), slice: nil)
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[1m#{policy}\e[0m to \e[1m#{slice.size}\e[0m files" }
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
- @files ||= Array(@loader.call).select do |f|
30
- @filters.all? do |filter|
31
- @logger.info { "applying \e[1m#{filter.class}\w[0m to \e[1m#{File.basename(f)}\e[0m" }
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.5
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