Module: Rewriter

Defined in:
lib/rewriter.rb,
lib/rewriter/version.rb

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.rewrite(path) ⇒ Object



8
9
10
11
12
13
# File 'lib/rewriter.rb', line 8

def rewrite(path)
  with_file(path) do |source|
    ast, comments = Parser::CurrentRuby.parse_with_comments(source)
    Unparser.unparse(ast, comments)
  end
end

.with_file(path) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rewriter.rb', line 15

def with_file(path)
  original = File.read(path)
  rewritten = yield(original)
  
  temp_path = "#{path}.rewritten"
  ::File.open(temp_path, 'w') do |temp_file|
    temp_file.write(rewritten)
  end
  
  ::File.rename(temp_path, path)
end