zorbash / opus

A framework for pluggable business logic components
MIT License
354 stars 21 forks source link

Add a :unless param for stages #13

Closed rafaels88 closed 5 years ago

rafaels88 commented 5 years ago

Proposal

Add the ability to pass a :unless parameter for all the stages where it is possible to pass a :if one.

The idea is that you don't need to name your functions like not_* in order to add a new stage to the pipeline.

Context

Imagine we can perform a step only if the user is allowed.

defmodule Pipeline do
  use Opus.Pipeline

  step :do_this, if: :user_allowed
end

Now, if we want to perform another step in case the user is not allowed, we would end up with something like:

defmodule Pipeline do
  use Opus.Pipeline

  step :do_this, if: :user_allowed
  step :do_another, if: :user_not_allowed
end

Which makes us implement two different functions (user_not_allowed and user_allowed) returning the opposite of the other. This would be simplified if we could do:

defmodule Pipeline do
  use Opus.Pipeline

  step :do_this, if: :user_allowed
  step :do_another, unless: :user_allowed
end
zorbash commented 5 years ago

You could do:

defmodule Pipeline do
  use Opus.Pipeline

  step :do_this, if: :user_allowed
  step :do_another, if: &(!__MODULE__.user_allowed(&1))
end

But I can totally see unless making the code more readable in your examples.