file-visitor 0.1.1 → 0.2.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: a90c3de765cb74ab492da1d8ded9a60843f268c2
4
- data.tar.gz: 6f0df4e777c51f4b87729a4d7038dd039bead761
3
+ metadata.gz: f1db8062d46a31040d1c227b5bc50e3d374d6868
4
+ data.tar.gz: 4b097533b3731bf8fca9bda7d7af37def9008902
5
5
  SHA512:
6
- metadata.gz: 02b9f326d9c5d563836e9bf8609b9a6c0d106a81f65c58e60f667940e70b75eb137291b168354c4421bf92718aa0b4abb2dbe80370af9681d3a237f9b1b1e34a
7
- data.tar.gz: 9f1ba5f1bf647649779d154a3d5525c0969d32d60225deb93cafaba10f106b213c4ce76b35d38dd7aeb64df2af301f4c2c043cb1161e069aa92aedd9084fb53d
6
+ metadata.gz: b0430857539fc390d49716f24ff39bafbd9454244a096d42e07641c7f80b8240482710d0890c1743024baea33c9f1ae0bf39bed73f0706640ed3b28d100c8cbd
7
+ data.tar.gz: ddf0ab7b178eade7a2dd751dbe9b37235e067433b949e5b757f0a460425d71ce539506b7a5e41ff9097715cec065f2bf057242acef18a49e12d3e4f736977b09
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ *.swp
1
2
  /.bundle/
2
3
  /.yardoc
3
4
  /Gemfile.lock
data/README.md CHANGED
@@ -16,7 +16,7 @@ But in the following cases, it is not enough.
16
16
  * need to write the conditions of the file path to collect by ruby.
17
17
  * need to test and reuse conditions.
18
18
 
19
- file-visitor way to collect files
19
+ Collecting files with file-visitor:
20
20
 
21
21
  require 'file/visitor'
22
22
 
@@ -28,6 +28,9 @@ file-visitor way to collect files
28
28
  # and last modified is more than 30 days ago
29
29
  visitor.add_filter(:mtime, :passed, 30, :days)
30
30
 
31
+ # set sort order (:asc or :desc)
32
+ visitor.set_direction(:desc)
33
+
31
34
  # remove all the matched files
32
35
  visitor.visit(root_dir) do |path|
33
36
  FileUtils.rm(path)
@@ -1,4 +1,6 @@
1
1
 
2
+ require 'set'
3
+
2
4
  require 'file/visitor/filter'
3
5
  require 'file/visitor/filter_dispatcher'
4
6
  require 'file/visitor/filter/proc'
@@ -6,15 +8,29 @@ require 'file/visitor/filter/proc'
6
8
  class File
7
9
  class Visitor
8
10
 
9
- attr_reader :filters
11
+ attr_reader :filters, :direction
10
12
  attr_accessor :visit_dot_dir
11
13
 
12
14
  FILTER_NS_BASE = File::Visitor::Filter
13
15
 
16
+ DIRECTION = Set.new [:asc, :desc]
17
+
14
18
  def initialize
15
19
  @filters = []
16
20
 
17
21
  @visit_dot_dir = false
22
+ @direction = :asc
23
+ end
24
+
25
+ def set_direction(dir)
26
+ if dir.nil?
27
+ raise ArgumentError, "direction is nil"
28
+ end
29
+ if !DIRECTION.include?(dir.to_sym)
30
+ raise ArgumentError,
31
+ "invalid direction: " + dir.to_s
32
+ end
33
+ @direction = dir
18
34
  end
19
35
 
20
36
  def visit(dir, &handler)
@@ -108,7 +124,14 @@ class File
108
124
  def visit_with_mode(dir, mode, &handler)
109
125
  assert_directory(dir)
110
126
 
111
- Dir.entries(dir).each do |entry|
127
+ entries = Dir.entries(dir)
128
+ .sort_by { |filename| filename }
129
+
130
+ if @direction == :desc
131
+ entries.reverse!
132
+ end
133
+
134
+ entries.each do |entry|
112
135
  next if (dot_dir?(entry) && !@visit_dot_dir)
113
136
 
114
137
  abs_path = File.join(dir, entry)
@@ -1,5 +1,5 @@
1
1
  class File
2
2
  class Visitor
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file-visitor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - bonar
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-31 00:00:00.000000000 Z
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler