class ArelConverter::Base

Attributes

options[RW]

Public Class Methods

new(path, options = {}) click to toggle source
# File lib/arel_converter/base.rb, line 6
def initialize(path, options = {})
  @path    = path
  @options = {dry_run: false}.merge(options)
end

Public Instance Methods

grep_matches_in_file(file) click to toggle source
# File lib/arel_converter/base.rb, line 65
def grep_matches_in_file(file)
  [] # abstract method overriden by subclasses
end
parse_directory(path) click to toggle source
# File lib/arel_converter/base.rb, line 15
def parse_directory(path)
  Dir[File.join(path, '**/*.rb')].each do |file|
    begin
      parse_file(file)
    rescue => e
      Formatter.alert(file, [])
    end
  end
end
parse_file(file) click to toggle source
# File lib/arel_converter/base.rb, line 25
def parse_file(file)

  lines_to_process = grep_matches_in_file(file)

  return if lines_to_process.empty?

  replacements = process_lines(lines_to_process)

  unless (replacements.nil? || replacements.empty?)
    Formatter.alert(file, replacements)
    update_file(file, replacements) unless @options[:dry_run]
  end
end
process_lines(lines) click to toggle source
# File lib/arel_converter/base.rb, line 39
def process_lines(lines)
  lines.map do |line|
    r = Replacement.new(line)
    begin
      next unless verify_line(line)
      r.new_content = process_line(line)
    rescue SyntaxError => e
      r.error = "SyntaxError when evaluating options for #{line}"
    rescue Exception => e
      r.error = "#{e.class} #{e.message} when evaluating options for \"#{line}\"\n#{e.backtrace.first}"
    end
    r
  end.compact
end
run!() click to toggle source
# File lib/arel_converter/base.rb, line 11
def run!
  File.directory?(@path) ? parse_directory(@path) : parse_file(@path)
end
update_file(file, line_replacements) click to toggle source
# File lib/arel_converter/base.rb, line 54
def update_file(file, line_replacements)
  contents = File.read(file)
  line_replacements.each do |r|
    contents.gsub!(r.old_content, r.new_content) if r.valid?
  end

  File.open(file, 'w') do |f|
    f.puts contents
  end
end

Protected Instance Methods

verify_line(line) click to toggle source
# File lib/arel_converter/base.rb, line 71
def verify_line(line)
  true
end