Skip to content

Commit 2788386

Browse files
committed
refactor
modified: format.rb
1 parent 5211eff commit 2788386

1 file changed

Lines changed: 45 additions & 16 deletions

File tree

.tools/format.rb

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
11
require_relative 'dir_handler'
22

3-
extensions = [ "cpp", "h", "hpp", "md" ]
4-
extensions.freeze
5-
dir = ARGV[0]
6-
dir.freeze
3+
#extensions = [ "cpp", "h", "hpp", "md" ]
4+
#extensions.freeze
5+
#dir = ARGV[0]
6+
#dir.freeze
7+
#
8+
##init handlers for all types of files
9+
#handlers = extensions.collect{ |e| DirHandler.new dir, e }
10+
#
11+
##for comma:
12+
## 1,2,3,4 => 1, 2, 3, 4
13+
#handlers.each do |h|
14+
# h.on_each_line do |line|
15+
# begin
16+
# line.gsub! /,(\S)/, ', \1' unless line.match /.*\".*,.*\".*/ or line.match /','/
17+
# rescue Exception => e
18+
# puts e.message + ", ignored."
19+
# end
20+
# end
21+
#end
22+
#
723

8-
#init handlers for all types of files
9-
handlers = extensions.collect{ |e| DirHandler.new dir, e }
24+
class Format
25+
attr_reader :extensions
1026

11-
#for comma:
12-
# 1,2,3,4 => 1, 2, 3, 4
13-
handlers.each do |h|
14-
h.on_each_line do |line|
15-
begin
16-
line.gsub! /,(\S)/, ', \1' unless line.match /.*\".*,.*\".*/ or line.match /','/
17-
rescue Exception => e
18-
puts e.message + ", ignored."
19-
end
27+
def initialize dir, extensions
28+
@dir = dir
29+
@extensions = extensions
30+
@dir.freeze
31+
@extensions.freeze
32+
@handlers = extensions.collect{ |e| DirHandler.new dir, e }
2033
end
21-
end
34+
35+
def for_commas
36+
@handlers.each do |h|
37+
h.on_each_line do |line|
38+
begin
39+
line.gsub! /,(\S)/, ', \1' unless line.match /.*\".*,.*\".*/ or line.match /','/
40+
rescue Exception => e
41+
puts e.message + ", ignored."
42+
end
43+
end
44+
end
45+
end
46+
end
47+
48+
format = Format.new ARGV[0], [ "cpp", "h", "hpp", "md" ]
49+
format.for_commas
50+

0 commit comments

Comments
 (0)