Skip to content
This repository has been archived by the owner on Oct 23, 2018. It is now read-only.

Commit

Permalink
created interactor and organizers
Browse files Browse the repository at this point in the history
  • Loading branch information
mfbmina committed May 22, 2014
1 parent e722afa commit 754ec13
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/zertico.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ module Zertico
autoload :Service, 'zertico/service'
autoload :Accessor, 'zertico/accessor'
autoload :Responder, 'zertico/responder'
autoload :Organizer, 'zertico/organizer'
autoload :Interactor, 'zertico/interactor'
autoload :Exceptions, 'zertico/exceptions'
end
5 changes: 5 additions & 0 deletions lib/zertico/exceptions.rb
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
6 changes: 6 additions & 0 deletions lib/zertico/exceptions/interactor_exception.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Zertico
module Exceptions
class InteractorException < Exception
end
end
end
15 changes: 15 additions & 0 deletions lib/zertico/interactor.rb
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
28 changes: 28 additions & 0 deletions lib/zertico/organizer.rb
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
11 changes: 11 additions & 0 deletions spec/zertico/interactor_spec.rb
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
56 changes: 56 additions & 0 deletions spec/zertico/organizer_spec.rb
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

0 comments on commit 754ec13

Please sign in to comment.