Module: IsThisUsed::CruftTracker::ClassMethods
- Defined in:
- lib/is_this_used/cruft_tracker.rb
Instance Method Summary collapse
- #determine_method_type(method_name) ⇒ Object
- #is_this_used?(method_name, method_type: nil) ⇒ Boolean
- #target_method(method_name, method_type) ⇒ Object
Instance Method Details
#determine_method_type(method_name) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/is_this_used/cruft_tracker.rb', line 103 def determine_method_type(method_name) is_instance_method = (self.instance_methods + self.private_instance_methods).include?( method_name ) is_class_method = (self.methods + self.private_methods).include?(method_name) if is_instance_method && is_class_method raise AmbiguousMethodType.new(self.name, method_name) elsif is_instance_method INSTANCE_METHOD elsif is_class_method CLASS_METHOD else raise NoSuchMethod.new(self.name, method_name) end end |
#is_this_used?(method_name, method_type: nil) ⇒ Boolean
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/is_this_used/cruft_tracker.rb', line 59 def is_this_used?(method_name, method_type: nil) IsThisUsed::Util::LogSuppressor.suppress_logging do method_type ||= determine_method_type(method_name) target_method = target_method(method_name, method_type) potential_cruft = PotentialCruft.find_or_create_by( owner_name: self.name, method_name: method_name, method_type: method_type ) target_method.owner.define_method target_method.name do |*args| IsThisUsed::Util::LogSuppressor.suppress_logging do CruftTracker::Recorder.record_invocation(potential_cruft) end if method_type == INSTANCE_METHOD target_method.bind(self).call(*args) else target_method.call(*args) end end end rescue ActiveRecord::StatementInvalid => e raise unless e.cause.present? && e.cause.instance_of?(Mysql2::Error) Rails.logger.warn( 'There was an error recording potential cruft. Does the potential_crufts table exist?' ) rescue Mysql2::Error::ConnectionError, ActiveRecord::ConnectionNotEstablished Rails.logger.warn( 'There was an error recording potential cruft due to being unable to connect to the database. This may be a non-issue in cases where the database is intentionally not available.' ) end |
#target_method(method_name, method_type) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/is_this_used/cruft_tracker.rb', line 94 def target_method(method_name, method_type) case method_type when INSTANCE_METHOD self.instance_method(method_name) when CLASS_METHOD self.method(method_name) end end |