gf-treevisitor 0.0.8 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/bin/tree.rb +9 -9
- data/lib/gf_utilities/kwartzhelper.rb +90 -90
- data/lib/gf_utilities/md5.rb +24 -24
- data/lib/gf_utilities/numeric.rb +17 -17
- data/lib/treevisitor/abs_node.rb +116 -116
- data/lib/treevisitor/cli/cli_tree.rb +2 -1
- data/lib/treevisitor/dir_processor.rb +40 -40
- data/lib/treevisitor/dir_tree_walker.rb +116 -116
- data/lib/treevisitor/leaf_node.rb +31 -27
- data/lib/treevisitor/tree_node.rb +134 -134
- data/lib/treevisitor/tree_node_visitor.rb +31 -31
- data/lib/treevisitor/visitors/block_tree_node_visitor.rb +4 -5
- data/lib/treevisitor/visitors/build_dir_tree_visitor.rb +44 -46
- data/lib/treevisitor/visitors/callback_tree_node_visitor.rb +6 -4
- data/lib/treevisitor/visitors/callback_tree_node_visitor2.rb +6 -4
- data/lib/treevisitor/visitors/clone_tree_node_visitor.rb +4 -5
- data/lib/treevisitor/visitors/depth_tree_node_visitor.rb +4 -4
- data/lib/treevisitor/visitors/flat_print_tree_node_visitors.rb +4 -5
- data/lib/treevisitor/visitors/print_dir_tree_visitor.rb +6 -6
- data/lib/treevisitor/visitors/print_tree_node_visitor.rb +6 -3
- data/lib/treevisitor/visitors/{print_node_visitor2.rb → print_tree_node_visitor2.rb} +9 -6
- data/lib/treevisitor.rb +3 -3
- data/test/gf_utilities/tc_md5.rb +14 -14
- data/test/treevisitor/cli/tc_cli_tree.rb +1 -1
- data/test/treevisitor/tc_dir_processor.rb +2 -2
- data/test/treevisitor/tc_tree_node.rb +3 -0
- metadata +5 -5
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.10
|
data/bin/tree.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
|
-
# Wrapper for the class CliTree
|
4
|
-
|
5
|
-
$TREEVISITOR_HOME = File.expand_path( File.join( File.dirname( __FILE__), ".." ) )
|
6
|
-
$:.unshift( File.join($TREEVISITOR_HOME, "lib" ) )
|
7
|
-
|
8
|
-
require "treevisitor/cli/cli_tree"
|
9
|
-
CliTree.run
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Wrapper for the class CliTree
|
4
|
+
|
5
|
+
$TREEVISITOR_HOME = File.expand_path( File.join( File.dirname( __FILE__), ".." ) )
|
6
|
+
$:.unshift( File.join($TREEVISITOR_HOME, "lib" ) )
|
7
|
+
|
8
|
+
require "treevisitor/cli/cli_tree"
|
9
|
+
exit CliTree.run
|
@@ -1,90 +1,90 @@
|
|
1
|
-
# ruby gems
|
2
|
-
#
|
3
|
-
# Check dependecies
|
4
|
-
require 'rubygems'
|
5
|
-
begin
|
6
|
-
require "kwartz"
|
7
|
-
require "kwartz/main"
|
8
|
-
rescue LoadError
|
9
|
-
abort 'kwartz gem required!!'
|
10
|
-
end
|
11
|
-
|
12
|
-
|
13
|
-
# ruby 1.9
|
14
|
-
# cambiare alla linea 73 kwartz/main.rb la linea seguente:
|
15
|
-
# return @_option_table.find { |row| row[1] == key }
|
16
|
-
# con
|
17
|
-
# return @_option_table.find { |row| row[1][0] == key[0] }
|
18
|
-
|
19
|
-
|
20
|
-
#
|
21
|
-
# kwartz utility
|
22
|
-
# template_dir: directory containing kwartz template
|
23
|
-
# template_include_dir: nil or a directory contained kwarz template referenced by template
|
24
|
-
# template_out_dir: where the output goes
|
25
|
-
#
|
26
|
-
def kwartz_compile( template_dir, template_include_dir, template_out_dir )
|
27
|
-
if not File.directory?(template_dir)
|
28
|
-
raise "template_dir: '#{template_dir}' is not a directory"
|
29
|
-
end
|
30
|
-
|
31
|
-
if not File.directory?(template_out_dir)
|
32
|
-
raise "template_out_dir: '#{template_out_dir}' is not a directory"
|
33
|
-
end
|
34
|
-
|
35
|
-
re_templatefile = /^[^\.](.+).html$/
|
36
|
-
|
37
|
-
maxtime_incs = Time.local( 1980 )
|
38
|
-
incs = []
|
39
|
-
if template_include_dir
|
40
|
-
if not File.directory?( template_include_dir )
|
41
|
-
raise "include_dir: '#{template_include_dir}' is not a directory"
|
42
|
-
end
|
43
|
-
incs = Dir.new( template_include_dir ).find_all { |f| f =~ re_templatefile }
|
44
|
-
incs = incs.map{ |f| File.join(template_include_dir, f) }
|
45
|
-
maxtime_incs = incs.inject(maxtime_incs) {|t,f|
|
46
|
-
t1 = File.mtime( f )
|
47
|
-
if t1 > t then t1 else t end
|
48
|
-
}
|
49
|
-
incs = incs.join(",")
|
50
|
-
end
|
51
|
-
|
52
|
-
Dir.foreach( template_dir ) { |f|
|
53
|
-
next unless f =~ re_templatefile
|
54
|
-
|
55
|
-
inpath = File.join(template_dir, f)
|
56
|
-
inpath_time = File.mtime( inpath )
|
57
|
-
|
58
|
-
plogicpath = template_dir + File::Separator + f.sub( /.html$/, ".plogic" )
|
59
|
-
plogicpath_time = Time.local( 1980 )
|
60
|
-
if File.exists?( plogicpath )
|
61
|
-
plogicpath_time = File.mtime( plogicpath )
|
62
|
-
end
|
63
|
-
|
64
|
-
outpath = File.join( template_out_dir, f.sub( /.html$/, ".rb" ) )
|
65
|
-
outpath_time = Time.local( 1980 )
|
66
|
-
if File.exist?( outpath )
|
67
|
-
outpath_time = File.mtime( outpath )
|
68
|
-
end
|
69
|
-
|
70
|
-
argv = %w[--delspan -a defun -l ruby]
|
71
|
-
if incs.length > 0
|
72
|
-
argv << "-i"
|
73
|
-
argv.push( incs )
|
74
|
-
end
|
75
|
-
|
76
|
-
if File.exist?( plogicpath )
|
77
|
-
argv.push( "-p", plogicpath )
|
78
|
-
end
|
79
|
-
argv.push( inpath )
|
80
|
-
|
81
|
-
if outpath_time < maxtime_incs or
|
82
|
-
outpath_time < inpath_time or
|
83
|
-
outpath_time < plogicpath_time
|
84
|
-
# puts "kwartz " + argv.join(" ")
|
85
|
-
main = Kwartz::Main.new(argv)
|
86
|
-
output = main.execute()
|
87
|
-
File.open(outpath, 'w') { |f| f.write(output) }
|
88
|
-
end
|
89
|
-
}
|
90
|
-
end
|
1
|
+
# ruby gems
|
2
|
+
#
|
3
|
+
# Check dependecies
|
4
|
+
require 'rubygems'
|
5
|
+
begin
|
6
|
+
require "kwartz"
|
7
|
+
require "kwartz/main"
|
8
|
+
rescue LoadError
|
9
|
+
abort 'kwartz gem required!!'
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
# ruby 1.9
|
14
|
+
# cambiare alla linea 73 kwartz/main.rb la linea seguente:
|
15
|
+
# return @_option_table.find { |row| row[1] == key }
|
16
|
+
# con
|
17
|
+
# return @_option_table.find { |row| row[1][0] == key[0] }
|
18
|
+
|
19
|
+
|
20
|
+
#
|
21
|
+
# kwartz utility
|
22
|
+
# template_dir: directory containing kwartz template
|
23
|
+
# template_include_dir: nil or a directory contained kwarz template referenced by template
|
24
|
+
# template_out_dir: where the output goes
|
25
|
+
#
|
26
|
+
def kwartz_compile( template_dir, template_include_dir, template_out_dir )
|
27
|
+
if not File.directory?(template_dir)
|
28
|
+
raise "template_dir: '#{template_dir}' is not a directory"
|
29
|
+
end
|
30
|
+
|
31
|
+
if not File.directory?(template_out_dir)
|
32
|
+
raise "template_out_dir: '#{template_out_dir}' is not a directory"
|
33
|
+
end
|
34
|
+
|
35
|
+
re_templatefile = /^[^\.](.+).html$/
|
36
|
+
|
37
|
+
maxtime_incs = Time.local( 1980 )
|
38
|
+
incs = []
|
39
|
+
if template_include_dir
|
40
|
+
if not File.directory?( template_include_dir )
|
41
|
+
raise "include_dir: '#{template_include_dir}' is not a directory"
|
42
|
+
end
|
43
|
+
incs = Dir.new( template_include_dir ).find_all { |f| f =~ re_templatefile }
|
44
|
+
incs = incs.map{ |f| File.join(template_include_dir, f) }
|
45
|
+
maxtime_incs = incs.inject(maxtime_incs) {|t,f|
|
46
|
+
t1 = File.mtime( f )
|
47
|
+
if t1 > t then t1 else t end
|
48
|
+
}
|
49
|
+
incs = incs.join(",")
|
50
|
+
end
|
51
|
+
|
52
|
+
Dir.foreach( template_dir ) { |f|
|
53
|
+
next unless f =~ re_templatefile
|
54
|
+
|
55
|
+
inpath = File.join(template_dir, f)
|
56
|
+
inpath_time = File.mtime( inpath )
|
57
|
+
|
58
|
+
plogicpath = template_dir + File::Separator + f.sub( /.html$/, ".plogic" )
|
59
|
+
plogicpath_time = Time.local( 1980 )
|
60
|
+
if File.exists?( plogicpath )
|
61
|
+
plogicpath_time = File.mtime( plogicpath )
|
62
|
+
end
|
63
|
+
|
64
|
+
outpath = File.join( template_out_dir, f.sub( /.html$/, ".rb" ) )
|
65
|
+
outpath_time = Time.local( 1980 )
|
66
|
+
if File.exist?( outpath )
|
67
|
+
outpath_time = File.mtime( outpath )
|
68
|
+
end
|
69
|
+
|
70
|
+
argv = %w[--delspan -a defun -l ruby]
|
71
|
+
if incs.length > 0
|
72
|
+
argv << "-i"
|
73
|
+
argv.push( incs )
|
74
|
+
end
|
75
|
+
|
76
|
+
if File.exist?( plogicpath )
|
77
|
+
argv.push( "-p", plogicpath )
|
78
|
+
end
|
79
|
+
argv.push( inpath )
|
80
|
+
|
81
|
+
if outpath_time < maxtime_incs or
|
82
|
+
outpath_time < inpath_time or
|
83
|
+
outpath_time < plogicpath_time
|
84
|
+
# puts "kwartz " + argv.join(" ")
|
85
|
+
main = Kwartz::Main.new(argv)
|
86
|
+
output = main.execute()
|
87
|
+
File.open(outpath, 'w') { |f| f.write(output) }
|
88
|
+
end
|
89
|
+
}
|
90
|
+
end
|
data/lib/gf_utilities/md5.rb
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
#
|
2
|
-
# calculate md5 of big files, found on usenet
|
3
|
-
#
|
4
|
-
|
5
|
-
if RUBY_VERSION =~ /1\.8/
|
6
|
-
# stdlib
|
7
|
-
require 'md5'
|
8
|
-
else
|
9
|
-
# stdlib
|
10
|
-
require 'digest/md5'
|
11
|
-
include Digest
|
12
|
-
end
|
13
|
-
|
14
|
-
class MD5
|
15
|
-
def self.file(file)
|
16
|
-
File.open(file, "rb") do |f|
|
17
|
-
res = self.new
|
18
|
-
while (data = f.read(4096))
|
19
|
-
res << data
|
20
|
-
end
|
21
|
-
res
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
1
|
+
#
|
2
|
+
# calculate md5 of big files, found on usenet
|
3
|
+
#
|
4
|
+
|
5
|
+
if RUBY_VERSION =~ /1\.8/
|
6
|
+
# stdlib
|
7
|
+
require 'md5'
|
8
|
+
else
|
9
|
+
# stdlib
|
10
|
+
require 'digest/md5'
|
11
|
+
include Digest
|
12
|
+
end
|
13
|
+
|
14
|
+
class MD5
|
15
|
+
def self.file(file)
|
16
|
+
File.open(file, "rb") do |f|
|
17
|
+
res = self.new
|
18
|
+
while (data = f.read(4096))
|
19
|
+
res << data
|
20
|
+
end
|
21
|
+
res
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/gf_utilities/numeric.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
class Numeric
|
2
|
-
#
|
3
|
-
# ritorna una stringa con le migliaia serparate da <separator>
|
4
|
-
# es.: 100000 -> 1.000.000
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# copiata da http://wiki.rubygarden.org/Ruby/page/show/FixNumFormat
|
8
|
-
#
|
9
|
-
def with_separator( separator = ',', length = 3 )
|
10
|
-
splitter = Regexp.compile "(\\d{#{length}})"
|
11
|
-
before, after = self.to_s.split('.')
|
12
|
-
before = before.reverse.gsub splitter, '\1' + separator
|
13
|
-
str = "#{ before.chomp( separator ).reverse }"
|
14
|
-
str += ".#{ after }" if after
|
15
|
-
return str
|
16
|
-
end
|
17
|
-
end
|
1
|
+
class Numeric
|
2
|
+
#
|
3
|
+
# ritorna una stringa con le migliaia serparate da <separator>
|
4
|
+
# es.: 100000 -> 1.000.000
|
5
|
+
#
|
6
|
+
#
|
7
|
+
# copiata da http://wiki.rubygarden.org/Ruby/page/show/FixNumFormat
|
8
|
+
#
|
9
|
+
def with_separator( separator = ',', length = 3 )
|
10
|
+
splitter = Regexp.compile "(\\d{#{length}})"
|
11
|
+
before, after = self.to_s.split('.')
|
12
|
+
before = before.reverse.gsub splitter, '\1' + separator
|
13
|
+
str = "#{ before.chomp( separator ).reverse }"
|
14
|
+
str += ".#{ after }" if after
|
15
|
+
return str
|
16
|
+
end
|
17
|
+
end
|
data/lib/treevisitor/abs_node.rb
CHANGED
@@ -1,116 +1,116 @@
|
|
1
|
-
# rubygems
|
2
|
-
require "rubygems"
|
3
|
-
require "abstract"
|
4
|
-
|
5
|
-
#
|
6
|
-
# Nodo Astratto
|
7
|
-
# Gerarchia delle classi
|
8
|
-
#
|
9
|
-
# AbsNode ha un nome, un parent
|
10
|
-
# | definisce un path
|
11
|
-
# |
|
12
|
-
# |- LeafNode
|
13
|
-
# |
|
14
|
-
# |- TreeNode
|
15
|
-
#
|
16
|
-
#
|
17
|
-
class AbsNode
|
18
|
-
|
19
|
-
attr_reader :parent
|
20
|
-
attr_reader :name
|
21
|
-
|
22
|
-
# solo TreeNode puo' scrivere vedi funzione add_leaf
|
23
|
-
attr_accessor :prev
|
24
|
-
attr_accessor :next
|
25
|
-
|
26
|
-
|
27
|
-
def initialize( name )
|
28
|
-
@parent = nil
|
29
|
-
@name = name
|
30
|
-
@prefix_path = nil
|
31
|
-
invalidate
|
32
|
-
end
|
33
|
-
|
34
|
-
#
|
35
|
-
# invalidate cached info
|
36
|
-
#
|
37
|
-
def invalidate
|
38
|
-
@path = nil
|
39
|
-
@path_from_root = nil
|
40
|
-
@depth = nil
|
41
|
-
end
|
42
|
-
|
43
|
-
def prefix_path
|
44
|
-
if not @parent.nil?
|
45
|
-
raise "Not root!!"
|
46
|
-
end
|
47
|
-
@prefix_path
|
48
|
-
end
|
49
|
-
|
50
|
-
def prefix_path=( prefix )
|
51
|
-
if not @parent.nil?
|
52
|
-
raise "Not root!!"
|
53
|
-
end
|
54
|
-
if prefix != @prefix_path
|
55
|
-
@prefix_path = prefix
|
56
|
-
invalidate
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def root
|
61
|
-
if root?
|
62
|
-
self
|
63
|
-
else
|
64
|
-
parent.root
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def path
|
69
|
-
return @path unless @path.nil?
|
70
|
-
if @parent.nil?
|
71
|
-
if @prefix_path
|
72
|
-
@path = @prefix_path + @name
|
73
|
-
else
|
74
|
-
@path = @name
|
75
|
-
end
|
76
|
-
else
|
77
|
-
@path = File.join( @parent.path, @name )
|
78
|
-
end
|
79
|
-
@path
|
80
|
-
end
|
81
|
-
|
82
|
-
def path_from_root
|
83
|
-
return @path_from_root unless @path_from_root.nil?
|
84
|
-
if @parent.nil?
|
85
|
-
if @prefix_path
|
86
|
-
@path_from_root = @prefix_path
|
87
|
-
else
|
88
|
-
@path_from_root = ""
|
89
|
-
end
|
90
|
-
else
|
91
|
-
@path_from_root = File.join( @parent.path_from_root, @name )
|
92
|
-
end
|
93
|
-
@path_from_root
|
94
|
-
end
|
95
|
-
|
96
|
-
def depth
|
97
|
-
return @depth unless @depth.nil?
|
98
|
-
if @parent.nil?
|
99
|
-
@depth = 1
|
100
|
-
else
|
101
|
-
@depth = @parent.depth + 1
|
102
|
-
end
|
103
|
-
@depth
|
104
|
-
end
|
105
|
-
|
106
|
-
def accept( visitor )
|
107
|
-
not_implemented
|
108
|
-
end
|
109
|
-
|
110
|
-
protected
|
111
|
-
|
112
|
-
def parent=( parent )
|
113
|
-
@parent = parent
|
114
|
-
end
|
115
|
-
|
116
|
-
end
|
1
|
+
# rubygems
|
2
|
+
require "rubygems"
|
3
|
+
require "abstract"
|
4
|
+
|
5
|
+
#
|
6
|
+
# Nodo Astratto
|
7
|
+
# Gerarchia delle classi
|
8
|
+
#
|
9
|
+
# AbsNode ha un nome, un parent
|
10
|
+
# | definisce un path
|
11
|
+
# |
|
12
|
+
# |- LeafNode
|
13
|
+
# |
|
14
|
+
# |- TreeNode
|
15
|
+
#
|
16
|
+
#
|
17
|
+
class AbsNode
|
18
|
+
|
19
|
+
attr_reader :parent
|
20
|
+
attr_reader :name
|
21
|
+
|
22
|
+
# solo TreeNode puo' scrivere vedi funzione add_leaf
|
23
|
+
attr_accessor :prev
|
24
|
+
attr_accessor :next
|
25
|
+
|
26
|
+
|
27
|
+
def initialize( name )
|
28
|
+
@parent = nil
|
29
|
+
@name = name
|
30
|
+
@prefix_path = nil
|
31
|
+
invalidate
|
32
|
+
end
|
33
|
+
|
34
|
+
#
|
35
|
+
# invalidate cached info
|
36
|
+
#
|
37
|
+
def invalidate
|
38
|
+
@path = nil
|
39
|
+
@path_from_root = nil
|
40
|
+
@depth = nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def prefix_path
|
44
|
+
if not @parent.nil?
|
45
|
+
raise "Not root!!"
|
46
|
+
end
|
47
|
+
@prefix_path
|
48
|
+
end
|
49
|
+
|
50
|
+
def prefix_path=( prefix )
|
51
|
+
if not @parent.nil?
|
52
|
+
raise "Not root!!"
|
53
|
+
end
|
54
|
+
if prefix != @prefix_path
|
55
|
+
@prefix_path = prefix
|
56
|
+
invalidate
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def root
|
61
|
+
if root?
|
62
|
+
self
|
63
|
+
else
|
64
|
+
parent.root
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def path
|
69
|
+
return @path unless @path.nil?
|
70
|
+
if @parent.nil?
|
71
|
+
if @prefix_path
|
72
|
+
@path = @prefix_path + @name
|
73
|
+
else
|
74
|
+
@path = @name
|
75
|
+
end
|
76
|
+
else
|
77
|
+
@path = File.join( @parent.path, @name )
|
78
|
+
end
|
79
|
+
@path
|
80
|
+
end
|
81
|
+
|
82
|
+
def path_from_root
|
83
|
+
return @path_from_root unless @path_from_root.nil?
|
84
|
+
if @parent.nil?
|
85
|
+
if @prefix_path
|
86
|
+
@path_from_root = @prefix_path
|
87
|
+
else
|
88
|
+
@path_from_root = ""
|
89
|
+
end
|
90
|
+
else
|
91
|
+
@path_from_root = File.join( @parent.path_from_root, @name )
|
92
|
+
end
|
93
|
+
@path_from_root
|
94
|
+
end
|
95
|
+
|
96
|
+
def depth
|
97
|
+
return @depth unless @depth.nil?
|
98
|
+
if @parent.nil?
|
99
|
+
@depth = 1
|
100
|
+
else
|
101
|
+
@depth = @parent.depth + 1
|
102
|
+
end
|
103
|
+
@depth
|
104
|
+
end
|
105
|
+
|
106
|
+
def accept( visitor )
|
107
|
+
not_implemented
|
108
|
+
end
|
109
|
+
|
110
|
+
protected
|
111
|
+
|
112
|
+
def parent=( parent )
|
113
|
+
@parent = parent
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
@@ -81,6 +81,7 @@ class CliTree
|
|
81
81
|
|
82
82
|
dtw = DirTreeWalker.new( dirname )
|
83
83
|
unless options[:all_files]
|
84
|
+
# TODO: la regex e' corretta ed un file che inizia con ".."??
|
84
85
|
dtw.add_ignore_pattern(/^\.[^.]+/) # ignore all file starting with "."
|
85
86
|
end
|
86
87
|
|
@@ -97,7 +98,7 @@ class CliTree
|
|
97
98
|
visitor = PrintDirTreeVisitor.new
|
98
99
|
dtw.run( visitor )
|
99
100
|
end
|
100
|
-
|
101
|
+
return 0
|
101
102
|
end
|
102
103
|
|
103
104
|
end
|
@@ -1,40 +1,40 @@
|
|
1
|
-
require 'pathname'
|
2
|
-
|
3
|
-
class DirProcessor
|
4
|
-
|
5
|
-
def initialize(
|
6
|
-
@
|
7
|
-
@
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
old_dirname = Dir.pwd
|
17
|
-
Dir.chdir( @dirname )
|
18
|
-
Dir["**/*"].each { |f|
|
19
|
-
pn = Pathname.new( f ).expand_path
|
20
|
-
# puts "#{self.class.name}#loadfromdir #{f}"
|
21
|
-
next if pn.directory?
|
22
|
-
process_file( pn )
|
23
|
-
}
|
24
|
-
Dir.chdir( old_dirname )
|
25
|
-
self
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def process_file( pn )
|
31
|
-
# puts "file: #{f}"
|
32
|
-
pair = @processors.find { |re,action| re =~ pn.to_s }
|
33
|
-
unless pair.nil?
|
34
|
-
pair[1].call( pn )
|
35
|
-
else
|
36
|
-
@default_processor.call( pn ) if @default_processor
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
class DirProcessor
|
4
|
+
|
5
|
+
def initialize( &action )
|
6
|
+
@processors = {}
|
7
|
+
@default_processor = action
|
8
|
+
end
|
9
|
+
|
10
|
+
def add_processor( re, &action )
|
11
|
+
@processors[ re ] = action
|
12
|
+
end
|
13
|
+
|
14
|
+
def process( dirname )
|
15
|
+
@dirname = dirname
|
16
|
+
old_dirname = Dir.pwd
|
17
|
+
Dir.chdir( @dirname )
|
18
|
+
Dir["**/*"].each { |f|
|
19
|
+
pn = Pathname.new( f ).expand_path
|
20
|
+
# puts "#{self.class.name}#loadfromdir #{f}"
|
21
|
+
next if pn.directory?
|
22
|
+
process_file( pn )
|
23
|
+
}
|
24
|
+
Dir.chdir( old_dirname )
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def process_file( pn )
|
31
|
+
# puts "file: #{f}"
|
32
|
+
pair = @processors.find { |re,action| re =~ pn.to_s }
|
33
|
+
unless pair.nil?
|
34
|
+
pair[1].call( pn )
|
35
|
+
else
|
36
|
+
@default_processor.call( pn ) if @default_processor
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|