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 #56 from st0012/with-heleprs
Browse files Browse the repository at this point in the history
Add with_* helpers (e.g. with_print_calls)
  • Loading branch information
st0012 committed Jun 28, 2020
2 parents f452073 + 7a5c49d commit b33d09d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,30 @@ write_calls(object, log_file: "/tmp/another_file")
```


### Use `with_HELPER` for chained method calls

One thing that really bothers me when debugging is to break method chains from time to time. Let's say I call a service object like this:

```ruby
SomeService.new(params).perform
```

In order to debug it, I'll need to break the method chain into

```ruby
service = SomeService.new(params)
print_calls(service, options)
service.perform
```

That's a 3-line change! Which obviously violates the goal of `tapping_device` - making debugging easier with 1 line of code.

So here's another option, just insert a `with_HELPER_NAME` call in between:

```ruby
SomeService.new(params).with_print_calls(options).perform
```

## Installation
Add this line to your application's Gemfile:

Expand Down
9 changes: 8 additions & 1 deletion lib/tapping_device/trackable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ def tap_mutation!(object, options = {}, &block)

[:calls, :traces, :mutations].each do |subject|
[:print, :write].each do |output_action|
define_method "#{output_action}_#{subject}" do |target, options = {}|
helper_method_name = "#{output_action}_#{subject}"

define_method helper_method_name do |target, options = {}|
send("output_#{subject}", target, options, output_action: "and_#{output_action}")
end

define_method "with_#{helper_method_name}" do |options = {}|
send(helper_method_name, self, options)
self
end
end
end

Expand Down
40 changes: 36 additions & 4 deletions spec/trackable/output_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,31 @@ def produce_expected_output(expected_output)
let(:tap_action) { send(helper_method, service, colorize: false) }

include_context "order creation"
it_behaves_like "output calls examples"
it_behaves_like "output calls examples" do
describe "with_print_calls" do
it "prints out calls" do
expect do
service.with_print_calls(colorize: false).perform(cart)
end.to produce_expected_output(expected_output)
end
end
end
end

describe "#print_traces" do
let(:helper_method) { "print_traces" }
let(:tap_action) { send(helper_method, cart, colorize: false) }

include_context "order creation"
it_behaves_like "output traces examples"
it_behaves_like "output traces examples" do
describe "with_print_traces" do
it "prints out traces" do
expect do
service.perform(cart.with_print_traces(colorize: false))
end.to produce_expected_output(expected_output)
end
end
end
end

describe "#print_mutations" do
Expand All @@ -210,15 +226,31 @@ def produce_expected_output(log_file = output_log_file, expected_output)
let(:tap_action) { send(helper_method, service, colorize: false) }

include_context "order creation"
it_behaves_like "output calls examples"
it_behaves_like "output calls examples" do
describe "with_write_calls" do
it "prints out calls" do
expect do
service.with_write_calls(colorize: false).perform(cart)
end.to produce_expected_output(expected_output)
end
end
end
end

describe "#write_traces" do
let(:helper_method) { "write_traces" }
let(:tap_action) { send(helper_method, cart, colorize: false) }

include_context "order creation"
it_behaves_like "output traces examples"
it_behaves_like "output traces examples" do
describe "with_write_traces" do
it "prints out traces" do
expect do
service.perform(cart.with_write_traces(colorize: false))
end.to produce_expected_output(expected_output)
end
end
end
end

describe "#write_mutations" do
Expand Down

0 comments on commit b33d09d

Please sign in to comment.