Class: Synvert::CheckingVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/synvert/checking_visitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CheckingVisitor

Returns a new instance of CheckingVisitor.



6
7
8
9
10
# File 'lib/synvert/checking_visitor.rb', line 6

def initialize(options={})
  @converters = options[:converters] || [
    Synvert::FactoryGirl::SyntaxMethodsConverter.new
  ]
end

Instance Attribute Details

#converters ⇒ Object (readonly)

Returns the value of attribute converters.



4
5
6
# File 'lib/synvert/checking_visitor.rb', line 4

def converters
  @converters
end

Instance Method Details

#convert(filename, source) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/synvert/checking_visitor.rb', line 20

def convert(filename, source)
  source_buffer = Parser::Source::Buffer.new filename
  source_buffer.source = source

  parser = Parser::CurrentRuby.new
  ast = parser.parse source_buffer

  converted_source = source
  @converters.each do |converter|
    if Array(converter.interesting_files).any? { |file_pattern| filename =~ file_pattern }
      converted_source = converter.rewrite(source_buffer, ast)
    end
  end

  converted_source
end

#convert_file(filename) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/synvert/checking_visitor.rb', line 12

def convert_file(filename)
  source = File.read filename
  converted_source = convert(filename, source)
  if source != converted_source
    File.write(filename, converted_source)
  end
end