Skip to content

samesystem/same-interactor

Repository files navigation

Same::Interactor

Build Status codecov Documentation

Service / Interactor on steroids.

Full Documentation

Check https://samesystem.github.io/same-interactor for more details

Installation

Add this line to your application's Gemfile:

gem 'same-interactor'

And then execute:

$ bundle

Or install it yourself as:

$ gem install same-interactor

Getting started

This Same::Interactor is build on top of Interactor gem. So everything what works with Interactor should work with Same::Interactor too. Same::Interactor just adds few additional features:

  • context_attr which allows to define required context attributes or attributes with defaults
  • validations which fails interactor before executing call method when validations fails. Also it returns validation errors.

Usage

class MyService
  include Same::Interactor

  validates :my_positive_number, numericality: { greater_than: 0 }

  context_attr :my_attr
  context_attr :my_optional_attr, optional: true
  context_attr :my_attr_with_default, default: -> { my_attr }
  context_attr :my_positive_number

  def call
    context.received_my_attr_with_default = my_attr_with_default
  end
end

# validations:
result = MyService.call(my_attr: 'foo', my_positive_number: -1000)
result.success? # => false
result.errors.full_messages # => ["My positive number must be greater than 0"]

# using defaults:
result = MyService.call(my_attr: 'foo', my_positive_number: 1)
result.received_my_attr_with_default # => "foo"
result = MyService.call(my_attr: 'foo', my_positive_number: 1, my_attr_with_default: 'bar')
result.received_my_attr_with_default # => "bar"

# missing required context_attr
MyService.call(my_positive_number: 1) # => raises Same::Interactor::MissingAttributeError

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.

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/samesystem/same-interactor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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

Code of Conduct

Everyone interacting in the Same::Interactor project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

No description, website, or topics provided.

Resources

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published