class Giteaucrat::Formatters::Formatter
Constants
- HASHBANG_REGEXP
Attributes
comment_lines[R]
Public Instance Methods
add_copyright!()
click to toggle source
# File lib/giteaucrat/formatters/formatter.rb, line 89 def add_copyright! @contents = [format_copyright, contents].join("\n\n") end
comment?()
click to toggle source
# File lib/giteaucrat/formatters/formatter.rb, line 77 def comment? !!@comment_lines end
comment_parts()
click to toggle source
# File lib/giteaucrat/formatters/formatter.rb, line 39 def comment_parts self.class.const_get(:COMMENT_PARTS) end
format_copyright()
click to toggle source
@return [String]
# File lib/giteaucrat/formatters/formatter.rb, line 18 def format_copyright first, _, last = comment_parts 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 << header_ruler(line_width) formatted << lines.map { |line| format_line(line) } formatted << footer_ruler(line_width) formatted.join("\n") end
format_line(line)
click to toggle source
# File lib/giteaucrat/formatters/formatter.rb, line 57 def format_line(line) " #{line} " end
header_ruler(line_width)
click to toggle source
# File lib/giteaucrat/formatters/formatter.rb, line 43 def header_ruler(line_width) ruler(line_width) end
parse_comment(comment)
click to toggle source
# File lib/giteaucrat/formatters/formatter.rb, line 70 def parse_comment(comment) @comment_lines = comment.split("\n") @copyright_lines = nil end
remove_copyright!()
click to toggle source
# 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), '') parse_comment($LAST_MATCH_INFO[:comment]) if $LAST_MATCH_INFO && $LAST_MATCH_INFO[:comment] end
ruler(line_width)
click to toggle source
# File lib/giteaucrat/formatters/formatter.rb, line 51 def ruler(line_width) first, middle, last = comment_parts ruler_width = (line_width - first.size - last.size) / middle.size "#{first}#{middle * ruler_width}#{last}" end
write_copyright!()
click to toggle source
@return [String]
# 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
Private Instance Methods
contents()
click to toggle source
# File lib/giteaucrat/formatters/formatter.rb, line 125 def contents @contents ||= read_contents end
copyright_lines()
click to toggle source
# File lib/giteaucrat/formatters/formatter.rb, line 95 def copyright_lines @copyright_lines ||= begin lines = [] lines << repo.copyright_label if owner || !authors.empty? authors_label = authors.size > 1 ? 'Authors: ' : 'Author: ' author_names = (authors - [owner]).map(&:identifier).sort prepend = ' ' * authors_label.size lines << "#{authors_label}#{owner.identifier}" author_names.each do |author| lines << "#{prepend}#{author}" end end if comment? lines << '' lines += comment_lines end lines end end
file()
click to toggle source
@return [Giteaucrat::File]
# File lib/giteaucrat/formatters/formatter.rb, line 121 def file __getobj__ end