Add #tap to Hanami::Utils::BasicObject
cllns opened this issue · comments
I tried to simulate an error on a Hanami::Interactor::Result
(which is a Hanami::Utils::BasicObject
):
Hanami::Interactor::Result.new.tap do |result|
result.add_error("DB problem, sorry!")
end
and I got: NoMethodError: undefined method 'tap' for #<Hanami::Interactor::Result:0x00007fc90839f9a8>
The implementation for #tap
on Hanami::Utils::BasicObject
should be as simple as:
def tap
yield self
self
end
(From this blog post: http://seejohncode.com/2012/01/02/ruby-tap-that/).
I know we want to keep Hanami::Utils::BasicObject
as minimal as possible, but this is a method that'd be useful to have. If we can't implement it on Hanami::Utils::BasicObject
, then maybe we could add it to Hanami::Interactor::Result
instead.
Thoughts @hanami/core ?
I've never understood tap, it has the same lines than the version assigning a variable and with an extra indentation, but that's my personal opinion :P
I think we could add it, it is a very easy method.
absolutely agree with @AlfonsoUceda (with tap
and adding method :D )
@cllns WDYT if we made it as good for first PR
issue?
@davydovanton Good idea, that's easy
right? I just added that. (I feel like that label should be simple
instead of easy
😉)
I wonder if the better move here is to make Hanami::Interactor::Result
not inherit from Hanami::Utils::Basic
object, and leave it as the default (::Object
)?
There are many other methods people could want to send to an Interactor's result:
https://ruby-doc.org/core-2.4.1/Object.html
It's not an object that's created many times, so I don't think the performance hit of using ::Object
vs Hanami::Utils::BasicObject
will be noticeable.
Hi! I am pretty new to ruby, but want to fix this, is it ok? Already checked out contributing guide. Thanks!
Whoops, I should be a little faster...
Are there any places where help needed? Maybe tests for hanami itself?
@cllns Please have a look at: #254 (comment)
TL;DR: BasicObject
reduces the clashes between Ruby default methods and result payload keys. The more we add methods, the more there is the chance of clashes.
You can solve the problem with:
result = Hanami::Interactor::Result.new
result.add_error("DB problem, sorry!")
result
I vote against this change 👎.
Thanks for the explanation. Agreed!