DEV: Stop injecting a service result object in the caller object

Currently, when calling a service with its block form, a `#result`
method is automatically created on the caller object. Even if it never
clashed so far, this could happen.

This patch removes that method, and instead use a more classical way of
doing things: the result object is now provided as an argument to the
main block. This means if we need to access the result object in an
outcome block, it will be done like this from now on:
```ruby
MyService.call(params) do |result|
  on_success do
    # do something with the result object
    do_something(result)
  end
end
```

In the same vein, this patch introduces the ability to match keys from
the result object in the outcome blocks, like we already do with step
definitions in a service. For example:
```ruby
on_success do |model:, contract:|
  do_something(model, contract)
end
```
Instead of
```ruby
on_success do
  do_something(result.model, result.contract)
end
```
This commit is contained in:
Loïc Guitaut
2024-10-21 15:37:02 +02:00
committed by Loïc Guitaut
parent 07ff21d045
commit f79dd5c8b5
25 changed files with 168 additions and 188 deletions

View File

@ -90,7 +90,7 @@ module ChatSDK
def stop_stream(message_id:, guardian:)
Chat::StopMessageStreaming.call(message_id:, guardian:) do
on_success { result.message }
on_success { |message:| message }
on_model_not_found(:message) { raise "Couldn't find message with id: `#{message_id}`" }
on_model_not_found(:membership) do
raise "Couldn't find membership for user with id: `#{guardian.user.id}`"
@ -145,7 +145,7 @@ module ChatSDK
raise "User with id: `#{guardian.user.id}` can't join this channel"
end
on_failed_contract { |contract| raise contract.errors.full_messages.join(", ") }
on_success { result.message_instance }
on_success { |message_instance:| message_instance }
on_failure { raise "Unexpected error" }
end