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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e94d61fa43f097a1912390317cde01b2119b997
4
- data.tar.gz: 12a767ee4da87adf667e5cdee448e3f473a8a233
3
+ metadata.gz: 28d079722f80fd8d08f242cea61b0994d6b2bb35
4
+ data.tar.gz: 0713446d2aa7ce967b7005aa96dcead35c3ea4e7
5
5
  SHA512:
6
- metadata.gz: 526f96d30f7e39d148eaed24e90266ba44edfb1c509618691be479e66fda569e3d001903f9e05234fea0e60a4a7749b0d1bc0534fe125be9a1621357d9126388
7
- data.tar.gz: 8c86dfc6a68ddd431a4656f7c833759f46717d89d8d89d967b0912de12505b562df93b3512474bbe111481c40b9eb810cf01e1f1d27b3966abb64a9617f5dc4f
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 second step is to provide the filters list to select file paths for which the `call` method is *truthy*.
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 creating `Proc` instances that satisfy the `callable` protocol:
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
- Now that you have all of the collaborators in place, you can create the `Worker` instance to performs actions on the filtered paths:
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
- #### Batches
90
- In case you are going to scan a large number of files, it is suggested to work in batches.
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(loader: loader, slice: 1000)
94
- worker.call do |slice|
95
- # perform action 1000 paths per time
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
- #### Limit
100
- In case you are going to apply some heavy filtering upon the selected files (i.e. reading the file in memory to get some creepy data), you can found helpful to limit the number of retuned paths before applying any filtering:
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
- # filters applied on a maximum of 6000 paths, working a slice of 1000 files per time
78
+ # perform actions on a slice of at max 35 elements
105
79
  end
106
80
  ```
107
81
 
108
- #### Enumerator
109
- In case you want access the sliced enumerator directly, just do not pass a block to the method:
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
- slices = worker.call
112
- count = slices.flatten.size
86
+ worker = FileScanner::Worker.new(loader: loader, filters: filters, all: true)
113
87
  ```
114
88
 
115
89
  #### Logger
@@ -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 set of file paths starting by a wildcard rule, filter them by any/all default/custom filters and apply a set of actions via a block call."
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"]
@@ -1,3 +1,3 @@
1
1
  module FileScanner
2
- VERSION = "2.3.0"
2
+ VERSION = "3.0.1"
3
3
  end
@@ -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
- attr_reader :loader, :filters
7
+ SLICE = 1000
8
+ ALL = :all?
9
+ ANY = :any?
8
10
 
9
- def initialize(loader:, filters: Filters::defaults, all: false, slice: nil, logger: Logger.new(nil))
10
- @loader = loader
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
- return :all? if all
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: 2.3.0
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-21 00:00:00.000000000 Z
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 set of file paths starting by a wildcard rule, filter
114
- them by any/all default/custom filters and apply a set of actions via a block call.
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: []
@@ -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