class PostProcessCrystal

Attributes

contents[R]
filename[R]

Public Class Methods

file_read_lines(path) click to toggle source
# File lib/ruby_crystal_codemod/post_process_crystal.rb, line 4
def self.file_read_lines(path)
  File.read(path).lines.map(&:chomp)
end
new(filename = "") click to toggle source
# File lib/ruby_crystal_codemod/post_process_crystal.rb, line 8
def initialize(filename = "")
  @filename = filename
  @contents = ""
end

Public Instance Methods

filename=(filename) click to toggle source
# File lib/ruby_crystal_codemod/post_process_crystal.rb, line 13
def filename=(filename)
  @filename = filename
  @contents = ""
end
post_process_crystal() click to toggle source
# File lib/ruby_crystal_codemod/post_process_crystal.rb, line 18
def post_process_crystal
  @contents = +""
  lines = self.class.file_read_lines(@filename)

  current_lang = nil
  regex = /^\s*# ?~# (?<action>(BEGIN|END)) (?<lang>(ruby|crystal))/
  uncomment_regex = /^(?<indent>\s*)# ?/
  lines.each do |line|
    matches = regex.match(line)
    if matches
      case matches["action"]
      when "BEGIN"
        current_lang = matches["lang"]
      when "END"
        current_lang = nil
      end
      next
    end
    case current_lang
    when "ruby"
      next
    when "crystal"
      line = line.sub(uncomment_regex, "\\k<indent>")
    end

    @contents << line << "\n"
  end
end