diff --git a/lib/exceptions/forbidden.rb b/lib/exceptions/forbidden.rb new file mode 100644 index 0000000..abd8976 --- /dev/null +++ b/lib/exceptions/forbidden.rb @@ -0,0 +1 @@ +class Forbidden < StandardError; end diff --git a/lib/payment_service_factory.rb b/lib/payment_service_factory.rb new file mode 100644 index 0000000..67fc519 --- /dev/null +++ b/lib/payment_service_factory.rb @@ -0,0 +1,11 @@ +require 'services/user_payment_service' +require 'services/manager_payment_service' + +class PaymentServiceFactory + def self.for(entity) + case entity + when User then Services::UserPaymentService.new + when Manager then Services::ManagerPaymentService.new + end + end +end diff --git a/lib/services/manager_payment_service.rb b/lib/services/manager_payment_service.rb new file mode 100644 index 0000000..e87bca9 --- /dev/null +++ b/lib/services/manager_payment_service.rb @@ -0,0 +1,7 @@ +module Services + class ManagerPaymentService + def pay(recepient, amount) + amount + end + end +end diff --git a/lib/services/user_payment_service.rb b/lib/services/user_payment_service.rb new file mode 100644 index 0000000..89914e5 --- /dev/null +++ b/lib/services/user_payment_service.rb @@ -0,0 +1,7 @@ +module Services + class UserPaymentService + def pay(recepient, amount) + raise Forbidden, 'you have to be a manager to pay' + end + end +end diff --git a/spec/anything_spec.rb b/spec/anything_spec.rb index a55d95e..26f0624 100644 --- a/spec/anything_spec.rb +++ b/spec/anything_spec.rb @@ -11,10 +11,10 @@ let(:manager) { Manager.new('Boss') } it 'allows manager to perform payments' do - expect(described_class.for(manager).pay(user, 500)).not_to raise_error + expect { described_class.for(manager).pay(user, 500) }.not_to raise_error end it 'forbids user toperform payments' do - expect(described_class.for(user).pay(manager, 500)).to raise_error(Forbidden) + expect { described_class.for(user).pay(manager, 500) }.to raise_error(Forbidden) end end