Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FileOrganizer
- def initialize(roots, home, debug=false)
- @roots = roots
- @home = home
- @debug = debug
- end
- def organize_by_class_lineage(klass, current_path, lineage=[])
- if @roots.include? klass
- new_dir = File.join(@home, *lineage[0...-1])
- new_path = File.join(new_dir, File.basename(current_path))
- mkdir_p new_dir
- mv current_path, new_path
- else
- organize_by_class_lineage(klass.superclass, current_path, lineage.unshift(klass.to_s.underscore))
- end
- end
- protected
- def mkdir_p(path)
- @debug ? puts(" mkdir -p #{path}") : `mkdir -p #{path}`
- end
- def mv(a,b)
- @debug ? puts(" mv #{a} -> #{b}") : `mv #{a} #{b}`
- end
- end
- if $0 == __FILE__
- # Organize all models by their class lineage
- organizer = FileOrganizer.new([ActiveRecord::Base, ActiveRecord::BaseWithoutTable, Object], "#{Rails.root}/app/models", true)
- for path in Dir["#{Rails.root}/app/models/**/*"]
- next unless File.basename(path) =~ /(.*).rb$/
- begin
- puts "Doing: #{model = $1.camelcase}"
- organizer.organize_by_class_lineage(eval(model), path)
- rescue LoadError => e
- puts "LOAD ERROR: #{path}"
- end
- end
- end
Add Comment
Please, Sign In to add comment