actor: Invoking undefined methods on a result does not raise errors

please check following example.

# i assign result.institutions = ??? in this service.
result = RetrieveInstitutions.result(params)

if result.success?
    @institutions = result.institutions1 # But i never defined this method in above service, but when invoke it, not raise any error, just return nil, this is not acceptable?
end

Above result.institutions1 never raise any error, just return nil, which make result.success? almost no any means for this case.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 16 (11 by maintainers)

Most upvoted comments

Hi @sunny sorry wasn’t able to work on this due to sudden work changes. Happy to help contribute to the idea, but can’t code for this for the meantime.

Please have a go at it, that sounds like a good solution! 🙌🏻

Yep definitely, the result should continue to act as it is. Sorry got my thoughts mixed up with the other issue with the stubbed outputs, which we can solve by doing the one I mentioned above.

Going back to the original issue for invoking undefined methods, I would use a Hash to manage all the outputs and use method_missing and work around that.

Just a quick thought dump on this, I would imagine it looking like:

class Result
  attr_accessor :result_hash
  
  def method_missing
    # check if symbol is in result hash, else call super which should raise an error.
  end
end

I’m going to tinker around a bit if I can come up with something better.

@sunny Ive had this issue before when using OpenStruct with our own PORO Service class. I resolved this by running with a Struct with a field called result and place all output fields into that as a hash. This wouldnt be a direct solution as we dont want to remove the dynamic creation of methods as it will break existing code.

I’m wondering if it would make sense to add a separate defined field and put all output in a hash there. This should mirror dynamically added fields. It would also help in stubbing the service results as you can do a double_instance that new method.

Let me know what you think as I’m currently keen on using actor and help contribute

define methods on the result set as it is being passed through.

I thought this approach is better, anyway, i consider this is a big issue, i use service_actor instead of Interactor, and add input, one import purpose is avoid mistakes, but this can hidden mistakes