Class: Giteaucrat::Formatters::RubyFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/giteaucrat/formatters/ruby_formatter.rb

Direct Known Subclasses

CoffeeFormatter, PythonFormatter

Constant Summary collapse

COMMENT_PARTS =
%w[# # #].freeze
CODING_REGEXP =

Returns:

  • (String)
/\A((#\s*.*coding:\s*[^\s]+)\s*\n+)?/
/(?<ruler>##+#\n)(?<copyright>(#\s*[^\s#]+.*\s#\n)+)(#\s+#?\n(?<comment>(#\s*.*#?\n)+))?\k<ruler>\n+/

Constants inherited from Formatter

Formatter::HASHBANG_REGEXP

Instance Attribute Summary

Attributes inherited from Formatter

#comment_lines

Instance Method Summary collapse

Methods inherited from Formatter

#add_copyright!, #comment?, #comment_parts, #footer_ruler, #header_ruler, #ruler, #write_copyright!

Instance Method Details

#encodingObject



49
50
51
# File 'lib/giteaucrat/formatters/ruby_formatter.rb', line 49

def encoding
  @encoding || (include_encoding? && '# coding: utf-8' || nil)
end


20
21
22
23
24
# File 'lib/giteaucrat/formatters/ruby_formatter.rb', line 20

def format_copyright
  copyright = super
  copyright = [encoding, copyright].compact.join("\n\n")
  copyright
end

#format_line(line) ⇒ Object



26
27
28
29
# File 'lib/giteaucrat/formatters/ruby_formatter.rb', line 26

def format_line(line)
  first, _, last = comment_parts
  "#{first} #{line} #{last}"
end

#include_encoding?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/giteaucrat/formatters/ruby_formatter.rb', line 45

def include_encoding?
  repo.include_encoding?
end

#parse_comment(comment) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/giteaucrat/formatters/ruby_formatter.rb', line 37

def parse_comment(comment)
  middle = Regexp.escape(comment_parts[1])
  comment_lines = comment.split("\n").map do |line|
    line.sub(/\A#{middle}\s?/, '').sub(/\s*#{middle}\s*\Z/, '')
  end
  @comment_lines = comment_lines
end

#remove_copyright!Object



31
32
33
34
35
# File 'lib/giteaucrat/formatters/ruby_formatter.rb', line 31

def remove_copyright!
  super
  contents.sub!(CODING_REGEXP, '')
  @encoding = Regexp.last_match(2) if Regexp.last_match(2)
end