Class: Giteaucrat::Formatters::Formatter
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Giteaucrat::Formatters::Formatter
show all
- Defined in:
- lib/giteaucrat/formatters/formatter.rb
Constant Summary
collapse
- HASHBANG_REGEXP =
/\A(#!.*)\n/
Instance Attribute Summary collapse
Instance Method Summary
collapse
Instance Attribute Details
Returns the value of attribute comment_lines.
75
76
77
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 75
def
@comment_lines
end
|
Instance Method Details
#add_copyright! ⇒ Object
89
90
91
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 89
def add_copyright!
@contents = [format_copyright, contents].join("\n\n")
end
|
77
78
79
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 77
def
!!@comment_lines
end
|
39
40
41
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 39
def
self.class.const_get(:COMMENT_PARTS)
end
|
47
48
49
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 47
def (line_width)
ruler(line_width)
end
|
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 18
def format_copyright
first, _, last =
lines = copyright_lines
max_line_size = lines.max_by(&:size).size
max_line_size += 1 if max_line_size.odd?
lines = lines.map do |line|
blank = ' ' * (max_line_size - line.size) if line.size < max_line_size
"#{line}#{blank}"
end
line_width = first.size + 1 + max_line_size + 1 + last.size
formatted = []
formatted << (line_width)
formatted << lines.map { |line| format_line(line) }
formatted << (line_width)
formatted.join("\n")
end
|
57
58
59
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 57
def format_line(line)
" #{line} "
end
|
43
44
45
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 43
def (line_width)
ruler(line_width)
end
|
70
71
72
73
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 70
def ()
@comment_lines = .split("\n")
@copyright_lines = nil
end
|
#remove_copyright! ⇒ Object
61
62
63
64
65
66
67
68
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 61
def remove_copyright!
if contents =~ HASHBANG_REGEXP
@hashbang = Regexp.last_match(1)
contents.sub!(HASHBANG_REGEXP, '')
end
contents.sub!(self.class.const_get(:COPYRIGHT_REGEXP), '')
($LAST_MATCH_INFO[:comment]) if $LAST_MATCH_INFO && $LAST_MATCH_INFO[:comment]
end
|
#ruler(line_width) ⇒ Object
51
52
53
54
55
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 51
def ruler(line_width)
first, middle, last =
ruler_width = (line_width - first.size - last.size) / middle.size
"#{first}#{middle * ruler_width}#{last}"
end
|
#write_copyright! ⇒ String
82
83
84
85
86
87
|
# File 'lib/giteaucrat/formatters/formatter.rb', line 82
def write_copyright!
remove_copyright!
add_copyright!
@contents = [@hashbang, contents].compact.join("\n")
write_contents(contents)
end
|