0.0
No commit activity in last 3 years
No release in over 3 years
Small, simple and dependency-free service object.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 5.9
~> 11.2
~> 0.41
 Project Readme

Build Status

Install

gem install interactor2

API

Method Summary
Interactor2.perform ::new and then #perform, all arguments will be passed to #initialize.
Interactor2#fail! Fail the interactor.
Interactor2#perform Must be overridden in child class, and should not call it directly, use ::perform instead.
Interactor2#error Returns the error message.
Interactor2#success? Returns true if there is no error.

Usage

require 'interactor2'
class AddToCart < Interactor2
  attr_reader :line_item, :cart # should be any attribute you want to expose

  def initialize(product, cart)
    @cart = cart
    @line_item = cart.line_items.new(product: product)
  end

  # business logic here.
  def perform
    unless @line_item.save
      fail! 'oops'
    end
  end
end
  add_to_cart = AddToCart.perform(@product)
  if add_to_cart.success?
    add_to_cart.item
    add_to_cart.cart
  else
    add_to_cart.error # => 'oops'
  end