Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #16 from st0012/add-and_print-method
Browse files Browse the repository at this point in the history
Add TappingDevice#and_print method
  • Loading branch information
st0012 committed Dec 25, 2019
2 parents 059f5d3 + 1bcb559 commit 2dab65e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
20 changes: 5 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ class PostsController < ApplicationController

def show
@post = Post.find(params[:id])
tap_on!(@post) do |payload|
puts(payload.method_name_and_location)
end
tap_on!(@post).and_print(:method_name_and_location)
end
end
```
Expand All @@ -51,9 +49,7 @@ to_param FROM /RUBY_PATH/gems/2.6.0/gems/actionpack-5.2.0/lib/action_dispatch/ro
Or you can use `tap_assoc!`. This is very useful for tracking potential n+1 query calls, here’s a sample from my work

```ruby
tap_assoc!(order) do |payload|
puts(payload.method_name_and_location)
end
tap_assoc!(order).and_print(:method_name_and_location)
```

```
Expand Down Expand Up @@ -193,9 +189,7 @@ initialize @ Student
- `filter_by_paths: [/path/]` - only contain calls from the specified paths

```ruby
tap_on!(@post, exclude_by_paths: [/active_record/]) do |payload|
puts(payload.method_name_and_location)
end
tap_on!(@post, exclude_by_paths: [/active_record/]).and_print(:method_name_and_location)
```

```
Expand Down Expand Up @@ -236,9 +230,7 @@ class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]

def show
tap_on!(@post) do |payload|
puts(payload.method_name_and_location)
end
tap_on!(@post).and_print(:method_name_and_location)
end
end
```
Expand Down Expand Up @@ -287,9 +279,7 @@ Passed as 'record_or_hash_or_array' in method ':polymorphic_method'
### `tap_assoc!`

```ruby
tap_assoc!(order) do |payload|
puts(payload.method_name_and_location)
end
tap_assoc!(order).and_print(:method_name_and_location)
```

```
Expand Down
7 changes: 7 additions & 0 deletions lib/tapping_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class TappingDevice

def initialize(options = {}, &block)
@block = block
@output_block = nil
@options = process_options(options)
@calls = []
@disabled = false
Expand All @@ -45,6 +46,10 @@ def tap_assoc!(record)
track(record, condition: :tap_associations?)
end

def and_print(payload_method)
@output_block = -> (payload) { puts(payload.send(payload_method)) }
end

def set_block(&block)
@block = block
end
Expand Down Expand Up @@ -208,6 +213,8 @@ def is_from_target?(object, tp)
def record_call!(payload)
return if @disabled

@output_block.call(payload) if @output_block

if @block
root_device.calls << @block.call(payload)
else
Expand Down
13 changes: 13 additions & 0 deletions spec/tapping_device_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,19 @@ def foo(obj)
end
end

describe "#and_print" do
let(:device) { described_class.new }

it "outputs payload with given payload method" do
stan = Student.new("Stan", 18)
device.tap_on!(stan).and_print(:method_name_and_arguments)

expect do
stan.name
end.to output("name <= {}\n").to_stdout
end
end

describe ".devices" do
it "stores all initialized devices" do
device_1 = described_class.new
Expand Down

0 comments on commit 2dab65e

Please sign in to comment.