file_scanner 2.0.4 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b8d05591c33d88bc64ed6029b3dba902b3b61bdd
4
- data.tar.gz: d635fe64822621e9a071759e7f1b7a254240555b
3
+ metadata.gz: b97bb23d5031571dadb1bbc7d3230fc6d05a1973
4
+ data.tar.gz: 0d042f4d231a4aa1e8f462cdc7cbd3c523bc313f
5
5
  SHA512:
6
- metadata.gz: 7d1c93540a329ab31185695f9632e398f05420da233898119586c908da20771f351d5f431da61b4e6fc13468d5db22f724c092a4407c8639a0bc205f738e0217
7
- data.tar.gz: e8b662063dc934a7b699f875f33f251901d62bdbb38ae44453c471fb5f29c62f3d1545fd7c659c8d1728e857b194bff63b25c61389b13d334c946a0b58982560
6
+ metadata.gz: a3fc550fd7fcf6f346b8fec40942dca5049f1fa1b00a24e696dcf38bffff3d7960cd5f17594482acb61ea932e3caae24c89b0c9eb54d3f82cac2612b75b0d2f2
7
+ data.tar.gz: ba4bc0f86eee6d8997e053ca0d00f5d58fa93f76a4d5f043728924630f53a8ea75261bc9ce696d4f2bef441e649a5e9ac98211ef9cd8e374a2f434d499f1fc5d
data/README.md CHANGED
@@ -9,7 +9,9 @@
9
9
  * [Defaults](#defaults)
10
10
  * [Custom](#custom)
11
11
  * [Worker](#worker)
12
+ * [Mode](#mode)
12
13
  * [Batches](#batches)
14
+ * [Limit](#limit)
13
15
  * [Enumerator](#enumerator)
14
16
  * [Logger](#logger)
15
17
  * [Factory](#factory)
@@ -48,7 +50,6 @@ loader = FileScanner::Loader.new(path: ENV["HOME"], extensions: %w[html txt])
48
50
 
49
51
  ### Filters
50
52
  The second step is to provide the filters list to select file paths for which the `call` method is *truthy*.
51
- Selection is done with the `any?` predicate, so also one matching filter will do the selection.
52
53
 
53
54
  #### Defaults
54
55
  If you specify no filters the default ones are loaded, selecting files by:
@@ -79,6 +80,13 @@ worker.call do |paths|
79
80
  end
80
81
  ```
81
82
 
83
+ ### Mode
84
+ 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.
85
+ In case you want restrict paths selection by all matching filters, just specify it:
86
+ ```ruby
87
+ worker = FileScanner::Worker.new(loader: loader, filters: filters, all: true)
88
+ ```
89
+
82
90
  #### Batches
83
91
  In case you are going to scan a large number of files, it is suggested to work in batches.
84
92
  The `Worker` constructor accepts a `slice` attribute to give you a chance to distribute loading:
@@ -89,6 +97,15 @@ worker.call do |slice|
89
97
  end
90
98
  ```
91
99
 
100
+ #### Limit
101
+ In case you are still concerned about the selected paths number, you can limit it before applying the slicing (if any):
102
+ ```ruby
103
+ worker = FileScanner::Worker.new(loader: loader, slice: 1000, limit: 7500)
104
+ worker.call do |slice|
105
+ # perform action 1000 paths per time till reaching the specified limit
106
+ end
107
+ ```
108
+
92
109
  #### Enumerator
93
110
  In case you want access the sliced enumerator directly, just do not pass a block to the method:
94
111
  ```ruby
@@ -119,8 +136,8 @@ end
119
136
  #### Factory
120
137
  You can create loader and worker instances at once by using the available factory:
121
138
  ```ruby
122
- worker = FileScanner::Worker.factory(path: ENV["HOME"], extensions: %w[html txt], filters: filters, logger: my_logger, slice: 1000)
139
+ worker = FileScanner::Worker.factory(path: ENV["HOME"], extensions: %w[html txt], filters: filters, all: true, slice: 1000, limit: 7500, logger: my_logger)
123
140
  worker.call do |slice, logger|
124
- # perform action 1000 paths per time
141
+ # perform action 1000 paths per time till reaching the specified limit
125
142
  end
126
143
  ```
@@ -1,3 +1,3 @@
1
1
  module FileScanner
2
- VERSION = "2.0.4"
2
+ VERSION = "2.1.0"
3
3
  end
@@ -4,23 +4,19 @@ require "file_scanner/loader"
4
4
 
5
5
  module FileScanner
6
6
  class Worker
7
- def self.default_logger
8
- Logger.new(nil).tap do |logger|
9
- logger.level = Logger::ERROR
10
- end
11
- end
12
-
13
- def self.factory(path:, extensions: [], filters: [], logger: default_logger, slice: nil)
7
+ def self.factory(path:, extensions: [], filters: [], all: false, slice: nil, limit: -1, logger: Logger.new(nil))
14
8
  loader = Loader.new(path: path, extensions: extensions)
15
- new(loader: loader, filters: filters, logger: logger, slice: slice)
9
+ new(loader: loader, filters: filters, slice: slice, all: all, limit: limit, logger: logger)
16
10
  end
17
11
 
18
12
  attr_reader :loader, :filters
19
13
 
20
- def initialize(loader:, filters: Filters::defaults, logger: self.class.default_logger, slice: nil)
14
+ def initialize(loader:, filters: Filters::defaults, all: false, slice: nil, limit: -1, logger: Logger.new(nil))
21
15
  @loader = loader
22
16
  @filters = filters
17
+ @all = !!all
23
18
  @slice = slice.to_i
19
+ @limit = limit.to_i
24
20
  @logger = logger
25
21
  end
26
22
 
@@ -34,21 +30,36 @@ module FileScanner
34
30
  raise e
35
31
  end
36
32
 
33
+ private def fetch_mode(mode)
34
+ return :any? unless @filters.respond_to?(mode)
35
+ mode
36
+ end
37
+
37
38
  private def files
38
39
  paths = @loader.call
39
40
  paths.select! { |file| filter(file) } || paths
40
41
  end
41
42
 
43
+ private def limit
44
+ return files if @limit <= 0
45
+ files.first(@limit)
46
+ end
47
+
48
+ private def mode
49
+ return :all? if @all
50
+ :any?
51
+ end
52
+
42
53
  private def filter(file)
43
- @filters.any? do |filter|
44
- @logger.debug { "applying \e[33m#{filter}\e[0m to #{File.basename(file)}" }
54
+ @filters.send(mode) do |filter|
55
+ @logger.debug { "selecting by \e[33m#{mode}\e[0m with filter \e[33m#{filter}\e[0m on #{File.basename(file)}" }
45
56
  filter.call(file)
46
57
  end
47
58
  end
48
59
 
49
60
  private def slices
50
- return [files] if @slice.zero?
51
- files.each_slice(@slice)
61
+ return [limit] if @slice.zero?
62
+ limit.each_slice(@slice)
52
63
  end
53
64
  end
54
65
  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: 2.0.4
4
+ version: 2.1.0
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 00:00:00.000000000 Z
11
+ date: 2017-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler