class CodeCaser::Caser

Public Class Methods

new(opts = {}) click to toggle source
# File lib/code_caser/caser.rb, line 6
def initialize(opts = {})
  @converter      = opts.fetch(:converter)
  @path_converter = PathConverter.new(opts.fetch(:path))
  @save           = opts.fetch(:save, true)
  @verbose        = opts.fetch(:verbose, false)
end

Public Instance Methods

start() click to toggle source
# File lib/code_caser/caser.rb, line 13
def start
  files = @path_converter.get_files
  if files.empty?
    puts "File or folder not found.\n"
    return
  elsif user_aborted?(files)
    puts "File conversion aborted.\n"
    return
  end
  convert_files(files)
  puts "\n#{files.count} file(s) converted.".colorize(:green)
  if @save
    puts "Backup copies of the original files can be found here:".colorize(:green)
    puts "#{backup_folder}\n"
  end
end

Private Instance Methods

backup_folder() click to toggle source
# File lib/code_caser/caser.rb, line 32
def backup_folder
  @backup_folder ||= @path_converter.join("_backup_#{Time.new.to_i}")
end
convert_file(file_path) click to toggle source
# File lib/code_caser/caser.rb, line 42
def convert_file(file_path)
  file_name = File.basename(file_path)
  puts "-> Converting #{file_name}...".colorize(:blue) if @verbose
  backup_file_path = File.join(backup_folder, file_name)
  FileUtils.cp(file_path, backup_file_path)
  # Replace the file with its converted equivalent.
  FileUtils.rm(file_path)
  f = File.new(file_path, "w+")
  IO.foreach(backup_file_path) do |line|
    f.puts(convert_line(line.chomp))
  end
end
convert_files(files) click to toggle source
# File lib/code_caser/caser.rb, line 36
def convert_files(files)
  FileUtils.mkdir_p(backup_folder) # Original files will be cached to this backup folder.
  files.each { |f| convert_file(f) if File.file?(f) }
  FileUtils.rm_r(backup_folder) unless @save
end
convert_line(line) click to toggle source
# File lib/code_caser/caser.rb, line 55
def convert_line(line)
  @converter.convert_line(line, @verbose)
end
user_aborted?(files) click to toggle source
# File lib/code_caser/caser.rb, line 59
def user_aborted?(files)
  puts "Warning: This will convert all files listed below from #{@converter.description}.\n".colorize(:yellow)
  puts "No back-ups of these files will be created.\n".colorize(:yellow) unless @save
  puts files
  puts ("\nMake sure your files are checked in to source control before converting." +
        "\nTo confirm, type 'CONVERT':").colorize(:yellow)
  STDIN.gets.chomp != "CONVERT"
end