Class: Giteaucrat::Formatters::Formatter

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/giteaucrat/formatters/formatter.rb

Direct Known Subclasses

JavaFormatter, RubyFormatter

Constant Summary collapse

HASHBANG_REGEXP =
/\A(#!.*)\n/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#comment_linesObject (readonly)

Returns the value of attribute comment_lines.



75
76
77
# File 'lib/giteaucrat/formatters/formatter.rb', line 75

def comment_lines
  @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

#comment?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/giteaucrat/formatters/formatter.rb', line 77

def comment?
  !!@comment_lines
end

#comment_partsObject



39
40
41
# File 'lib/giteaucrat/formatters/formatter.rb', line 39

def comment_parts
  self.class.const_get(:COMMENT_PARTS)
end


47
48
49
# File 'lib/giteaucrat/formatters/formatter.rb', line 47

def footer_ruler(line_width)
  ruler(line_width)
end

Returns:

  • (String)


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 = 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) ⇒ Object



57
58
59
# File 'lib/giteaucrat/formatters/formatter.rb', line 57

def format_line(line)
  "  #{line}  "
end

#header_ruler(line_width) ⇒ Object



43
44
45
# File 'lib/giteaucrat/formatters/formatter.rb', line 43

def header_ruler(line_width)
  ruler(line_width)
end

#parse_comment(comment) ⇒ Object



70
71
72
73
# File 'lib/giteaucrat/formatters/formatter.rb', line 70

def parse_comment(comment)
  @comment_lines = comment.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), '')
  parse_comment($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 = comment_parts
  ruler_width = (line_width - first.size - last.size) / middle.size
  "#{first}#{middle * ruler_width}#{last}"
end

#write_copyright!String

Returns:

  • (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