Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tonytonyjan/interactor2
Small, simple and dependency-free service object.
https://github.com/tonytonyjan/interactor2
Last synced: about 1 month ago
JSON representation
Small, simple and dependency-free service object.
- Host: GitHub
- URL: https://github.com/tonytonyjan/interactor2
- Owner: tonytonyjan
- Created: 2016-07-21T08:54:43.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-07-31T14:04:41.000Z (over 8 years ago)
- Last Synced: 2024-11-13T21:46:53.455Z (about 1 month ago)
- Language: Ruby
- Size: 2.93 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/tonytonyjan/interactor2.svg?branch=master)](https://travis-ci.org/tonytonyjan/interactor2)
# 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
```ruby
require 'interactor2'
class AddToCart < Interactor2
attr_reader :line_item, :cart # should be any attribute you want to exposedef 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
``````ruby
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
```