Giter Site home page Giter Site logo

interactor-contracts's Introduction

Interactor::Contracts

Build Status Code Climate Inline docs

Interactor::Contracts is an extension to the interactor gem that gives you the ability to specify the expectations (expected inputs) and assurances (expected outputs) of your interactors.

Installation

Add this line to your application's Gemfile:

gem 'interactor-contracts'

And then execute:

$ bundle

Or install it yourself as:

$ gem install interactor-contracts

Usage

Let's extend the sample AuthenticateUser from the Interactor examples with a contract that specifies its expectations and assurances.

class AuthenticateUser
  include Interactor
  include Interactor::Contracts

  expects do
    required(:email).filled
    required(:password).filled
  end

  assures do
    required(:user).filled
    required(:token).filled
  end

  on_breach do |breaches|
    context.fail!(breaches)
  end

  def call
    if user = User.authenticate(context.email, context.password)
      context.user = user
      context.token = user.secret_token
    else
      context.fail!(:message => "authenticate_user.failure")
    end
  end
end

The expects block defines the expectations: the expected attributes of the context prior to the interactor running, along with any predicates that further constrain the input.

The assures block defines the assurances: the expected attributes of the context after the interactor runs and successfully completes, along with any predicates the further constrain the output.

Because interactors can have transitive dependencies through the use of organizers, any other inputs or outputs are ignored from the perspective of the contract and are passed along to the outgoing (successful) context.

Both expects and assures wrap dry-validation, so you can use any predicates defined in it to describe the expected inputs and outputs of your interactor.

To hook into a failed expectation or assurance, you can use the on_breach method to defined a breach handler. It should take a 1-arity block that expects an array of Breach objects. These objects have a property attribute that will give you the key that's in breach of its contract. Breaches also have a messages attribute that gives the reasons that property is in breach.

By default, when an on_breach consequence is not specified, the contract will #fail! the Interactor::Context with the keys that are in breach and arrays of messages about what the breaches are.

For example, the above interactor acts as follows:

result = AuthenticateUser.call({})
#=> #<Interactor::Context email=["email is missing"], password=["password is missing"]>

result.failure?  #=> true

Development

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

When writing code, you can use the helper application Guard to automatically run tests and coverage tools whenever you modify and save a file. This helps to eliminate the tedium of running tests manually and reduces the change that you will accidentally forget to run the tests. To use Guard, run bundle exec guard.

Before committing code, run rake to check that the code conforms to the style guidelines of the project, that all of the tests are green (if you're writing a feature; if you're only submitting a failing test, then it does not have to pass!), and that the changes are sufficiently documented.

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.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/michaelherold/interactor-contracts.

Supported Ruby Versions

This library aims to support and is tested against the following Ruby versions:

  • Ruby 2.4
  • Ruby 2.5
  • Ruby 2.6
  • JRuby 9.1

If something doesn't work on one of these versions, it's a bug.

This library may inadvertently work (or seem to work) on other Ruby versions, however support will only be provided for the versions listed above.

If you would like this library to support another Ruby version or implementation, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.

Versioning

This library aims to adhere to Semantic Versioning 2.0.0. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, that version should be immediately yanked and/or a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions. As a result of this policy, you can (and should) specify a dependency on this gem using the Pessimistic Version Constraint with two digits of precision. For example:

spec.add_dependency "interactor-contracts", "~> 0.1"

License

The gem is available as open source under the terms of the MIT License.

interactor-contracts's People

Contributors

andrewaguiar avatar michaelherold avatar noelrocha avatar raykov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.