Sha256: b34468686350be040f383f194d4c9ccc085557f6cc15ee74c6b27890991f852e

Contents?: true

Size: 1.28 KB

Versions: 9

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Pragmater
  # Formats pragma comments in a consistent manner.
  class Formatter
    def self.shebang_format
      %r(\A\#\!\s?\/.*ruby\Z)
    end

    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

    def self.valid_formats
      Regexp.union shebang_format, pragma_format
    end

    def initialize string
      @string = string
    end

    def format_shebang
      return string unless string.match? self.class.shebang_format

      _, path = string.split "!"
      "#! #{path.strip}"
    end

    def format_pragma
      return string unless string.match? self.class.pragma_format

      key, value = string.split ":"
      "# #{key.gsub(/\#\s?/, "")}: #{value.strip}"
    end

    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

    private

    attr_reader :string
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pragmater-7.1.0 lib/pragmater/formatter.rb
pragmater-7.0.1 lib/pragmater/formatter.rb
pragmater-7.0.0 lib/pragmater/formatter.rb
pragmater-6.3.3 lib/pragmater/formatter.rb
pragmater-6.3.2 lib/pragmater/formatter.rb
pragmater-6.3.1 lib/pragmater/formatter.rb
pragmater-6.3.0 lib/pragmater/formatter.rb
pragmater-6.2.1 lib/pragmater/formatter.rb
pragmater-6.2.0 lib/pragmater/formatter.rb