collectiveidea / interactor

Interactor provides a common interface for performing complex user interactions.
MIT License
3.36k stars 212 forks source link

Add Interactor Requirements #113

Closed yjukaku closed 7 years ago

yjukaku commented 8 years ago

This adds a very, very simple contract DSL on top of Interactors. I know that #112 and #82 both exist, however I believe that neither of them have gained traction/have been merged because they are either too complex, try to do too much, or break backwards compatibility.

This PR simply adds the class method context_requires, which can be used like so:

class SendEmail
  include Interactor
  context_requires :email
  # or like this:
  # context_requires :email, :user_id
  # or like this, with strictly typed params
  # context_requires :email, user_id: String
  # context_requires email: String, user_id: Integer, some_options: Hash
  def call
    UserMailer.welcome_email(context.email).deliver
  end  
end

Since this is a developer tool (not for users to see), this raises a new exception, Interactor::RequirementsNotMet if eg. email is nil or not specified when the interactor is called with one of the following messages:

SendEmail.call
# => InteractorRequirements::RequirementsNotMet: Context requires email, but it wasn't specified
SendEmail.call(email: nil)
# => InteractorRequirements::RequirementsNotMet: Context requires email, but it was nil

or with type requirements:

SendEmail.call(email: "some@example.com", user_id: 1.5)
# => InteractorRequirements::RequirementsNotMet: Context requires account_id to be a String, but it was a Float.

Credit to @Mingan for the code that this is based on.

Addresses #92 , #109

yjukaku commented 8 years ago

It looks like the CI build is failing because of a dependency issue that is also in master, btw 🙊

yjukaku commented 8 years ago

@laserlemon @jonstokes I see that you had a lengthy discussion about a similar proposal, befor eit was spinned off into troupe. Any thoughts on this PR?

samtgarson commented 8 years ago

Any news or opinions on this? I would love this light handed approach, it keeps the gem as un-opinionated and simple as it currently is, which is a plus in my book.

mingan commented 8 years ago

In any case, the Travis issue can be traced back to codeclimate-test-reporter and the solution is described in their readme:

gem "simplecov", "~> 0.11.2"
laserlemon commented 7 years ago

Closing in favor of #123.