Class: Fit::FileRunner
- Inherits:
-
Object
show all
- Defined in:
- lib/fit/file_runner.rb
Constant Summary
collapse
- DEFAULT_ENCODING =
'UTF-8'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of FileRunner.
Instance Attribute Details
#fixture ⇒ Object
Returns the value of attribute fixture.
14
15
16
|
# File 'lib/fit/file_runner.rb', line 14
def fixture
@fixture
end
|
Returns the value of attribute input.
14
15
16
|
# File 'lib/fit/file_runner.rb', line 14
def input
@input
end
|
#output ⇒ Object
Returns the value of attribute output.
14
15
16
|
# File 'lib/fit/file_runner.rb', line 14
def output
@output
end
|
#tables ⇒ Object
Returns the value of attribute tables.
14
15
16
|
# File 'lib/fit/file_runner.rb', line 14
def tables
@tables
end
|
Instance Method Details
#check_output_file(arg) ⇒ Object
56
57
58
59
|
# File 'lib/fit/file_runner.rb', line 56
def check_output_file arg
error "no output file" if arg.nil?
output_name = File.expand_path arg
end
|
#create_output_file(output_name) ⇒ Object
61
62
63
64
65
66
67
68
|
# File 'lib/fit/file_runner.rb', line 61
def create_output_file output_name
FileUtils.mkpath File.dirname(output_name)
if output_name.respond_to? :encoding
@output = File.open output_name, "w:#@encoding"
else
@output = File.open output_name, 'w'
end
end
|
#process ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/fit/file_runner.rb', line 84
def process
begin
unless @input.index('<wiki>').nil?
@tables = Parse.new @input, ['wiki', 'table', 'tr', 'td']
@fixture.do_tables @tables.parts
else
@tables = Parse.new @input, ['table', 'tr', 'td']
@fixture.do_tables @tables
end
rescue Exception => e
exception e
end
if @encoding == DEFAULT_ENCODING
@tables.print @output
else
buffer = StringIO.new
conv = Iconv.new "#{DEFAULT_ENCODING}//IGNORE", DEFAULT_ENCODING
@tables.print buffer, conv
buffer.print conv.iconv(nil)
conv = Iconv.new "#@encoding//IGNORE", DEFAULT_ENCODING
@output.print conv.iconv(buffer.string)
@output << conv.iconv(nil)
buffer.close
end
@output.close
end
|
#process_args(args) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/fit/file_runner.rb', line 31
def process_args args
error "no input file" if args[0].nil?
input_name = File.expand_path args[0]
begin
if input_name.respond_to? :encoding
input_file = File.open input_name, "r:#@encoding"
else
input_file = File.open input_name
end
rescue Errno::ENOENT
error "#{input_name}: file not found"
end
output_name = check_output_file args[1]
create_output_file output_name
@fixture.summary['input file'] = input_name
@fixture.summary['input update'] = input_file.mtime.to_s
@fixture.summary['output file'] = output_name
unless input_name.respond_to? :encoding
read_file_with_encoding input_file
else
read_file input_file
end
input_file.close
end
|
#read_file(input_file) ⇒ Object
80
81
82
|
# File 'lib/fit/file_runner.rb', line 80
def read_file input_file
@input = (@encoding == DEFAULT_ENCODING) ? input_file.read : input_file.read.encode(DEFAULT_ENCODING)
end
|
#read_file_with_encoding(input_file) ⇒ Object
70
71
72
73
74
75
76
77
78
|
# File 'lib/fit/file_runner.rb', line 70
def read_file_with_encoding input_file
if @encoding == DEFAULT_ENCODING
@input = input_file.read
else
conv = Iconv.new DEFAULT_ENCODING, @encoding
@input = conv.iconv(input_file.read)
@input << conv.iconv(nil)
end
end
|
#run(args, opts = nil) ⇒ Object
23
24
25
26
27
28
29
|
# File 'lib/fit/file_runner.rb', line 23
def run args, opts=nil
@encoding = opts[:encoding].upcase unless opts.nil?
process_args args
process
$stderr.puts @fixture.totals
exit @fixture.total_errors
end
|