GithubHelp home page GithubHelp logo

jonatasdaniel / interaptor Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 2.0 30 KB

Easy Interactor gem for ruby apps

License: MIT License

Ruby 95.34% Shell 1.21% Dockerfile 3.45%
ruby interactor clean-architecture

interaptor's Introduction

Interaptor

Build Status

Installation

Add this line to your application's Gemfile:

gem 'interaptor'

And then execute:

$ bundle

Or install it yourself as:

$ gem install interaptor

Why?

Interactor is a single purpose object used to encapsulate business logic of the application. Inspired by collectiveidea/interactor.

The reason behind creating another Interactor Ruby gem is: I would like to have an object that I could instantiate and pass parameters in, like a PORO object. I'm not a big fan of calling my business objects using class methods.

Usage

  • create your interactor class
class CreateBankAccount
  include Interaptor

  def initialize(current_user)
    @current_user = current_user
  end

  def execute(name:, account_number:, routing_number:)
    # do something
    bank_account = BankAccount.create!(
      name: name, account_number: account_number, routing_number: routing_number
    )

    return bank_account
  end
end

and call it

result = CreateBankAccount.new(my_logged_user).call(**bank_account_params)
if result.success?
  result.value # value is the object returned in your interactor, in this case, bank_account
else
  result.errors.each do |error|
    puts "Some error happened related to #{error.source}. Detail: #{error.message}"
  end
end

or you can call! expecting an exception if some error happens

begin
  bank_account = CreateBankAccount.new(my_logged_user).call!(**bank_account_params)
rescue Interaptor::Failure => e
  e.errors.each do |error|
    puts "Some error happened related to #{error.source}. Detail: #{error.message}"
  end
end

To add errors inside your interactor class you have three ways:

  • add error and keep with your interactor processing (keep in mind that this option will not raise an exception when calling call! method)
class CreateBankAccount
  include Interaptor

  def execute
    add_error('Some error', source: 'optional source')

    #do something
  end

end
  • add error and stop processing
class CreateBankAccount
  include Interaptor

  def execute
    fail!('Some error', source: 'optional source')

    #nothing more will be executed here
  end

end
  • add multiple errors and stop processing
class CreateBankAccount
  include Interaptor

  def execute
    add_error('Some error', source: 'optional source')
    add_error('Some error again', source: 'optional source')
    fail!

    #nothing more will be executed here
  end

end

You also have the option to add before/after callbacks like:

class CreateBankAccount
  include Interaptor

  before do
    # execute something before execute method
  end

  after do
    # execute something after execute method if no exception is thrown
  end

  def execute
    #something here
  end

end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/jonatasdaniel/interaptor. 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.

To run tests: docker-compose up

To release a new version: docker-compose run --rm -v ~/.gitconfig:/root/.gitconfig -v ~/.ssh/:/root/.ssh/ -v ~/.gem/:/root/.gem/ -v $(pwd):/interaptor/ -w /interaptor interaptor rake release

License

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

Code of Conduct

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

interaptor's People

Contributors

jonatasdaniel avatar rfdlp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

duderamos rfdlp

interaptor's Issues

Create sequencial interactors

Open to discuss how to create a feature to have sequencial interactors.

E.G.:
CheckoutOrder has CreateCreditCard, PayOrder and AddToShipQueue as sequencial interactors. If some of these fail, the entire process stops and in some cases, we need rollback.

Points to decide:

  • decide when the sequencial will be called (explicit or implicit)
  • find a way to pass constructor params to interactor objects
  • find a way how to rollback the interactor process (maybe have explicit commit and rollback hooks on each interactor)

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.