dir_processor 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/dir_processor.gemspec +1 -0
- data/lib/dir_processor/version.rb +1 -1
- data/lib/dir_processor.rb +68 -46
- data/spec/example_spec.rb +27 -19
- data/spec/support/file_utils.rb +6 -1
- data/spec/support/spec_helper.rb +2 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a1ba8e96fb0e3e65cefc5ad96d82f63de75f465
|
4
|
+
data.tar.gz: 27118b88368131720966b7b612e4461a15eaeb45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e57ab852ded737a40e7478376d9fe3647ba17ea3ec83af05d0230e91783c42ecfb8102415d2486d5cf49c2ba773d380705fe187fd3791cc50ddfab0b94f90a7
|
7
|
+
data.tar.gz: affd001b0e212c03865455aa843cb3b140f144cd25b85900744474e7ffcf19281e7d06de15236f15b873268487564e3a2e8949e6948dd79f2ab06148e9802dd9
|
data/dir_processor.gemspec
CHANGED
data/lib/dir_processor.rb
CHANGED
@@ -1,10 +1,53 @@
|
|
1
1
|
require "dir_processor/version"
|
2
2
|
require 'def_dsl'
|
3
|
+
# require 'pathname'
|
3
4
|
|
4
5
|
def DirProcessor &block
|
5
6
|
DirProcessor.new &block
|
6
7
|
end
|
7
8
|
|
9
|
+
# my try to dry
|
10
|
+
module FeedMethod
|
11
|
+
def feed dir
|
12
|
+
return unless accept? dir
|
13
|
+
|
14
|
+
# Copy/Paste:
|
15
|
+
crap = proc { |x| x == '.' || x == '..' }
|
16
|
+
|
17
|
+
Dir.chdir dir do
|
18
|
+
this_dir = dir
|
19
|
+
dir = '.'
|
20
|
+
|
21
|
+
all = Dir.entries(dir).reject(&crap).group_by { |x| is_dir?(x) ? :dirs : :files } #long_name
|
22
|
+
all_files = all[:files]
|
23
|
+
all_dirs = all[:dirs]
|
24
|
+
|
25
|
+
so2(:first).each do |processor|
|
26
|
+
processor.feed this_dir
|
27
|
+
end
|
28
|
+
so2(:file).each do |processor|
|
29
|
+
(all[:files] || []).each { |file| processor.feed file } # long_name(dir,file) }
|
30
|
+
end
|
31
|
+
so2(:dir).each do |processor|
|
32
|
+
# require 'pry'; binding.pry
|
33
|
+
(all[:dirs] || []).each do |f|
|
34
|
+
processor.feed f #long_name(dir,f)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def long_name dir,file
|
43
|
+
File.join(dir,file).sub /^\.\//, ''
|
44
|
+
end
|
45
|
+
def is_dir? file
|
46
|
+
raise "file disappeared or did'nt exist..." unless File.exist? file
|
47
|
+
File.directory? file
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
8
51
|
module DirProcessor
|
9
52
|
def new &block
|
10
53
|
DirProcessor.new &block
|
@@ -56,30 +99,7 @@ module DirProcessor
|
|
56
99
|
def feed dir; @block.call dir end
|
57
100
|
end
|
58
101
|
|
59
|
-
|
60
|
-
return unless accept? dir
|
61
|
-
|
62
|
-
# Copy/Paste:
|
63
|
-
crap = proc { |x| x == '.' || x == '..' }
|
64
|
-
all = Dir.entries(dir).reject(&crap).group_by { |x| File.directory?(x) ? :dirs : :files }
|
65
|
-
all_files = all[:files]
|
66
|
-
all_dirs = all[:dirs]
|
67
|
-
|
68
|
-
so2(:first).each do |processor|
|
69
|
-
processor.feed dir
|
70
|
-
end
|
71
|
-
so2(:file).each do |processor|
|
72
|
-
(all[:files] || []).each { |file| processor.feed long_name(dir,file) }
|
73
|
-
end
|
74
|
-
so2(:dir).each do |processor|
|
75
|
-
(all[:dirs] || []).each { |f| processor.feed long_name(dir,f) }
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
private
|
80
|
-
def long_name dir,file
|
81
|
-
File.join(dir,file).sub /^\.\//, ''
|
82
|
-
end
|
102
|
+
include FeedMethod
|
83
103
|
end
|
84
104
|
extend DefDSL
|
85
105
|
def_dsl ADir, AFile
|
@@ -93,28 +113,30 @@ module DirProcessor
|
|
93
113
|
instance_eval &block #yield
|
94
114
|
end
|
95
115
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
116
|
+
include FeedMethod
|
117
|
+
def accept?(*); true end
|
118
|
+
|
119
|
+
# def feed dir
|
120
|
+
# # return unless accept? dir
|
121
|
+
|
122
|
+
# # Copy/Paste:
|
123
|
+
# crap = proc { |x| x == '.' || x == '..' }
|
124
|
+
# all = Dir.entries(dir).reject(&crap).group_by { |x| File.directory?(x) ? :dirs : :files }
|
125
|
+
# all_files = all[:files]
|
126
|
+
# all_dirs = all[:dirs]
|
127
|
+
|
128
|
+
# so2(:file).each do |processor|
|
129
|
+
# (all[:files] || []).each { |file| processor.feed long_name(dir,file) }
|
130
|
+
# end
|
131
|
+
# so2(:dir).each do |processor|
|
132
|
+
# (all[:dirs] || []).each { |f| processor.feed long_name(dir,f) }
|
133
|
+
# end
|
134
|
+
# end
|
135
|
+
|
136
|
+
# private
|
137
|
+
# def long_name dir,file
|
138
|
+
# File.join(dir,file).sub /^\.\//, ''
|
139
|
+
# end
|
118
140
|
|
119
141
|
# def feed dir
|
120
142
|
# # @so = @dir.feed(dir).send(:so)
|
data/spec/example_spec.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# do we need variant for lazy stuff??? (to get rid of dir~first do)
|
2
|
-
#
|
2
|
+
# one_dir / dir '', 1..2 do
|
3
3
|
# (...)
|
4
4
|
# so1,so2: returns just last and array
|
5
5
|
# file,files: yields one by one and all as array
|
@@ -23,7 +23,7 @@ describe 'DirProcessor method' do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'is configured by block' do
|
26
|
-
DirProcessor(){}.send(:so).should_not == DirProcessor(){ dir('
|
26
|
+
DirProcessor(){}.send(:so).should_not == DirProcessor(){ dir('*'){} }.send(:so)
|
27
27
|
end
|
28
28
|
|
29
29
|
example 'main' do
|
@@ -34,7 +34,9 @@ describe 'DirProcessor method' do
|
|
34
34
|
end
|
35
35
|
dir 'mp3s' do
|
36
36
|
first do |dir|
|
37
|
-
|
37
|
+
# already chdir-ed in the dir
|
38
|
+
File.basename(File.expand_path('.')).should == dir
|
39
|
+
# Dir.exist?(dir).should == true
|
38
40
|
(data[:dirs] ||= []) << dir
|
39
41
|
end
|
40
42
|
|
@@ -45,28 +47,31 @@ describe 'DirProcessor method' do
|
|
45
47
|
end
|
46
48
|
dir 'empty' do
|
47
49
|
first do |dir|
|
48
|
-
|
50
|
+
# already chdir-ed in the dir
|
51
|
+
File.basename(File.expand_path('.')).should == dir
|
52
|
+
# Dir.exist?(dir).should == true
|
49
53
|
(data[:dirs] ||= []) << dir
|
50
54
|
end
|
51
55
|
end
|
52
56
|
end
|
53
57
|
|
54
58
|
temp_dir do
|
55
|
-
mkfile 'about.png'
|
56
|
-
mkfile 'any.other'
|
59
|
+
mkfile '1/about.png'
|
60
|
+
mkfile '1/any.other'
|
57
61
|
|
58
|
-
mkfile 'mp3s/one.mp3'
|
59
|
-
mkfile 'mp3s/two.mp3'
|
60
|
-
mkfile 'mp3s/three.mp3'
|
62
|
+
mkfile '1/mp3s/one.mp3'
|
63
|
+
mkfile '1/mp3s/two.mp3'
|
64
|
+
mkfile '1/mp3s/three.mp3'
|
61
65
|
|
62
|
-
|
63
|
-
|
64
|
-
this.feed '
|
66
|
+
mkdir '1/empty'
|
67
|
+
|
68
|
+
this.feed '1'
|
65
69
|
end
|
66
70
|
|
67
71
|
data.keys.should =~ [:about, :dirs, :mp3s]
|
68
72
|
data[:about].should == 'about.png'
|
69
|
-
data[:mp3s].should =~ %w[ mp3s/one.mp3 mp3s/two.mp3 mp3s/three.mp3 ]
|
73
|
+
# data[:mp3s].should =~ %w[ mp3s/one.mp3 mp3s/two.mp3 mp3s/three.mp3 ]
|
74
|
+
data[:mp3s].should =~ %w[ one.mp3 two.mp3 three.mp3 ]
|
70
75
|
data[:dirs].should =~ %w[ empty mp3s ]
|
71
76
|
end
|
72
77
|
|
@@ -74,21 +79,24 @@ describe 'DirProcessor method' do
|
|
74
79
|
data = {}
|
75
80
|
|
76
81
|
this = DirProcessor do
|
77
|
-
dir '
|
82
|
+
dir '*' do
|
78
83
|
first do |dir|
|
79
|
-
|
84
|
+
# already chdir-ed in the dir
|
85
|
+
File.basename(File.expand_path('.')).should == dir
|
86
|
+
# Dir.exist?(dir).should == true
|
80
87
|
(data[:dirs] ||= []) << dir
|
81
88
|
end
|
82
89
|
end
|
83
90
|
end
|
84
91
|
|
85
92
|
temp_dir do
|
86
|
-
|
87
|
-
|
93
|
+
mkdir '1/empty'
|
94
|
+
Dir.exist?('1/empty').should == true
|
95
|
+
this.feed '1'
|
88
96
|
end
|
89
97
|
|
90
|
-
data.keys.should
|
91
|
-
data[:dirs].should
|
98
|
+
data.keys.should == [:dirs]
|
99
|
+
data[:dirs].should == %w[ empty ]
|
92
100
|
end
|
93
101
|
|
94
102
|
end
|
data/spec/support/file_utils.rb
CHANGED
@@ -2,6 +2,7 @@ require 'fileutils'
|
|
2
2
|
|
3
3
|
# runs block in temp dir, purge it after all
|
4
4
|
def temp_dir temp='tmp/trash'
|
5
|
+
FileUtils.rm_rf temp
|
5
6
|
FileUtils.mkpath temp
|
6
7
|
Dir.chdir(temp) { yield }
|
7
8
|
FileUtils.rm_rf temp
|
@@ -9,6 +10,10 @@ end
|
|
9
10
|
|
10
11
|
# mkpath + touch
|
11
12
|
def mkfile file
|
12
|
-
|
13
|
+
mkdir File.split(file)[0]
|
13
14
|
FileUtils.touch file
|
15
|
+
end
|
16
|
+
|
17
|
+
def mkdir dir
|
18
|
+
FileUtils.mkpath dir
|
14
19
|
end
|
data/spec/support/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dir_processor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander K
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-04-
|
11
|
+
date: 2013-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: def_dsl
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|