class RenameGem::Renamer::FileHandler

Attributes

file[R]
path[R]
possession[R]

Public Class Methods

new(path) click to toggle source
# File lib/rename_gem/renamer/file_handler.rb, line 10
def initialize(path)
  @path = path
  @file = File.new(path.to_s)
  @possession = Possession.new(file)
end

Public Instance Methods

edit(from, to) click to toggle source
# File lib/rename_gem/renamer/file_handler.rb, line 16
def edit(from, to)
  temp_file = Tempfile.new(path.filename)
  changes = false

  file.each_line do |line|
    temp_file.puts replacement(line, from, to)

    changes = true
  rescue StringReplacer::NoMatchError
    temp_file.puts line
  end

  temp_file.close

  if changes
    FileUtils.mv(temp_file.path, path.to_s)
    possession.update(file)
  end

  temp_file.unlink

  changes
end
rename(from, to) click to toggle source
# File lib/rename_gem/renamer/file_handler.rb, line 40
def rename(from, to)
  new_filename = replacement(path.filename, from, to)
  built_path = path.build(new_filename)

  File.rename(path.to_s, built_path.to_s)
  @path = built_path

  true
rescue StringReplacer::NoMatchError
  false
end

Private Instance Methods

replacement(text, from, to) click to toggle source
# File lib/rename_gem/renamer/file_handler.rb, line 54
def replacement(text, from, to)
  replacer = StringReplacer.new(text)
  replacer.replace(from).with(to)
end