Last active
September 8, 2021 01:30
-
-
Save willnet/cf8229980b876e5a6c0b627686e5f19a to your computer and use it in GitHub Desktop.
ARのオブジェクトを生成しているところがどこか探すコード
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
class ApplicationRecord < ActiveRecord::Base | |
@ar_initialize_counter = Hash.new(0) | |
@ar_find_counter = Hash.new(0) | |
after_initialize :count_initialize | |
def count_initialize | |
return unless ApplicationRecord.enable_counter? | |
gems_paths = (Gem.path | [Gem.default_dir]).map { |p| Regexp.escape(p) } | |
caller_in_app = caller.find { |file_and_lineno| file_and_lineno !~ gems_paths} | |
ApplicationRecord.instance_variable_get(:@ar_initialize_counter)[caller_in_app] += 1 | |
end | |
after_find :count_find | |
def count_find | |
return unless ApplicationRecord.enable_counter? | |
gems_paths = (Gem.path | [Gem.default_dir]).map { |p| Regexp.escape(p) } | |
caller_in_app = caller.find { |file_and_lineno| !gems_paths.any? { |gem_path| file_and_lineno.match?(gem_path) } } | |
ApplicationRecord.instance_variable_get(:@ar_find_counter)[caller_in_app] += 1 | |
end | |
def self.enable_counter? | |
@enable_counter | |
end | |
def self.enable_counter | |
@enable_counter = true | |
end | |
def self.disable_counter | |
@enable_counter = false | |
end | |
def self.log_counter | |
File.open('hoge.log', 'w') do |file| | |
file.puts 'ar_initialize_counter' | |
file.write @ar_initialize_counter.inspect | |
file.puts 'ar_find_counter' | |
file.write @ar_find_counter.inspect | |
end | |
end | |
def self.reset_counter | |
@ar_initialize_counter = Hash.new(0) | |
@ar_find_counter = Hash.new(0) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment