Skip to content

Instantly share code, notes, and snippets.

@Widdershin
Last active August 29, 2015 14:05
Show Gist options
  • Save Widdershin/de3038305133ef2da8bf to your computer and use it in GitHub Desktop.
Save Widdershin/de3038305133ef2da8bf to your computer and use it in GitHub Desktop.
A simple utility for moving and renaming Ruby/Rails app and spec code with one command.
#!/usr/bin/env ruby
require 'fileutils'
class Mvrb
CODE_EXTENSION = '.rb'
attr_reader :old_path, :new_path
def initialize(old_path, new_path)
@old_path = old_path
@new_path = new_path
end
def move
move_code_files
rename_class_in_files
end
private
def move_code_files
move_file(old_app_path, new_app_path)
move_file(old_spec_path, new_spec_path)
end
def rename_class_in_files
rename_class_in_file(new_app_path)
rename_class_in_file(new_spec_path)
end
def rename_class_in_file(filename)
old_contents = File.read(filename)
new_contents = old_contents.gsub(old_classname, new_classname)
File.open(filename, 'w') { |file| file.write new_contents }
end
def move_file(origin, destination)
FileUtils.mv(origin, destination)
end
def old_classname
classname_from_path(old_path)
end
def new_classname
classname_from_path(new_path)
end
def classname_from_path(path)
filename = File.basename(path, CODE_EXTENSION)
titleize filename
end
def titleize string
string.split("_").map(&:capitalize).join
end
def app_path(path)
"app/#{path}.rb"
end
def spec_path(path)
"spec/#{path}_spec.rb"
end
def old_app_path
app_path @old_path
end
def new_app_path
app_path @new_path
end
def old_spec_path
spec_path @old_path
end
def new_spec_path
spec_path @new_path
end
end
Mvrb.new(*ARGV).move
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment