Skip to content

Instantly share code, notes, and snippets.

@johana-star
Forked from southpolesteve/gist:5060497
Last active December 14, 2015 09:09
Show Gist options
  • Save johana-star/5062975 to your computer and use it in GitHub Desktop.
Save johana-star/5062975 to your computer and use it in GitHub Desktop.
I tried to simplify this, but man, I don't know if I can crush down these conditionals and enumerables.
namespace :cleaner do
task :find do
models = Dir.glob("app/models/**/*")
methods = %w[]
models.each do |model|
if File.file?(model)
File.open(model).lines.each do |line|
if method = method_name? line
methods << method
end
end
end
end
all_files = Dir.glob("app/**/*")
methods.each do |method|
use_count = 0
all_files.each do |file|
if File.file?(file)
begin # Hack to handle binary files.
file_count = File.readlines(file).grep(Regexp.new(method)).size
rescue
use_count += file_count unless file_count.nil?
end
puts "#{method} is a candidate for deletion" if use_count == 1
end
end
end
end
protected
def method_name?(line)
if line.match(/def ([a-z0-9._?!]*)/)
line.match(/def (?:self.)?([a-z0-9._?!]*)/)[1]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment