class Recode::Runner

Attributes

extensions[RW]
handler[RW]
new_string[RW]
old_string[RW]
path[RW]

Public Class Methods

new(path: nil, extensions: nil, old_string: nil, new_string: nil) click to toggle source
# File lib/recode/runner.rb, line 9
def initialize(path: nil, extensions: nil, old_string: nil, new_string: nil)
  @path, @extensions = path, extensions
  @old_string, @new_string = old_string, new_string
end

Public Instance Methods

execute(handler) click to toggle source
# File lib/recode/runner.rb, line 14
def execute(handler)
  @handler = handler
  refactor_filenames
  refactor_contents
end

Private Instance Methods

apply_changes(string) click to toggle source
# File lib/recode/runner.rb, line 44
def apply_changes(string)
  result = string.dup
  changes.each { |from, to| result.gsub!(from, to) }
  result
end
changes() click to toggle source
# File lib/recode/runner.rb, line 50
def changes
  @changes ||= change_variations old_string, new_string
end
files() click to toggle source
# File lib/recode/runner.rb, line 54
def files
  @files ||= Dir["#{path}/**/*.{#{extensions.join ','}}"].sort
end
refactor_content(file) click to toggle source
# File lib/recode/runner.rb, line 37
def refactor_content(file)
  before = File.read file
  after = apply_changes before
  payload = { file: file, before: before, after: after }
  handler.edit **payload unless before == after
end
refactor_contents() click to toggle source
# File lib/recode/runner.rb, line 27
def refactor_contents
  files.each { |file| refactor_content file }
end
refactor_filename(file) click to toggle source
# File lib/recode/runner.rb, line 31
def refactor_filename(file)
  target = apply_changes file
  payload = { source: file, target: target }
  handler.rename **payload unless target == file
end
refactor_filenames() click to toggle source
# File lib/recode/runner.rb, line 22
def refactor_filenames
  files.each { |file| refactor_filename file }
  refresh_files
end
refresh_files() click to toggle source
# File lib/recode/runner.rb, line 58
def refresh_files
  @files = nil
end