class Far::Replacer

Attributes

change_report[RW]
file_list[RW]
options[RW]
substitution[R]

Public Class Methods

new(substitution, options) click to toggle source
# File lib/far/replacer.rb, line 6
def initialize(substitution, options)
  self.substitution = substitution
  @options          = Options.new options
  generate_change_report
end

Public Instance Methods

diff() click to toggle source
# File lib/far/replacer.rb, line 76
def diff
  files.each do |file|
    replacement_number = 0

    diff_file(file).lines.each_slice(2) do |slice|
      change_report[file.name][replacement_number][:changed] = slice[1]
      replacement_number += 1
    end
  end
end
diff_file(file) click to toggle source
# File lib/far/replacer.rb, line 71
def diff_file(file)
  original_version, changed_version = replace_file(file)
  Differ.diff_by_line(changed_version, original_version).format_as :plain
end
dry_run?() click to toggle source
# File lib/far/replacer.rb, line 51
def dry_run?
  !@options[:please].value || !@options[:replace].value
end
files() click to toggle source
# File lib/far/replacer.rb, line 29
def files
  file_list.map { |file| File.new file, find }
end
find() click to toggle source
# File lib/far/replacer.rb, line 17
def find
  @substitution.split('/')[1]
end
generate_change_report() click to toggle source
# File lib/far/replacer.rb, line 55
def generate_change_report
  change_report
end
in_place() click to toggle source
# File lib/far/replacer.rb, line 33
def in_place
  return "" if dry_run?
  return "-i ''"
end
redirection() click to toggle source
# File lib/far/replacer.rb, line 38
def redirection
  "> #{tmp_file}" if dry_run?
end
replace() click to toggle source
# File lib/far/replacer.rb, line 21
def replace
  @substitution.split('/')[2]
end
replace_file(file) click to toggle source
# File lib/far/replacer.rb, line 63
def replace_file(file)
  original_version = `cat #{file.name}`
  `#{change_file_cmd(file)}`
  changed_version  = `cat #{replaced_file(file.name)}`

  return original_version, changed_version
end
replaced_file(file) click to toggle source
# File lib/far/replacer.rb, line 42
def replaced_file(file)
  return tmp_file if dry_run?
  file
end
run?() click to toggle source
# File lib/far/replacer.rb, line 47
def run?
  !dry_run?
end
substitution=(value) click to toggle source
# File lib/far/replacer.rb, line 12
def substitution=(value)
  value += "/" unless value[-1] == "/"
  @substitution = value
end

Private Instance Methods

change_file_cmd(file) click to toggle source
# File lib/far/replacer.rb, line 96
def change_file_cmd(file)
  "sed #{in_place} -e #{substitution} #{file.name} #{redirection}"
end
file_list_cmd() click to toggle source
# File lib/far/replacer.rb, line 88
def file_list_cmd
  "ack #{find} -l #{@options.to_command_line}"
end
lines_to_change_cmd(file) click to toggle source
# File lib/far/replacer.rb, line 92
def lines_to_change_cmd(file)
  "ack #{find} #{file} /dev/null"
end
tmp_file() click to toggle source
# File lib/far/replacer.rb, line 100
def tmp_file
  "./far.tmp"
end