DriedInteraction
DriedInteraction is a simple gem that helps you write interactors with input params validation.
Installation
Add this line to your application's Gemfile:
gem 'dried_interaction'
And then execute:
$ bundle
Or install it yourself as:
$ gem install dried_interaction
Why it may be useful to you
- Keeps your business logic typed
- Validates interactor input data by default
- Includes incredible DRY stack gems by default for yours interactors
Usage
Setting up an interactor
For setting up an interactor you need to include DriedInteraction into your class. Then you can add contract for call method which will validate input params. By default interactor returns Dry::Matcher::ResultMatcher.
class PublishPost
include DriedInteraction
option :normalize_params, reader: :private, default: -> { PostParamsNormalize.new }
option :post_repo, reader: :private, default: -> { PostRepo }
option :notify, reader: :private, default: -> { PostNotify.new }
contract do
required(:user).filled(Dry.Types.Instance(User))
required(:params).hash do
required(:title).filled(:string)
required(:content).filled(:string)
optional(:public).filled(:bool)
end
end
def call(user:, params:)
normalized_params = yield normalize_params.call(params)
post = yield save_post(normalized_params)
notify.call(post)
Success(post)
end
private
def save_post(params)
post = post_repo.new(params)
post.save ? Success(post) : Failure('Error message')
end
end
Interactor calling
PublishPost.new.call(user, params) do |interactor|
interactor.success do |post|
# handle success
end
interactor.failure do |error|
# handle failure
end
end
Additional info
- You have to return a monad from interactor as result.
- Contract failure will be handled as exception.
Contributing
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request
License
The gem is available as open source under the terms of the MIT License.