Created
March 13, 2019 06:59
-
-
Save yalab/5f6aab1190562c381b9917663d5eccfe to your computer and use it in GitHub Desktop.
find method that write by copy and paste.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# USAGE find_copy_and_paste_method.rb MOD_NAME | |
Rails.application.eager_load! | |
base_class = ARGV[0].constantize | |
def all_subclasses(klasses) | |
if klasses == [] | |
klasses | |
else | |
klasses.map{|k| all_subclasses(k.subclasses) << klasses }.flatten | |
end | |
end | |
classes = all_subclasses([base_class]).uniq | |
pry = Pry.new | |
opts = OpenStruct.new('dconstants?': false, 'instance-methods': true) | |
all_methods = {} | |
URL_HELPERS = Rails.application.routes.url_helpers | |
IGNORE_CLASSES = [Kernel, BasicObject, ActiveSupport, ActionView, ActionDispatch, Object, Tapp, PP].map(&:name).to_set.freeze | |
classes.each do |klass| | |
p klass | |
m = Pry::Command::Ls::Methods.new(klass, {}, opts, pry) | |
m.send(:all_methods).each_with_object(all_methods) do |method, hash| | |
owner = method.owner | |
next if IGNORE_CLASSES.include?(owner.name.to_s.split(/::/).first) | |
source = begin | |
method.source | |
rescue Pry::CommandError | |
next | |
end | |
method_name = method.name | |
next if method_name.match?(/_(path|url)\Z/) | |
hash[method_name] ||= {} | |
hash[method_name][owner] ||= [] | |
hash[method_name][owner] << Digest::SHA1.hexdigest(source.gsub(/\s/, '')) | |
hash[method_name][owner].uniq! | |
end | |
end | |
puts "### find duplicate methdo ###" | |
all_methods.each do |method_name, classes| | |
classes.group_by{|k, v| v }.select{|k, v| v.length > 1 }.each do |hash, duplicate| | |
p "##{method_name}: " + duplicate.map(&:first).join(" ") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment