Guest User

Untitled

a guest
Jan 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. class FileOrganizer
  2.  
  3. def initialize(roots, home, debug=false)
  4. @roots = roots
  5. @home = home
  6. @debug = debug
  7. end
  8.  
  9. def organize_by_class_lineage(klass, current_path, lineage=[])
  10. if @roots.include? klass
  11. new_dir = File.join(@home, *lineage[0...-1])
  12. new_path = File.join(new_dir, File.basename(current_path))
  13. mkdir_p new_dir
  14. mv current_path, new_path
  15. else
  16. organize_by_class_lineage(klass.superclass, current_path, lineage.unshift(klass.to_s.underscore))
  17. end
  18. end
  19.  
  20. protected
  21.  
  22. def mkdir_p(path)
  23. @debug ? puts(" mkdir -p #{path}") : `mkdir -p #{path}`
  24. end
  25.  
  26. def mv(a,b)
  27. @debug ? puts(" mv #{a} -> #{b}") : `mv #{a} #{b}`
  28. end
  29.  
  30. end
  31.  
  32. if $0 == __FILE__
  33.  
  34. # Organize all models by their class lineage
  35.  
  36. organizer = FileOrganizer.new([ActiveRecord::Base, ActiveRecord::BaseWithoutTable, Object], "#{Rails.root}/app/models", true)
  37.  
  38. for path in Dir["#{Rails.root}/app/models/**/*"]
  39. next unless File.basename(path) =~ /(.*).rb$/
  40. begin
  41. puts "Doing: #{model = $1.camelcase}"
  42. organizer.organize_by_class_lineage(eval(model), path)
  43. rescue LoadError => e
  44. puts "LOAD ERROR: #{path}"
  45. end
  46. end
  47.  
  48. end
Add Comment
Please, Sign In to add comment