-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtabber.rb
executable file
·53 lines (46 loc) · 1.45 KB
/
tabber.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
module Tabber
extend self
def tab_out(string)
lines = string.split("\n").map do |line|
whitespace = string[/^(\s*)/, 1]
first, rest = line.lstrip.split(/\s+/, 2)
[whitespace + first, rest.split(/,\s*/)].flatten
end
sizes = []
lines.each do |line|
line.each_with_index do |field, i|
sizes[i] ||= 0
sizes[i] = field.size if sizes[i] < field.size
end
end
lines.each_with_index do |line, i|
line.each_with_index do |field, j|
total_length = sizes[j] + 1
spaces = " " * (total_length - field.size)
add = ((j == 0) ? "" : ",") + spaces
lines[i][j] = field + add unless (j == (lines[i].size - 1))
end
end
lines.map {|line| line.join}.join("\n")
end
end
require 'rubygems'
require 'test/spec'
describe 'Tabber' do
setup do
@string = <<-EOF
map.thing '/thing', :controller => 'feet', :method => "default"
map.thing_two '/thing_two', :controller => 'f'
map.thing_three '/thing_three', :controller => 'f', :method => "three"
EOF
@result = <<-EOF
map.thing '/thing', :controller => 'feet', :method => "default"
map.thing_two '/thing_two', :controller => 'f'
map.thing_three '/thing_three', :controller => 'f', :method => "three"
EOF
end
it "should generate the output from the input" do
Tabber.tab_out(@string).should == @result.rstrip
end
end