README.md

Interactor::Schema

This gem was born out of frustration. In using Interactor, I often wouldn't know
what methods would be defined on it. By forcing a schema upfront, and also
requiring methods in Interactor classes, we bring order to
Interactor::Context.

Use with Organizers

Interactor::Schema works great with Interactor::Organizers!

classSaveUserincludeInteractor::SchemaincludeInteractor::Organizer
schema :name
organize ValidateInfo, SendMailerendclassValidateInfoincludeInteractordefcall# These will work, since name is in our schema
context.name
context.name ="Bob"# These will fail with NoMethodErrors since age is not in our schema
context.age
context.age =28endend

The initial schema will be propagated through the entire chain, meaning no
attributes outside of name can be set. This is particularly helpful for
maintaining clarity of what information is being consumed by a chain of
Interactors.

Multiple Schemas

In the case of multiple schemas being provided only the first one will be used
(others will be discarded). The idea is that you should know upfront what
attributes will be needed.

Modifying the schema in the middle of the chain could result in confusing
behavior.

Interactor Helper Methods

This gem comes with two helper methods - one that should help enforce context
cleanliness and one to delegate to the context.

require_in_context

Interactor::require_in_context ensures that the attributes provided as
arguments are present and not nil? when the service is called.

classSaveUserincludeInteractor
require_in_context :nameend

This will fail:

SaveUser.call
# => ArgumentError: Missing the following attributes in context: name

delegate_to_context

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.