Skip to content

Instantly share code, notes, and snippets.

@Widdershin
Created August 25, 2014 21:57
Show Gist options
  • Save Widdershin/9a4de0054709b4d8737f to your computer and use it in GitHub Desktop.
Save Widdershin/9a4de0054709b4d8737f 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 python
"""
mvrb
A simple utility to move and rename Rails app and spec code
Usage:
mvrb services/widget_extractor concepts/widget_concatenator
Would move app/services/widget_extractor.rb to app/concepts/widget_concatenator.
Would also move spec/services/widget_extractor_spec.rb and rename classes.
"""
import os
import shutil
import sys
APP_PATH = "app/{path}.rb".format
SPEC_PATH = "spec/{path}_spec.rb".format
def mvrb(old_path, new_path):
move_code_to_new_path(old_path, new_path)
replace_classnames(old_path, new_path)
def move_code_to_new_path(old_path, new_path):
def move_file(old_full_path, new_full_path):
shutil.move(old_full_path, new_full_path)
move_file(APP_PATH(path=old_path), APP_PATH(path=new_path))
move_file(SPEC_PATH(path=old_path), SPEC_PATH(path=new_path))
def replace_classnames(old_path, new_path):
old_classname = classname_from_path(old_path)
new_classname = classname_from_path(new_path)
rename_classname(old_classname, new_classname, APP_PATH(path=new_path))
rename_classname(old_classname, new_classname, SPEC_PATH(path=new_path))
def classname_from_path(path):
_, filename = os.path.split(path)
return filename.title().replace('_', '')
def rename_classname(old_classname, new_classname, path):
with open(path) as open_file:
old_contents = open_file.read()
new_contents = old_contents.replace(old_classname, new_classname)
with open(path, 'w') as open_file:
open_file.write(new_contents)
if __name__ == '__main__':
old_path, new_path = sys.argv[1:3]
mvrb(old_path, new_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment