Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add .parameters and .parameters! methods for input parameters expectations on call #112

Closed
wants to merge 2 commits into from

Conversation

daniilsunyaev
Copy link

Сurrently, you can never be sure, what parameters will be specified in the context on call. I need to grep for call invocations across the project or look through the entire interactor carefully for context.foo things to understand what are the input variables.
We use interactors quite often and now have an internal convention to declare parameters like this:

class Out
  include Interactor

  delegate :message, to: :context

  def call
    puts message
  end
end

Out.call(message: "Hi!")
Hi!
=> #<Interactor::Context message="Hi!">

thus, it is very easy to see what do we need to specify to make this interactor working without deep understanding of its contents.
I think that this is a good practice, and perhaps there should be a method to do this.

This PR introduces two new methods:
.parameters - simply defines specified delegators to the context. So, previous example would look like this:

class Out
  include Interactor

  parameters :message

  def call
    puts message
  end
end

Out.call(message: "Hi!")
Hi!
=> #<Interactor::Context message="Hi!">

.parameters! - does essentially the same, but adds a before hook that will fail the context if any of specified params is missing.

class Baz
  include Interactor

  parameters! :foo, :bar

  def call
    puts "#{foo} #{bar}"
  end
end

Baz.call(foo: "hello", bar: "world")
hello world
=> #<Interactor::Context foo="hello", bar="world">
Baz.call!(foo: "hello")
=> #<Interactor::Failure: #<Interactor::Context foo="hello">>

I see that there are some related issues (#92, #67, #109) and even a PR (#82) which evolved to a separate gem (https://github.com/jonstokes/troupe).
To be honest, I was lazy enough not to read all of those threads from beginning to end 😄. So, maybe this solution has been already discussed and rejected for some reasons.

I'm not sure about the name of this methods, but it looks as a very simple solution that will stick well with lightness if interactors.

@daniilsunyaev
Copy link
Author

It looks like this build (as well as current master) is failing due to simplecov 0.12 update.
It's trying to install json 2.0.1, which is not compatible with ruby 1.9.3.
simplecov-ruby/simplecov#511

@artofhuman
Copy link
Contributor

@laserlemon Any chance to merge it?

@laserlemon
Copy link
Collaborator

Closing in favor of #123.

@laserlemon laserlemon closed this Mar 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants