Class: Pragmater::Formatter
- Inherits:
-
Object
- Object
- Pragmater::Formatter
- Defined in:
- lib/pragmater/formatter.rb
Overview
Formats pragma comments in a consistent manner.
Class Method Summary collapse
Instance Method Summary collapse
- #format ⇒ Object
- #format_pragma ⇒ Object
- #format_shebang ⇒ Object
-
#initialize(string) ⇒ Formatter
constructor
A new instance of Formatter.
Constructor Details
#initialize(string) ⇒ Formatter
Returns a new instance of Formatter.
27 28 29 |
# File 'lib/pragmater/formatter.rb', line 27 def initialize string @string = string end |
Class Method Details
.pragma_format ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/pragmater/formatter.rb', line 10 def self.pragma_format / \A # Start of line. \# # Start of comment. \s? # Space - optional. \w+ # Key - 1 or more word characters only. \: # Key and value delimiter. \s? # Space - optional. [\w\-]+ # Value - 1 or more word or dash characters. \Z # End of line. /x end |
.shebang_format ⇒ Object
6 7 8 |
# File 'lib/pragmater/formatter.rb', line 6 def self.shebang_format %r(\A\#\!\s?\/.*ruby\Z) end |
.valid_formats ⇒ Object
23 24 25 |
# File 'lib/pragmater/formatter.rb', line 23 def self.valid_formats Regexp.union shebang_format, pragma_format end |
Instance Method Details
#format ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/pragmater/formatter.rb', line 45 def format klass = self.class case string when klass.shebang_format then format_shebang when klass.pragma_format then format_pragma else string end end |
#format_pragma ⇒ Object
38 39 40 41 42 43 |
# File 'lib/pragmater/formatter.rb', line 38 def format_pragma return string unless string.match?(self.class.pragma_format) key, value = string.split ":" "# #{key.gsub(/\#\s?/, "")}: #{value.strip}" end |
#format_shebang ⇒ Object
31 32 33 34 35 36 |
# File 'lib/pragmater/formatter.rb', line 31 def format_shebang return string unless string.match?(self.class.shebang_format) _, path = string.split "!" "#! #{path.strip}" end |