This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Zertico | ||
module Exceptions | ||
autoload :InteractorException, 'zertico/exceptions/interactor_exception' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Zertico | ||
module Exceptions | ||
class InteractorException < Exception | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Zertico | ||
class Interactor | ||
def fail!(message = '') | ||
raise Zertico::Exceptions::InteractorException, message | ||
end | ||
|
||
def perform(params) | ||
fail!('Need to overwrite this method!') | ||
end | ||
|
||
def rollback(params) | ||
# Should overwrite! | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Zertico | ||
class Organizer | ||
def define_interactors(interactors = []) | ||
@interactors_classes = interactors.flatten | ||
end | ||
|
||
def perform(params) | ||
@params = params | ||
begin | ||
@interactors_classes.each_with_index do |interactor_class, i| | ||
@index = i | ||
response_hash = interactor_class.new.perform(@params) | ||
@params.merge!(response_hash) | ||
end | ||
true | ||
rescue Zertico::Exceptions::InteractorException | ||
rollback | ||
end | ||
end | ||
|
||
def rollback | ||
(0..@index-1).each do |i| | ||
@interactors_classes[i].new.rollback(@params) | ||
end | ||
false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'spec_helper' | ||
|
||
describe Zertico::Interactor do | ||
let(:interactor) { Zertico::Interactor.new } | ||
|
||
describe '#fail' do | ||
it 'should raise the interactor exception' do | ||
expect { interactor.fail! }.to raise_error | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require 'spec_helper' | ||
|
||
describe Zertico::Organizer do | ||
let(:organizer) { Zertico::Organizer.new } | ||
|
||
describe '#define_interactors' do | ||
context 'when it not receive params' do | ||
it 'should return an empty array' do | ||
organizer.define_interactors.should == [] | ||
end | ||
end | ||
|
||
context 'when it receive some params' do | ||
it 'should return a filled array' do | ||
organizer.define_interactors([Zertico::Interactor]).should == [Zertico::Interactor] | ||
end | ||
end | ||
end | ||
|
||
describe '#perform' do | ||
context 'when it not raise an exception' do | ||
before :each do | ||
organizer.define_interactors([Zertico::Interactor]) | ||
Zertico::Interactor.stub_chain(:new, perform: {}) | ||
end | ||
|
||
it 'should return true' do | ||
organizer.perform({}).should be_true | ||
end | ||
end | ||
|
||
context 'when it raise an exception' do | ||
before :each do | ||
organizer.define_interactors([Zertico::Interactor]) | ||
Zertico::Organizer.stub(rollback: false) | ||
end | ||
|
||
it 'should return false' do | ||
organizer.perform({}).should be_false | ||
end | ||
end | ||
end | ||
|
||
describe '#rollback' do | ||
context 'when it raise an exception' do | ||
before :each do | ||
organizer.define_interactors([Zertico::Interactor]) | ||
organizer.instance_variable_set("@index", 0) | ||
end | ||
|
||
it 'should return false' do | ||
organizer.rollback.should be_false | ||
end | ||
end | ||
end | ||
end |