Class: PostProcessCrystal

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_crystal_codemod/post_process_crystal.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = "") ⇒ PostProcessCrystal

Returns a new instance of PostProcessCrystal.



8
9
10
11
# File 'lib/ruby_crystal_codemod/post_process_crystal.rb', line 8

def initialize(filename = "")
  @filename = filename
  @contents = ""
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



2
3
4
# File 'lib/ruby_crystal_codemod/post_process_crystal.rb', line 2

def contents
  @contents
end

#filenameObject

Returns the value of attribute filename.



2
3
4
# File 'lib/ruby_crystal_codemod/post_process_crystal.rb', line 2

def filename
  @filename
end

Class Method Details

.file_read_lines(path) ⇒ Object



4
5
6
# File 'lib/ruby_crystal_codemod/post_process_crystal.rb', line 4

def self.file_read_lines(path)
  File.read(path).lines.map(&:chomp)
end

Instance Method Details

#post_process_crystalObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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