Skip to content

Commit

Permalink
API-34439 add breakpoint dev tool
Browse files Browse the repository at this point in the history
  • Loading branch information
nihil2501 committed Apr 15, 2024
1 parent f72cca0 commit 9c6de47
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ config/initializers/backtrace_silencers.rb @department-of-veterans-affairs/va-ap
config/initializers/betamocks.rb @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
config/initializers/bgs.rb @department-of-veterans-affairs/Benefits-Team-1 @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
config/initializers/breakers.rb @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
config/initializers/breakpoint.rb @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
config/initializers/clamscan.rb @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
config/initializers/config.rb @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
config/initializers/cookie_rotation.rb @department-of-veterans-affairs/va-api-engineers @department-of-veterans-affairs/backend-review-group
Expand Down
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ RSpec/SubjectStub:
Rails/FilePath:
EnforcedStyle: arguments

Lint/Debugger:
DebuggerMethods:
Breakpoint:
- Breakpoint.at_receiver
- Breakpoint.at_owner

# This one doesn’t always make sense, it’s mostly the swagger,
# factories and pact provider states — which need a tear_down block regardless
Lint/EmptyBlock:
Expand Down
44 changes: 44 additions & 0 deletions config/initializers/breakpoint.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

# Only load `Breakpoint` when its only dependency, `Pry`, is loaded. If
# `Breakpoint` is ever deemed to have been loaded inappropriately, then the
# claim ought to instead be that `Pry` was loaded inappropriately. That's what
# we're expressing here.
return unless defined?(Pry)

module Breakpoint
class << self
# Add a breakpoint at the top of an instance's method:
# `Breakpoint.at_receiver(some_instance, :some_instance_method)`
#
# Add a breakpoint at the top of a class's method:
# `Breakpoint.at_receiver(SomeClass, :some_class_method)`
def at_receiver(receiver, name)
at_owner(receiver.singleton_class, name)
end

# Add a breakpoint at the top of a method for all of a class's instances:
# `Breakpoint.at_owner(SomeClass, :some_instance_method)`
def at_owner(owner, name)
owner.is_a?(Module) or
raise ArgumentError, 'not an owner'

mod =
Module.new do
define_method(name) do |*args, **kwargs, &block|
# Now you have access to all the above arguments, plus whatever else
# is available in the current environment.
binding.pry # rubocop:disable Lint/Debugger
super(*args, **kwargs, &block)
end
end

owner.prepend(mod)
end
end
end

# Install breakpoints wherever you want if you need to investigate a particular
# instance during the execution of something, or even right here if you already
# have a handle on the thing you care about, like a class, e.g.:
# Breakpoint.at_receiver(Blueprinter::Base, :prepare_data)

0 comments on commit 9c6de47

Please sign in to comment.