Class: Far::Replacer

Inherits:
Object
  • Object
show all
Defined in:
lib/far/replacer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(substitution, options) ⇒ Replacer

Returns a new instance of Replacer.



6
7
8
9
10
# File 'lib/far/replacer.rb', line 6

def initialize(substitution, options)
  self.substitution = substitution
  @options          = Options.new options
  generate_change_report
end

Instance Attribute Details

#change_reportObject

Returns the value of attribute change_report.



4
5
6
# File 'lib/far/replacer.rb', line 4

def change_report
  @change_report
end

#file_listObject

Returns the value of attribute file_list.



4
5
6
# File 'lib/far/replacer.rb', line 4

def file_list
  @file_list
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/far/replacer.rb', line 4

def options
  @options
end

#substitutionObject

Returns the value of attribute substitution.



3
4
5
# File 'lib/far/replacer.rb', line 3

def substitution
  @substitution
end

Instance Method Details

#diffObject



76
77
78
79
80
81
82
83
84
85
# 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) ⇒ Object



71
72
73
74
# File 'lib/far/replacer.rb', line 71

def diff_file(file)
  original_version, changed_version = replace_file(file)
  Differ.(changed_version, original_version).format_as :plain
end

#dry_run?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/far/replacer.rb', line 51

def dry_run?
  !@options[:please].value || !@options[:replace].value
end

#filesObject



29
30
31
# File 'lib/far/replacer.rb', line 29

def files
  file_list.map { |file| File.new file, find }
end

#findObject



17
18
19
# File 'lib/far/replacer.rb', line 17

def find
  @substitution.split('/')[1]
end

#generate_change_reportObject



55
56
57
# File 'lib/far/replacer.rb', line 55

def generate_change_report
  change_report
end

#in_placeObject



33
34
35
36
# File 'lib/far/replacer.rb', line 33

def in_place
  return "" if dry_run?
  return "-i ''"
end

#redirectionObject



38
39
40
# File 'lib/far/replacer.rb', line 38

def redirection
  "> #{tmp_file}" if dry_run?
end

#replaceObject



21
22
23
# File 'lib/far/replacer.rb', line 21

def replace
  @substitution.split('/')[2]
end

#replace_file(file) ⇒ Object



63
64
65
66
67
68
69
# 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) ⇒ Object



42
43
44
45
# File 'lib/far/replacer.rb', line 42

def replaced_file(file)
  return tmp_file if dry_run?
  file
end

#run?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/far/replacer.rb', line 47

def run?
  !dry_run?
end